I'm trying to extract two strings from each line, some lines don't have either string, some line has one string.
My input input.txt:
ID=MD001;refer=init;loc=tap2
ID=MD002;Name=Jam;refer=init;loc=tap2
ID=MD003;Name=Jane;Value=vip;refer=init;loc=tap2
ID=MD008;Name=George;product=car;vall=some;Value=vim;refer=init;loc=tap2
ID=MD0010;product=cars;Value=vip4;refer=init;loc=tap2
ID=MD0018;product=cars;
...
I want to match string "Name" or/and "Value", and output them out as:
ID=MD002 Name=Jam
ID=MD003 Name=Jane Value=vip
ID=MD008 Name=George Value=vim
ID=MD0010 Value=vip4
I tried:
head input.txt | awk '$1 ~ /Name|Value/ {match($1, /(ID=*);.*(Name*);.*(Value.*)/, name); print name[1] name[2] name[3]}'
But it did print anything. Thanks for the help.
Could you please try following.
awk '
BEGIN{
FS=";"
}
{
for(i=2;i<=NF;i++){
if($i~/^Name|^Value/){
val=(val?val OFS:"")$i
}
}
if(val){
print $1,val;
}
val=""
}
' Input_file
Output will be as follows for shown samples.
ID=MD002 Name=Jam
ID=MD003 Name=Jane Value=vip
ID=MD008 Name=George Value=vim
ID=MD0010 Value=vip4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With