Here is a fragment of a line in file:
LN=FINE FOODS & PHARMACEUTICALS NTM, MIC=XAIM, RIC=FF.MI, SG=MA1
I am interested in extracting the value of tag "MIC", i.e. my desired output is:
XAIM
The whole line is quite long:
20200403: #379 IT0005215329 {CU=EUR, GTPID=144115188076657542, II=IT0005215329, IS=18814564, LN=FINE FOODS & PHARMACEUTICALS NTM, MIC=XAIM, RIC=FF.MI, SG=MA1, SN=801670, STY=ORDINARY, TK="0.0002 to 0.1,0.0005 to 0.2,0.001 to 0.5,0.002 to 1,0.005 to 2,0.01 to 5,0.02 to 10,0.05 to 20,0.1 to 50,0.2 to 100,0.5 to 200,1 to 500,2 to 1000,5 to 2000,10 to 5000,20 to 10000,50 to 20000,100 to 50000,200", TS=FF, TY=S, UQ=1}
The position on the line of the tag "MIC" is not always the same.
I read through quite a few tutorials and it seems that all of their solutions involves creating a custom field separators and then extracting a desired pattern by using pattern's position on the line.
For example, I attempted to follow along the example given in this thread, namely I used this code to extract value from "MIC" tag:
awk 'BEGIN {FS="MIC=|,"} {print $2}' input.txt
I got the following output:
GTPID=144115188076657542
If you check the whole line sample that I provided above, the output is the value of the second tag "GTPID" that has "=" symbol. At first I was thinking that {FS="MIC=|,"}
meant "create two custom field separators, the 1st one being MIC=
and the 2nd one being ,
and for some reason I expected that {print $2}
will print out whatever is between those two field separators.
But obviously the code above prints the value of whatever pattern that contains symbol "=" happens to be 2nd on the line.
How do I extract value that is between MIC=
and ,
then?
sed -n 's/.*MIC=\([^,]*\).*/\1/p'
would do.