How to grab one number from a row in a text but not another number
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.