LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   help - extract value from file (https://www.linuxquestions.org/questions/linux-newbie-8/help-extract-value-from-file-881212/)

kaveiros 05-17-2011 09:26 AM

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.

grail 05-17-2011 09:47 AM

Code:

grep -o VAR15 file

EricTRA 05-17-2011 10:00 AM

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
which would give you:
Code:

VAR15=0.0
VAR15=1.0
VAR15=0.5

Looking forward to your participation in the forums. Have fun with Linux.

Kind regards,

Eric

MTK358 05-17-2011 11:17 AM

Code:

sed 's/VAR15=\([0-9]\+\.[0-9]\+\)/\1/'
This will replace each line with the value of "VAR15" on that line.

kaveiros 05-17-2011 01:02 PM

Quote:

Originally Posted by EricTRA (Post 4358929)
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
which would give you:
Code:

VAR15=0.0
VAR15=1.0
VAR15=0.5

Looking forward to your participation in the forums. Have fun with Linux.

Kind regards,

Eric

Great! That will do the job!

Thank you all for the instant replies!

Any ideas how to print/extract also other columns e.g. LON, LAT or ID ?

Thanks again

MTK358 05-17-2011 01:10 PM

Quote:

Originally Posted by kaveiros (Post 4359104)
Any ideas how to print/extract also other columns e.g. LON, LAT or ID ?

Code:

sed 's/VAR15=\([^,]\+\)/&/'
Just replace the "VAR15" part with the part you want to extract. Of if you want to get the field to extract from a shell variable, ise this:

Code:

sed 's/'"$field"'=\([^,]\+\)/&/'
Unlike my previous solution, this one will keep the "VAR15=" part.

kaveiros 05-17-2011 01:28 PM

Quote:

Originally Posted by MTK358 (Post 4359111)
Code:

sed 's/VAR15=\([^,]\+\)/&/'
Just replace the "VAR15" part with the part you want to extract. Of if you want to get the field to extract from a shell variable, ise this:

Code:

sed 's/'"$field"'=\([^,]\+\)/&/'
Unlike my previous solution, this one will keep the "VAR15=" part.

Thanks again!
Maybe I was not clear before. I meant to have at the same time VAR15 and LON, LAT, ID

Regards,

K.

MTK358 05-17-2011 01:56 PM

Quote:

Originally Posted by kaveiros (Post 4359127)
Thanks again!
Maybe I was not clear before. I meant to have at the same time VAR15 and LON, LAT, ID

I don't understand.

They are already "at the same time" in the original file, so what's the need for processing it?

kaveiros 05-17-2011 02:05 PM

Quote:

Originally Posted by MTK358 (Post 4359157)
I don't understand.

They are already "at the same time" in the original file, so what's the need for processing it?

What I mean is to have something like :

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.

MTK358 05-17-2011 03:36 PM

How about:

Code:

sed 's/([^,]\+,[^,]\+,[^,]\+,[^,]\+).*,VAR15=\([^,]\+\)/\1,&/'

grail 05-17-2011 08:03 PM

Code:

awk -F, '{printf "%s,%s,%s,",$1,$3,$4;match($0,/VAR15[^,]*/,f);print f[1]}' file

kaveiros 05-18-2011 03:19 AM

Quote:

Originally Posted by grail (Post 4359397)
Code:

awk -F, '{printf "%s,%s,%s,",$1,$3,$4;match($0,/VAR15[^,]*/,f);print f[1]}' file

Yes! That should it!

Thanks again all of you for the help!

K.


All times are GMT -5. The time now is 06:01 AM.