Need help in solving to obtain desired print output using awk or perl or any commands, Please help!!

I have an file which have data in lines as follows

ad, findline=24,an=54,ab=34,av=64,ab=7989,ab65=34,aj=323,ay=34,au=545,ad=5545
ab,abc,an10=23,an2=24,an31=32,findline=00,an33=23,an32=26,an40=45,ac23=5,ac=87,al=76,ad=26
ab,abc,an1=23,ar=57,an10=24,findline=00,at=39,am=67,an31=35,an33=23,an3=26,an40=95,ac=55

I want to obtain output as below

findline=00 an10=23 an31=32 an40=45
findline=00 an10=24 an31=32 an40=95

i have been using the below commands before, with the below output

gzip -dcr/file path/filename.gz | grep findline=00 | awk -F, '{print $1,$4,$6,$7}' | sort -n | uniq -c

OUTPUT

1 findline=00 an10=23 an31=32 an40=45
1 findline=00 an10=24 an31=32 an40=95

INPUT File Lines (eg)

findline=02,ab,abc,an19=23,an1=28,an31=36,an40=41,an33=22,an32=28,ac23=53,am=57,al33=56,an3=56
findline=04,ab,abc,an14=24,an1=27,an31=334,an40=98,ar33=59,an32=22,ac23=19,am=17,al33=13,an3=76,ac=55
findline=00,ab,abc,an10=23,an1=24,an31=32,an40=45,an33=23,an32=26,ac23=5,am=87,al33=76,an3=16
findline=00,ab,abc,an10=24,an1=23,an31=35,an40=95,ar33=57,an32=24,ac23=39,am=67,al33=23,an3=26,ac=55

But since my variable that I am looking out for an10 , an31 and an40 are in different fields, in the new files, I cant obtain an uniform output with the above command.
Any idea how to get the desired output using the desired command

Please use code tags as required by forum rules!

It's not that easy to infer your requirements from what you've posted, so I hope I'm on the right path. Why don't you help us help you by thinking twice and giving a detailed and precise spec?
Try to pipe your data through

awk '
BEGIN           {FS=","
                 for (i=split("findline,an10,an31,an40", t); i; i--) FLD[t]=i
                }

/findline=00/   {for(i=1; i<=NF;i++)    {T=$i; sub (/=.*$/, _, T)
                                         if (T in FLD )
                                         OUT[FLD[T]]=$i
                                        }
                 for (f=1; f<=4; f++) printf "%s ", OUT[f];
                 print ""
                }
'
findline=00 an10=23 an31=32 an40=45 
findline=00 an10=24 an31=35 an40=95 
 

You are on right path, but need an input command like the one provided

Since you only supplied the output from your gzip command, there was no way for us to test with that command in scripts we were trying to build and test for you.

With what RudiC provided and the pipeline that we thought you had created, we assumed you would be able to change the first line of RudiC's script from:

awk '

to:

gzip -dcr/file path/filename.gz | awk '

to get what you want.

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk .

1 Like