LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-04-2012, 10:47 AM   #1
msbstar
Member
 
Registered: Jan 2010
Distribution: Ubuntu 10.4
Posts: 122

Rep: Reputation: 16
Locating a string and printing the immediate value which comes after it.


Hello everybody,

I have a text file containing hundreds of lines of words and numbers. Following is a sample:

-----------------------------------------------
.
.
.
counts=11 photons=23.5 emision=132.8 filt.=33
counts=67 photons=17.2 emision=237.1 filt.=21
counts=93 photons=12.7 emision=764.9 filt.=52
counts=19 photons=63.0 emision=631.2 filt.=35
.
.
.
-------------------------------------------------


I need a command in Linux which finds all the "emision" keywords in the file and then only returns their values (in this case, 132.8, 273.1, 764.9, 631.2).

Is Linux able to do this!?

Thanks in advance for the help.

Best regards,
Hassan
 
Old 10-04-2012, 11:01 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,061

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
^ what have you tried so far ? where are you stuck.
grep or awk will likely do what you need.
 
1 members found this post helpful.
Old 10-04-2012, 11:35 AM   #3
msbstar
Member
 
Registered: Jan 2010
Distribution: Ubuntu 10.4
Posts: 122

Original Poster
Rep: Reputation: 16
Hi schneidz,

Thanks for the reply.
Actually I had no idea how to do that.

Would you please give an example of grep printing, for example, the values of the "emision" keyword?

Thanks.
 
Old 10-04-2012, 11:43 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
There are multiple ways of doing what you ask.

This is probably the easiest way (assuming your example is accurate):
Code:
cut -c32-36 infile
The -c32-36 tells cut to show characters 32 to 36

Have a look at the cut man page for more details/options.

A more complex solution would be to use awk:
Code:
awk 'BEGIN { FS="[= ]" } { print $6 }' infile
or, using sed:
Code:
sed 's/.*emision=\(.*\) .*/\1/' infile
Up to you to figure out how the last 2 work
 
2 members found this post helpful.
Old 10-04-2012, 11:58 AM   #5
msbstar
Member
 
Registered: Jan 2010
Distribution: Ubuntu 10.4
Posts: 122

Original Poster
Rep: Reputation: 16
Thanks druuna.
 
  


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
getting garbage when printing array of string dynamically. ashish anand Programming 3 02-12-2012 11:32 PM
printing a string that unfolds with Python xadrith Programming 4 07-23-2008 01:13 PM
Excluding a string in awk when printing szahri Programming 2 03-22-2007 02:00 AM
C - Printing ELF String Table Index nixnoobie Programming 3 04-01-2005 06:10 AM
Weird string printing problem (in C) Keffin Programming 4 02-05-2004 03:09 AM


All times are GMT -5. The time now is 09:13 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration