The UNIX and Linux Forums  
Hello and Welcome from to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
extract a string from main string madhu.it Shell Programming and Scripting 1 10-06-2008 05:05 AM
extract a sub string from a main string madhu.it Shell Programming and Scripting 5 10-04-2008 03:22 AM
How to extract variable number from a String vitesse Shell Programming and Scripting 19 09-22-2008 10:03 AM
extract from string variable into new variables Sniper Pixie UNIX for Dummies Questions & Answers 2 03-03-2006 10:49 AM
How to extract a portion of a string from the whole string ds_sastry UNIX for Dummies Questions & Answers 2 09-29-2001 11:40 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-08-2009
modey3 modey3 is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 9
Using GREP to extract variable following a string

Hello,

I'm running calculations and I need to extract a specific number from a output file. So far I've only been able to GREP entire lines containing the string: '1 F=' . I would like to go a step further and extract just the number following '1 F='. The entire line looks like:

1 F= -.13250138E+03 E0= -.13249556E+03 d E =-.174650E-01 mag= 35.2157

The number I would like to extract is in red. Thanks for any help.

modey3

Last edited by modey3; 02-08-2009 at 10:08 PM..
  #2 (permalink)  
Old 02-08-2009
Radar's Avatar
Radar Radar is offline
Registered User
  
 

Join Date: Dec 2006
Location: Clifton Park, NY
Posts: 86
Code:
[radar@msi-k8t:~]$ cat testfile.txt
1 F= -.13250138E+03 E0= -.13249556E+03 d E =-.174650E-01 mag= 35.2157
[radar@msi-k8t:~]$ grep "1 F=" testfile.txt | awk '{print $3}'
-.13250138E+03
  #3 (permalink)  
Old 02-08-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
Quote:
Originally Posted by Radar View Post
Code:
[radar@msi-k8t:~]$ cat testfile.txt
1 F= -.13250138E+03 E0= -.13249556E+03 d E =-.174650E-01 mag= 35.2157
[radar@msi-k8t:~]$ grep "1 F=" testfile.txt | awk '{print $3}'
-.13250138E+03
why exactly do you need 'grep'?
  #4 (permalink)  
Old 02-08-2009
Radar's Avatar
Radar Radar is offline
Registered User
  
 

Join Date: Dec 2006
Location: Clifton Park, NY
Posts: 86
Quote:
Originally Posted by vgersh99 View Post
why exactly do you need 'grep'?
Because the file in question more than likely contains multiple lines, not all of which contain the needed pattern.
  #5 (permalink)  
Old 02-08-2009
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
I think vger's point is that awk can do that bit too:

Code:
awk '/1 F=/ { print $3 }' testfile.txt
  #6 (permalink)  
Old 02-09-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
Quote:
Originally Posted by Annihilannic View Post
I think vger's point is that awk can do that bit too:

Code:
awk '/1 F=/ { print $3 }' testfile.txt
Code:
awk '$1=="1" && $2 == "F=" { print $3 }' testfile.txt

Last edited by vgersh99; 02-10-2009 at 02:51 PM.. Reason: fixed: $2 == "F="
  #7 (permalink)  
Old 02-09-2009
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
Quote:
Originally Posted by vgersh99 View Post
Code:
awk '$1=="1" && $2 == "1 F=" { print $3 }' testfile.txt
Is that better somehow vger? I think you meant $2 == "F=" in there...
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:15 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0