LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-17-2011, 09:26 AM   #1
kaveiros
LQ Newbie
 
Registered: May 2011
Posts: 7

Rep: Reputation: 0
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.
 
Old 05-17-2011, 09:47 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,544

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Code:
grep -o VAR15 file
 
1 members found this post helpful.
Old 05-17-2011, 10:00 AM   #3
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
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
 
1 members found this post helpful.
Old 05-17-2011, 11:17 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

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

Last edited by MTK358; 05-17-2011 at 11:18 AM.
 
1 members found this post helpful.
Old 05-17-2011, 01:02 PM   #5
kaveiros
LQ Newbie
 
Registered: May 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Smile

Quote:
Originally Posted by EricTRA View Post
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
 
Old 05-17-2011, 01:10 PM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Quote:
Originally Posted by kaveiros View Post
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.
 
Old 05-17-2011, 01:28 PM   #7
kaveiros
LQ Newbie
 
Registered: May 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by MTK358 View Post
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.
 
Old 05-17-2011, 01:56 PM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Quote:
Originally Posted by kaveiros View Post
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?
 
Old 05-17-2011, 02:05 PM   #9
kaveiros
LQ Newbie
 
Registered: May 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by MTK358 View Post
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.
 
Old 05-17-2011, 03:36 PM   #10
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
How about:

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

Last edited by MTK358; 05-17-2011 at 03:37 PM.
 
2 members found this post helpful.
Old 05-17-2011, 08:03 PM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,544

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Code:
awk -F, '{printf "%s,%s,%s,",$1,$3,$4;match($0,/VAR15[^,]*/,f);print f[1]}' file
 
2 members found this post helpful.
Old 05-18-2011, 03:19 AM   #12
kaveiros
LQ Newbie
 
Registered: May 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to do search & replace on a text file--need to extract URLs from a sitemap file Mountain Linux - General 4 08-07-2015 10:52 AM
Prompt the user for a file to open, extract the XML and write to another text file. richiep Linux - Newbie 7 10-22-2010 03:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration