Replacing string in nth line

Reply

Join Date: Jun 2008
Posts: 2
Reputation: maxmave is an unknown quantity at this point 
Solved Threads: 0
maxmave maxmave is offline Offline
Newbie Poster

Replacing string in nth line

 
0
  #1
Jun 4th, 2008
Hello All,

How to replace a string in nth line of a file using sed or awk.

For Ex: test.txt

Line 1 : TEST1 TEST2 TEST3
Line 2 : TEST1 TEST2 TEST3 TEST4
Line 3 : TEST1 TEST2 TEST3 TEST5
Line 4 : TEST1 TEST2 TEST3 TEST6
Line 5 : TEST1 TEST2 TEST3 TEST7

i want to go to 4th line of a file and replace the word TEST2 in that line with the word DEV2.

any suggestions will be helpful.

Thanks

Rahul
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Replacing string in nth line

 
0
  #2
Jun 4th, 2008
Hey There,

For sed, you can just specify the line number, like substituting THIS for THAT on line 4:

sed 4s/THIS/THAT/

In Awk, I believe you can specify the NR variable to cherry-pick the line.

Best wishes,

MIke
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Replacing string in nth line

 
0
  #3
Jun 4th, 2008
if TEST2 is the second field and no where else and you want to replace only that second field,
Shell Scripting Syntax (Toggle Plain Text)
  1. awk 'NR==4{$2="DEV"}1' file
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC