Print a word after a match

Hi all,

I have the below line:

08 03 * * 1-5 XXXXXXXXXXXXX -ENVI LDNFOUAT10 -EXE xxxxxxxx -CONFIG \${xxxxx} -SUBCLASS RESET -START -EXTRAAPPARGS \" -env 38LDNFOUAT10 \"  >> /tmp/SRRC_xxxxxxx_start.log.`/usr/bin/date +\%Y\%m\%d` 2>&1

I want to print just one word after the string "-env", in this case the output would be 38LDNFOUAT10.
Sorry if this has been asked before, I've been googling and all I can do is print the line AFTER the match.

Please use code tags

awk -F"-env " '{split($2,a," ");print a[1]}' file
38LDNFOUAT10

This solution was posted just a few post before yours:

Use some time to read forum before post.

echo '08 03 * * 1-5 XXXXXXXXXXXXX -ENVI LDNFOUAT10 -EXE xxxxxxxx -CONFIG \${xxxxx} -SUBCLASS RESET -START -EXTRAAPPARGS \" -env 38LDNFOUAT10 \" >> /tmp/SRRC_xxxxxxx_start.log.`/usr/bin/date +\%Y\%m\%d` 2>&1' | awk -F"-env" '{split($2,a," "); print a[1]}'

That's odd, if I run the command you guys provided in my host (I'm using Cygwin) it works, however if I run it in a Solaris machine i'm getting a different output:

echo '08 03 * * 1-5 XXXXXXXXXXXXX -ENVI LDNFOUAT10 -EXE xxxxxxxx -CONFIG \${xxxxx} -SUBCLASS RESET -START -EXTRAAPPARGS \" -env 38LDNFOUAT10 \" >> /tmp/SRRC_xxxxxxx_start.log.`/usr/bin/date +\%Y\%m\%d` 2>&1' | awk -F"-env" '{split($2,a," "); print a[1]}'
5

What could be the reason?

---------- Post updated at 01:28 PM ---------- Previous update was at 01:25 PM ----------

Jotne, I've spent the morning searching in this and other forums and couldn't find what I was looking for. Thanks for the link though.

you could use nawk instead of awk

1 Like

Not sure why its not working, its ok on my ubuntu.
Maybe some of the other here nows?

Edit: after reading post from rajamadhavan, try
awk -F'-env' '{split($2,a," "); print a[1]}'
or
awk '{split($2,a," "); print a[1]}' FS="-env"
awk '{split($2,a," "); print a[1]}' FS='-env'

1 Like

Try using nawk instead of awk.
Seems like the Solaris awk is accepting only a single-character FS.

1 Like

Yes I tried nawk instead and it works, thanks for your help.