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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-13-2004
handak9 handak9 is offline
Registered User
  
 

Join Date: Jul 2004
Posts: 31
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,
  #2 (permalink)  
Old 07-13-2004
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
Try

sed -e 's/^2\trunning/2\tfailed/' < testfile
  #3 (permalink)  
Old 07-13-2004
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
Maybe...

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

Or with awk...

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

Join Date: Jul 2004
Posts: 31
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?
  #5 (permalink)  
Old 07-13-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
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
  #6 (permalink)  
Old 07-13-2004
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
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".
Sponsored Links
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 10:16 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