Re: Extract a substring of n digits from a string

From: Janis Papanagnou (Janis_Papanagnou_at_hotmail.com)
Date: 10/27/05


Date: Thu, 27 Oct 2005 02:16:53 +0200

Giacomo wrote:
> I need to extract a substring of n adjacent digits from every single
> line of a file. The position of the n digits are different from line to
> line.

What type of shell or programs you do have to use?

What have you tried to program thus far?

General outline, for example...
Depending on whether the shell/tool/program supports extended regular
expressions or not you have to either define a regexp like [0-9]{n} or
construct one from n sequences of [0-9]. This regexp must be embedded
within white space [ \t] or non-numerical patterns [^0-9] depending on
your requirements. Take care of the line boundaries, so you'll likely
have to consider start of line ^ for the left and end of line $ for
the right boundary. Finally extract the substring from the matching
part. Consider to add spaces to the front and read of the input line
to simplify the matching and extraction of the substring pattern.

> For example:
>
> asdasd 123 asd 191991 1234
> lijoioi 4567 asdi 67567 iojoii
>
> For n=4 the result for each line must be 1234 e 4567.

Janis



Relevant Pages

  • Re: Find the first numeric substring
    ... I need code that would extract the first numeric ... I might have a string that looks like this: ... because it is the first numeric substring or token. ... which will return an error if there are no digits in the string. ...
    (microsoft.public.excel.programming)
  • Re: Extract a substring of n digits from a string
    ... :> I need to extract a substring of n adjacent digits from every single ... The position of the n digits are different from line to ... : BashDiff: Super Bash shell ...
    (comp.unix.shell)
  • Re: Extract a substring of n digits from a string
    ... Giacomo wrote: ... > I need to extract a substring of n adjacent digits from every single ... The position of the n digits are different from line to ...
    (comp.unix.shell)
  • Re: Extract a substring of n digits from a string
    ... Giacomo wrote: ... > I need to extract a substring of n adjacent digits from every single ... The position of the n digits are different from line to ...
    (comp.unix.shell)
  • Re: Extract 5 digit number from string
    ... I only want the part to be extract which have 5 digits after each ... The following VBA UDF gives the desired result in all of your examples. ... Function Extr5D(str As String) As String ...
    (microsoft.public.excel.programming)