The UNIX and Linux Forums  
Hello and Welcome from United States 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
Need to replace the first word of a line if it occurs again in the next line(shell) geeko Shell Programming and Scripting 4 06-18-2009 02:36 PM
Read line by line not word by word vadharah Shell Programming and Scripting 6 03-02-2008 02:29 PM
to get a line above the search word sachin.gangadha UNIX for Dummies Questions & Answers 16 01-07-2008 07:19 AM
Can a shell script pull the first word (or nth word) off each line of a text file? tricky Shell Programming and Scripting 5 08-17-2006 06:29 AM
How to read from a file line by line and do stuff spaceship Shell Programming and Scripting 4 03-17-2005 09:47 PM

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 07-01-2008
jaggesh jaggesh is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 2
query on how to search for a line and read 4th word from that line

Assume I have a text file as below:

me
con pi
ind ken
pras ur
me
con rome
ind kent
pras urs
pintu
con mys
ind pan
pras ki
con kit
ind sys

My requirement,

I need to search for "con rome" and if exists, then print 4th word from rome, i.e in above example, since "con rome" exist in one of the line, I need to print "urs". Please let me know how to do it using awk or sed commands.
  #2 (permalink)  
Old 07-01-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,262
Edit: Forget what I posted - I read "field" instead of word.

Last edited by zaxxon; 07-01-2008 at 07:20 AM..
  #3 (permalink)  
Old 07-01-2008
krishmaths krishmaths is offline
Registered User
  
 

Join Date: Sep 2006
Location: Mysore, India
Posts: 191
One solution:
Code:
awk '/con rome/,/\*/ {print}' file | awk 'NR==3 {print $2}'
  #4 (permalink)  
Old 07-01-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
If Perl is acceptable:

Code:
perl -0777 -ne'print $1,"\n" while /con rome\s*(?:\w+\s*){3}(\w+)/g' input
If the input does not contain empty lines:

Code:
perl -00 -ne'print $1,"\n" while /con rome\s*(?:\w+\s*){3}(\w+)/g' input

Last edited by radoulov; 07-01-2008 at 07:55 AM..
  #5 (permalink)  
Old 07-01-2008
jaggesh jaggesh is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 2
Thanks all for the help
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 11:19 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