LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-23-2009, 09:27 PM   #1
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Rep: Reputation: 19
How to grab one number from a row in a text but not another number


Hi there,

I have a text file that contains the following string of numbers and letters:

Code:
Mean track length: 3.45 +/- 1.23 mm
or

Code:
Mean track length: 22.45 +/- 12.23 mm

In the first example, I would like to grab only 3.45 and write it into a new file. Then I would like to grab only 1.23 and write it into another file.
I have 80,000 files to do and those numbers can be different every time.

Thanks for any help/suggestions,

Mike

PS: I'm comfortable with simple grep, sed, awk, and tr scripts but not a hard line programmer.

Last edited by Mike_V; 04-23-2009 at 09:31 PM.
 
Old 04-23-2009, 09:43 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Here's one quick'n'dirty solution:
Quote:
awk '{print $4}' INFILE > OUTFILE
 
Old 04-23-2009, 09:58 PM   #3
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Original Poster
Rep: Reputation: 19
Hi Paulsm4,
That is just the simplest thing... yep, I like Linux.
And I tried to find a solution to this "problem" in google, without luck, but now: I'm a happy camper.
Thanks a lot!
Mike
 
Old 04-23-2009, 10:00 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
 awk '{print $4 > "newfile1.txt"; print $(NF-1)> "newfile2.txt"}' file
 
Old 04-23-2009, 10:07 PM   #5
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Original Poster
Rep: Reputation: 19
ghostdog74:

that's an elegant solution...

I have a quick question:

what's a good source to learn about the different codes such as:
$4
NF
(etc?)

Of course from the help I got above, I now know what $4 and NF stand for, but where could I find more?

Thanks,
Mike

Last edited by Mike_V; 04-23-2009 at 10:09 PM.
 
Old 04-23-2009, 10:22 PM   #6
Libu
Member
 
Registered: Oct 2003
Location: Chennai
Distribution: Slackware 12.1
Posts: 165

Rep: Reputation: 36
Google gave me these links:

http://stud.wsi.edu.pl/~robert/awk/.
Theres a section on awk variables.

http://www.vectorsite.net/tsawk.html
 
Old 04-23-2009, 10:57 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Go to the user guide
 
Old 04-24-2009, 06:00 AM   #8
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Thumbs up One more solution

hey,

I can help you in another way. But this may help you out in future.

Take your example, you have this line

Code:
Mean track length: 3.45 +/- 1.23 mm
Now, I suppose you have a big file containing these kind of lines.
Code:
for i in `cat file.txt`;

do

(echo $i | awk -F ":" '{print $2}' | awk '{print $1}') > file1
(echo $i | awk -F "-" '{print $2}' | awk '{print $1}') > file2

done;
This is a long code, but it helps you to understand awk a little more ;-)

Also, you can find more awk examples here.

http://www.tldp.org/LDP/abs/html/awk.html
http://www.pathogenomics.sfu.ca/brin...xcmds.html#awk
 
Old 04-24-2009, 07:00 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by vikas027 View Post
Code:
for i in `cat file.txt`;

do

(echo $i | awk -F ":" '{print $2}' | awk '{print $1}') > file1
(echo $i | awk -F "-" '{print $2}' | awk '{print $1}') > file2

done;
although its another way of doing things, but here are some points to take note nevertheless.
1) using for loop and cat has the potential to break if lines contains spaces (although can be worked around) and you want to capture whole line to variable. Use a while read loop is "better" in this sense.
Code:
while read line
do
....
done < file
2) its great that unix has pipes, but using it like that is inefficient, especially if its a very large file. for every line in file.txt, you have to execute awk twice. tools like awk( grep/sed) already is optimized to loop over files, therefore even without bash's for or while loop, calling awk one time is good enough to process the file.
 
Old 04-25-2009, 04:57 AM   #10
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Smile

Quote:
Originally Posted by ghostdog74 View Post
although its another way of doing things, but here are some points to take note nevertheless.
1) using for loop and cat has the potential to break if lines contains spaces (although can be worked around) and you want to capture whole line to variable. Use a while read loop is "better" in this sense.
Code:
while read line
do
....
done < file
2) its great that unix has pipes, but using it like that is inefficient, especially if its a very large file. for every line in file.txt, you have to execute awk twice. tools like awk( grep/sed) already is optimized to loop over files, therefore even without bash's for or while loop, calling awk one time is good enough to process the file.
Thanks for the feedback Ghostdog, frankly I am not very good in shell scripting. My codes are not generally very optimized, but yes I do manage to get things going

But, now I am focusing on make optimized scripts. Its my learning phase
 
  


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
highlight text of a file from specified line number tanveer Linux - General 9 12-06-2006 10:27 PM
need a number from a text file and do things with it - how? flipwhy Linux - Newbie 16 11-11-2006 11:37 AM
Grab text lines in text file LULUSNATCH Programming 1 12-02-2005 10:55 AM
Read in an Octal number from a text file using C++ pjordan Programming 2 11-18-2004 03:03 PM
replace a string/number in a text file jpan Linux - General 3 10-22-2004 09:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:32 PM.

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
Open Source Consulting | Domain Registration