Register
It is currently Thu Apr 24, 2014 12:16 am

How to extract a substring from a text file


All times are UTC - 6 hours


Post new topic Reply to topic  [ 10 posts ] 
Author Message
 PostPosted: Thu Dec 16, 2010 3:05 am   

Joined: Thu Dec 16, 2010 3:01 am
Posts: 3
Hi!

I have several files that are all the sames, except they have different numbers inside.
I would like to write a script that can extract one particular number in every of these files and copy it to another new file, so that at the end, I have one file containing all these values I need.

Basically, in every file, there is a line which is like this :

Throughput 0.151315

So, I would like to locate this line first and then get the number that is after "Throughput" and copy this number in another file. I've been browsing how to do this but can't find the exact solution.

Can somebody help please ?
Thanks in advance!


Top
 Profile  
 PostPosted: Thu Dec 16, 2010 5:32 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 512
use parameter expansion
Code:
while read line
do [[ $line = Throughput* ]] && echo "${line#* }" >otherFile
done < mainFile


Top
 Profile  
 PostPosted: Thu Dec 16, 2010 12:05 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
Or
Code:
grep -e '^Throughput [0-9.]+' | cut -d' ' -f2 > otherFile

or
Code:
sed -n '/^Throughput [0-9.]\+/{s/^Throughput //;p}' > otherFile

etc.


Top
 Profile  
 PostPosted: Thu Dec 16, 2010 3:56 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
or
Code:
awk '/^Throughput/ { print $2; }' main*.txt > newfile.txt


Top
 Profile  
 PostPosted: Thu Dec 16, 2010 5:41 pm   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 224
Location: London - UK
Patsie's awk one looks nicest :)


Top
 Profile  
 PostPosted: Fri Dec 17, 2010 2:14 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 657
Location: Des Moines, Iowa
I like Watael's the best... it's using just bash builtins and bash parameter expansion to do the work. Very elegant.


Top
 Profile WWW  
 PostPosted: Fri Dec 17, 2010 2:34 am   

Joined: Thu Dec 16, 2010 3:01 am
Posts: 3
Thanks everyone !!!
I tried Watael's one and it works, except for one thing. In my file, there are lines that looks like this :

Throughput 0.24324
ThroughputFail 3.35545
ThroughputAbort 4.34243

If I apply Watael's method, it returns the 4.34243 because of course Throughput* means all these 3 lines and the last one will overwrite everything. How can I specify I only want the line with Throughput alone ? I guess I have to say Throughput *SPACE* but how to do it ?


@Patsie + choroba : I would like to understand your lines too, that looks interesting. Could you please explain what it does and explain the syntax in details if possible ? Also, where do we specify the input filename ?

Thanks again guys!


Top
 Profile  
 PostPosted: Fri Dec 17, 2010 9:09 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 512
Code:
while read line
do [[ $line = Throughput\ * ]] && echo "${line#* }" >otherFile
done < mainFile


Top
 Profile  
 PostPosted: Fri Dec 17, 2010 10:14 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
chogall wrote:
@Patsie + choroba : I would like to understand your lines too, that looks interesting. Could you please explain what it does and explain the syntax in details if possible ? Also, where do we specify the input filename ?

To understand, you should read on how grep, cut, sed and awk work. These are all handy tools, so probably read their man pages or try to google some introductory texts. To specify the parameters, you can either prepend cat input | to them, or put the list of input files before the first redirection sign (| or >).


Top
 Profile  
 PostPosted: Mon Dec 20, 2010 3:11 am   

Joined: Thu Dec 16, 2010 3:01 am
Posts: 3
Ok thanks watael and choroba :)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP