Results 1 to 4 of 4
Hi everyone
I need to use sed or awk to append text to lines in which a pattern has been matched.
In other words, add the character '5' to every ...
- 04-24-2007 #1Just Joined!
- Join Date
- Apr 2007
- Posts
- 13
Awk/sed to add text
Hi everyone
I need to use sed or awk to append text to lines in which a pattern has been matched.
In other words, add the character '5' to every line containg the string 'BLAH'
I find the man pages very cryptic and don't have time at the moment to lear these applications. PLease help.
Thanks
- 04-24-2007 #2Linux User
- Join Date
- Aug 2006
- Posts
- 458
example input:
output:Code:aaaaa sadafsa blah asljfsldf asfdsad asfdsfad asfdfsafd blah xxxxx asdfsafdsa asfdasdfa sadfasdsaf
Code:# awk '/blah/{ $0=$0"5" } {print} ' file aaaaa sadafsa blah asljfsldf5 asfdsad asfdsfad asfdfsafd blah xxxxx5 asdfsafdsa asfdasdfa sadfasdsaf
- 04-24-2007 #3
Let's take an example: Suppose a1.txt contains the lines..see cat command output on a1.txt file.
$ cat a1.txt
BLAH arun
arun arun
arun BLAH
Jhola Linux BLAH Hurray!
Quiting after entring the line below
Bye ByeBLAH!!!
# this will give change BLAH to BLAH5
$ sed -n '/BLAH/s/BLAH/&5/ p' a1.txt
BLAH5 arun
arun BLAH5
Jhola Linux BLAH5 Hurray!
Bye ByeBLAH5!!!
#Same as above, but useful if a line contains multiple BLAH keyword
$ sed -n '/BLAH/s/BLAH/&5/gp' a1.txt
BLAH5 arun
arun BLAH5
Jhola Linux BLAH5 Hurray!
Bye BLAH5 ByeBLAH5!!!
# This will append your desired text lets say "5" at the last of the line which # contains BLAH keyword
$ sed -n '/BLAH/s/$/5/ p' a1.txt
BLAH arun5
arun BLAH5
Jhola Linux BLAH Hurray!5
Bye ByeBLAH!!!5
I hope this will help...or let us know.
Cheers!
/Arun Sangal
- 04-25-2007 #4Just Joined!
- Join Date
- Apr 2007
- Posts
- 13
Thanks guys for the quick response and the help. Sed is now my friend!


Reply With Quote