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 > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Text formatting question aliaa2a Shell Programming and Scripting 7 05-24-2008 07:27 AM
Question about sed. Inserting text in field? pcrs Shell Programming and Scripting 2 02-12-2007 02:56 PM
searching text question BG_JrAdmin UNIX for Dummies Questions & Answers 4 05-29-2005 08:52 AM
Text parsing question 98_1LE UNIX for Dummies Questions & Answers 3 03-24-2002 11:04 AM
Filtering text from a string Cozmic UNIX for Dummies Questions & Answers 5 03-15-2001 11:18 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-21-2008
DethLark DethLark is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 2
Question Another text filtering question

I want to remove everything from a file but the word following the search word.

Example:

crap crap crap crap SearchWord WordToKeep crap crap crap

How would I do this with say awk or grep?

Thank you!
  #2 (permalink)  
Old 02-21-2008
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Quote:
Originally Posted by DethLark View Post
I want to remove everything from a file but the word following the search word.

Example:

crap crap crap crap SearchWord WordToKeep crap crap crap

How would I do this with say awk or grep?

Thank you!
Code:
{ wordtokeep=$(sed -e "s/.*SearchWord \([^ ]*\) .*/\1/g" file); echo "$wordtokeep" > file; }
  #3 (permalink)  
Old 02-21-2008
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
Sorry to correct you, vino, but your code will work only in most cases, not in all: if the WordToKeep is the last word in the line it will fail because you require a space after it.

On the first line your code will work, on the second it will fail:

Code:
"crap crap searchword wordtokeep crap"
"crap crap searchword wordtokeep"
It will also fail in cases where the WordToKeep is followed by some punctuation mark like it is the case in texts:

Code:
"crap crap searchword wordtokeep, crap"

I therefore suggest the following enhancement to your code (<spc> and <tab> are characters, only encoded here for readability):

Code:
sed -e "s/.*SearchWord \([^<spc><tab>;,.!?":][^<spc><tab>;,.!?":]*\)/\1/g" file
bakunin
  #4 (permalink)  
Old 02-21-2008
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Thanks for that.

Cheers !
  #5 (permalink)  
Old 02-21-2008
DethLark DethLark is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 2
Well I knew it wasn't all that easy. Thanks for the answers.
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 09:21 PM.


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