I have a file with around 12 000 lines.
pk=91001011964;fck=85;fcv=true;fpi=1;fci=cacf;fmd=1422745568,;fck=83;fcv=decoccm;fpi=1;fci=cacf;fmd=1423070648,;fck=87;fcv=false;fpi=1;fci=cacf;fmd=1422745568,;fck=86;fcv=true;fpi=1;fci=cacf;fmd=1422745568,;fck=35;fcv=54bed48ad84397433fef0a350d65a893;fpi=1;fci=cacf;fmd=1422745568,;fck=83;fcv=travide;fpi=1;fci=cacf;fmd=1422811693,;fck=88;fcv=true;fpi=1;fci=cacf;fmd=1422745568,;fck=84;fcv=10;fpi=1;fci=cacf;fmd=1422745568;vpi=ppi=1;pci=cacf;pmd=1423070648;pos=null;pod=0;pso=null;psd=0;vos=null
pk=91001045537;fck=86;fcv=true;fpi=1;fci=cacf;fmd=1421428238,;fck=85;fcv=true;fpi=1;fci=cacf;fmd=1421428238,;fck=87;fcv=false;fpi=1;fci=cacf;fmd=1421428238,;fck=88;fcv=true;fpi=1;fci=cacf;fmd=1421428238,;fck=84;fcv=10;fpi=1;fci=cacf;fmd=1421428238,;fck=35;fcv=cd9b9ed4fc167d8007dd9544114bc83f;fpi=1;fci=cacf;fmd=1421428238,;fck=83;fcv=demelog;fpi=1;fci=cacf;fmd=1421557377;vpi=ppi=1;pci=cacf;pmd=1421557377;pos=null;pod=0;pso=null;psd=0;vos=null
pk=91001440737;fck=86;fcv=true;fpi=1;fci=cacf;fmd=1421687966,;fck=85;fcv=true;fpi=1;fci=cacf;fmd=1421687966,;fck=87;fcv=true;fpi=1;fci=cacf;fmd=1421687966,;fck=88;fcv=true;fpi=1;fci=cacf;fmd=1421687966,;fck=84;fcv=10;fpi=1;fci=cacf;fmd=1421687966,;fck=35;fcv=7360072ca14cb5d5578a3902fbe51792;fpi=1;fci=cacf;fmd=1421687191;fck=78;fcv=de724a544277d79c14d19809fe51ab71;fpi=1;fci=cacf;fmd=1421687966,;fck=83;fcv=demelog;fpi=1;fci=cacf;fmd=1421816564;vpi=ppi=1;pci=cacf;pmd=1421816564;pos=null;pod=0;pso=null;psd=0;vos=null
I need to parse the file, look for specific string values in a specific order in the line and print or pick the closest column for that line. If that string value does not exist, print an empty column.
Let's say I have fck=35
. if it exists , print the searched string and the closest fmd. For line 3, it would be fck=35;fmd=1421687191
. If fck=35
does not exist, add an empty column and that would make fck=35;;
Let's say I'm looking after the for fck=78
. If it does exist, print the searched string and the closest fmd. For line 3, I will have fck=78;fmd=1421687966
. If fck=78
does not exist, add an empty column and that would make fck=78;;
.
The other conditions are the following
For fck=84 look for the next fcv
For fck=85 look for the next fcv
For fck=86 look for the next fcv
For fck=83 look for the next fcv
The next fcv for fck=83 can be naismc, decoide, decoccm, travide, travccm or mariccm
I will use line 3 as my example :
fck=35;fmd=1421687191;fck=78;fmd=1421687966;fck=84;fcv=10;fck=85;fcv=true;fck=86;fcv=true;fck=83;fcv=demelog
or (in case fmd or fcv are missing or at least some: this part is no longer line 3, only another line I've not put in my example but I guess it does not matter)
fck=35;;fck=78;;fck=84;fcv=10;fck=85;fcv=true;fck=86;;fck=83;fcv=demelog
I was thinking to do this in awk
or sed
. But I have a doubt on how powerful it is and for what I have seen so far, it is not helpful. Maybe Python can do the trick but I'm quite unfamiliar with Python.
Any help is welcome.
A mate of mine helped me out with this
awk -F\; -v fck=78 '{for (i=1;i<=NF;i++) if($i=="fck="fck) print "fck="fck";"$(i-1)}' name_of_the_file
As you can see, it is only taking one condition , here fck=78 when I need to have all the conditions above.
Any hints on what I can improve on that tiny script or anything that can make a difference would be great. But another script is possible. I was thinking about Python but I'm not sure on how to do that.
TIA
fmd
should be on the same string or you do not want to count strings at all? (becouse there are 2fsk=35
in lines)awk -F\; -v fck=35 fck=78 '{for (i=1;i<=NF;i++) if($i=="fck="fck) print "fck="fck" else if($i=="fck="fck) print "fck="fck";"$(i-1)}' file_name