| | |
Replacing string in nth line
![]() |
•
•
Join Date: Jun 2008
Posts: 2
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
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
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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
•
•
Join Date: Apr 2006
Posts: 148
Reputation:
Solved Threads: 40
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)
awk 'NR==4{$2="DEV"}1' file
![]() |
Other Threads in the Shell Scripting Forum
- Previous Thread: print first / last char from a file
- Next Thread: specify the current directory
| Thread Tools | Search this Thread |





