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
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
replace only 1st word of a line if it comes in the subsequent lines at same postion. geeko Shell Programming and Scripting 9 06-03-2009 07:25 AM
Read line by line not word by word vadharah Shell Programming and Scripting 6 03-02-2008 02:29 PM
how to move word by word on command line pbsrinivas UNIX for Dummies Questions & Answers 1 11-23-2007 06:17 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

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 04-04-2007
stevefox stevefox is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 114
Question How to replace a word at a parcitular line

Could someone tell me how to replace a word at a particular line by a single SED or AWK command?

e.g. I have a file with the contents below:
Code:
$ cat file1
111 AAA
333 CCC
222 BBB
444 CCC

I want to replace the word "CCC" with a blank to get the desired output below:

Code:
111 AAA
333 CCC
222 BBB
444

I can get the above by the command below but I want it to work for any line using simpler commands.

Code:
paste -s file1 | sed -e s/CCC$// | tr -s '\11' '\12'


Any help will be appreciated.

Steve
  #2 (permalink)  
Old 04-04-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
If the pattern is in the last line of file
Code:
sed "$ s/CCC$//" file
If you know the line number of pattern
Code:
sed "4 s/CCC$//" file
  #3 (permalink)  
Old 04-04-2007
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
in perl:
Code:
perl -pi -e 's/CCC// if m/^444/' filename
replace 444 with the number present on the line where you want to do the change
  #4 (permalink)  
Old 04-04-2007
stevefox stevefox is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 114
Smile

Perfect!
Thanks guys!

Cheers
Steve
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:34 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