Link to home
Start Free TrialLog in
Avatar of CRC64
CRC64

asked on

Find a regular expression on a line given the character offset of that line

The problem I face is easier to explain with an example:

 if given the offset position of letter "h"  that is 6, return number 105
 if given the offset position of letter "s"  that is 57, return number 107

Here is the input text:
___________________________
105  hello world

106  today is friday
107  tomorrow is saturday
___________________________

I am using bash.

I found that  using "expr substr" I can locate the character at the given offset, but don't know how to proceed from there using grep.  I am happy with using other tools (sed - my preference- or awk ). If it has to be done with perl  I am happy to go that way to but I do not have experience with that language.

The regular expression would be "^[0-9]* "  that is a number followed by a space

Many thanks in advance for your help
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CRC64
CRC64

ASKER

Fantastic!

 Can you please explain the 3 lines of code below (or tell me which feature to look for) ?

  x=${input:0:$offset}

   x=${x##*}

  echo ${x%%[^0-9]*}
x=${x##*}
should be
x=${x##*
}
To remove everything up to the last newline in the first $offset characters of $input
Avatar of CRC64

ASKER

Thank You