Help with awk command to extract messages

Need expert's opinion urgently.

I have a file which contains messages.
A message is separated by $ in this file.
In each message we need to get string aftr :20: and send it to an output file. If :20: is not prsent in a message then we need to search for :20C: and send the corresponding string to o/p file. if dat too is not present then search :20D: in same message. If nothing prsent among :20:,:20C:,:20D: then we need to send a blank line to o/p corresponding to that message.
Please help.

like
{1:F01ADA00}{2:I 05 }{4:

-}${1:F01ADAGGB2SA}{2:O9500000000}{3:{108:094123}}{4:
:20:STMMYM0
-}
o/p file:
<BLANK LINE>
STMMYM0

Many Thanks in advance.

Try:

perl -l -0044ne '/:20(?:C|D)?:(.*)/;print $1' input > output
1 Like
awk -F ":20[:|C:|D:]" '{print $2}' infile
1 Like

perl -l -0044ne '/:20(?:C|D)?:(.*)/;print $1' input > output

this worked fine for me.
Thanks bartus11. :slight_smile:
Can you please explain the functionality.

awk command is throwing an error.
May be I am doing something wrong as I am not much aware abt the syntax.

awk: syntax error near line 1
awk: bailing out near line 1

In solaris, use nawk or /usr/xpg4/bin/awk

Seems to print result from last match when line is missing all of :20: and :20C: and :20D:
This update fixes it by matching to nothing before doing the :20(CD): match

perl -l -0044ne '/()/;/:20(C|D)?:(.*)/;print $2' input > output