The UNIX and Linux Forums  

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 Replacement for sed dbsurf Shell Programming and Scripting 0 01-25-2008 02:47 PM
Regarding Replacement rajx UNIX for Dummies Questions & Answers 2 05-22-2007 06:19 AM
Replacement of Delimiter panknil Shell Programming and Scripting 3 05-14-2007 04:35 AM
cpu replacement. help IMPTRUE UNIX for Dummies Questions & Answers 3 07-25-2003 05:40 AM
global replacement Babu UNIX for Dummies Questions & Answers 2 07-04-2001 06:02 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-13-2004
Registered User
 

Join Date: Jul 2004
Posts: 31
Stumble this Post!
Replacement using sed

Hi

I have the following file that i need to run a sed command on

1<tab>running
2<tab>running
3<tab>running
4<tab>running

I want to be able to replace a line i.e the second one with '2<tab>failed'. As the first number is unique that can be used to search for the relevant line (using ^2 i guess).

So i want the output to be

1<tab>running
2<tab>failed
3<tab>running
4<tab>running

Thanks,
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-13-2004
Moderator
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,432
Stumble this Post!
Try

sed -e 's/^2\trunning/2\tfailed/' < testfile
Reply With Quote
  #3 (permalink)  
Old 07-13-2004
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,243
Stumble this Post!
Maybe...

sed '/^2<tab>/s/running/failed/' testfile

Or with awk...

awk '$1==2 {gsub("running","failed")};1' testfile
Reply With Quote
  #4 (permalink)  
Old 07-13-2004
Registered User
 

Join Date: Jul 2004
Posts: 31
Stumble this Post!
Thanks.

sed '/^2/s/running/failed/' testfile works great.

I also have the scenario where I have the following

1<tab>sleeping<tab>100

and I want to replace the 'sleeping 100' part with 'running'.

How do I match a word and the remainder of the line?
Reply With Quote
  #5 (permalink)  
Old 07-13-2004
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Stumble this Post!
Message for Ygor....
I would have used the longhand version
awk '$1==2 {gsub("running","failed")} {print $0}' testfile

- I've never seen the ";1" shorthand before - is it just another way of saying {print $0} ??

Cheers
ZB
Reply With Quote
  #6 (permalink)  
Old 07-13-2004
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,243
Stumble this Post!
zazzybob,

Yes, you're right. From man awk...
Quote:
A pattern-action statement has the form:

pattern { action }

A missing { action } means print the line; a missing pattern always matches.
"1" is a shorthand for "1!=0" which always evaluates to "true".
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:33 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0