sed regular expression help

please consider this:

echo "11111*X*005010X279~ST*270*1111111*005010X279~BHT*0011*11" |  sed 's/.*\(005010X(\d)(\d)(\d)[A-Za-z]*\).*$/\1/'

i'm searching for first occurrence of 005010X [digit] [digit] [digit] [optional letter] while leaving rest of characters out.

:confused:

any tips? thnx in advance guys.

echo "11111*X*005010X279~ST*270*1111111*005010X279~BHT*0011*11" | sed 's/.*\(005010X[0-9]\{3\}[A-Za-z]\).*$/\1/'

gave

005010X279

is this what you looking for?

1 Like

thnx so much!,

for some reason i needed to adjust it to this:

echo "11111*X*005010X279E~ST*270*1111111*005010X279~BHT*0011*11" | sed 's/.*\(005010X[0-9][0-9][0-9][A-Za-z]\).*$/\1/'

What OS are you using?

>What OS are you using?
aix 5.3
using ksh

That should not make a difference then. Are you sure it makes a difference?

But anyway, the last letter is optional, so neither statement is according to specification. The trouble is that you can't really get it to work like this, because of .* at the beginning which becomes too greedy and so the second pattern gets matched..

echo "11111*X*005010X279X~ST*270*1111111*005010X279~BHT*0011*11" | sed 's/[*~]/\n/g' | sed -n '/005010X[0-9]\{3\}[A-Za-z]\{0,1\}/{p;q;}'

or

echo "11111*X*005010X279X~ST*270*1111111*005010X279~BHT*0011*11" | awk -F'~' '/005010X[0-9][0-9][0-9][A-Za-z]?/{print $1;exit}' RS=\*

Hello Scrutinizer

in following sed command

Could you pls. explain what {p;q;} does.

thanks

It means print the buffer and then quit (so that only the first match gets printed)

1 Like