help - extract value from file
Hi to all,
I was wondering if it is possible to extract a value from a file with awk, grep or something similar. I have a file like this... ID1,NAME1,LAT1,LON1,VAR1=5.0,VAR2=7.0,VAR6=9.0,VAR15=0.0,VAR20=0.0 ID2,NAME2,LAT2,LON2,VAR1=6.0,VAR15=1.0,VAR20=5.0,VAR22=0.0 ID3,NAME3,LAT3,LON3,VAR1=10.0,VAR2=20.0,VAR3=8.0,VAR10=3.0,VAR15=0.5,VAR20=9.0 . . . and I want to extract VAR15 from each line (which can be at any column unfortunately - columns separated with commas - csv file), or VAR15 together with LATn,LONn from each line. Is it possible to do it with awk, grep or something other in linux? Thanks in advance. |
Code:
grep -o VAR15 file |
Hello and Welcome to LinuxQuestions,
I don't know if you only want to extract VAR15 or if you also want to extract the value that comes with it, which might seem logical to me. In the first case grail's solution works great. If you need the value, you could go for something like this: Code:
cat file |tr "," "\n" |grep VAR15 Code:
VAR15=0.0 Kind regards, Eric |
Code:
sed 's/VAR15=\([0-9]\+\.[0-9]\+\)/\1/' |
Quote:
Thank you all for the instant replies! Any ideas how to print/extract also other columns e.g. LON, LAT or ID ? Thanks again |
Quote:
Code:
sed 's/VAR15=\([^,]\+\)/&/' Code:
sed 's/'"$field"'=\([^,]\+\)/&/' |
Quote:
Maybe I was not clear before. I meant to have at the same time VAR15 and LON, LAT, ID Regards, K. |
Quote:
They are already "at the same time" in the original file, so what's the need for processing it? |
Quote:
ID1,LAT1,LON1,VAR15=0.0 ID2,LAT2,LON2,VAR15=1.0 ID3,LAT3,LON3,VAR15=0.5 as an extract when in the original file only ID, NAME, LAT and LON are always at the same column (the first 4) . I hope this make sense. Thanks for your time. K. |
How about:
Code:
sed 's/([^,]\+,[^,]\+,[^,]\+,[^,]\+).*,VAR15=\([^,]\+\)/\1,&/' |
Code:
awk -F, '{printf "%s,%s,%s,",$1,$3,$4;match($0,/VAR15[^,]*/,f);print f[1]}' file |
Quote:
Thanks again all of you for the help! K. |
All times are GMT -5. The time now is 06:01 AM. |