Results 1 to 3 of 3
Hello,
I want to add a line containing xyz after a line containing abc (there is only one of them) in about 60 files. I can do it with a ...
- 06-01-2010 #1Just Joined!
- Join Date
- Jun 2010
- Posts
- 5
simple sed question
Hello,
I want to add a line containing xyz after a line containing abc (there is only one of them) in about 60 files. I can do it with a command:
sed 's/abc/abc\nxyz/' <infile >outfile
How do I do this for all the files? I've looked at several tutorials and "Linux in a Nutshell" but can't work it out.
Thanks
Tony
- 06-01-2010 #2
Be aware that your sed command will split a line if that lines contains "abc" and does not ONLY contain "abc". For instance, if the input was:
then your code would change it to:Code:abcdef
If you want to add a new line after any line CONTAINING and not necessary EQUALLING "abc", you could do:Code:abc xyzdef
This would append the line "xyz" after any line containing "abc".Code:sed -e '/abc/ a xyz
In any case, this is not your main question.
There are basically two ways to do this for multiple files. Both involve writing a script: an executable file that contains mutliple commands. If the files match a pattern, you can do something simple:
On the other hand, if it's a bit more complicated and you want to specify each file, you could do:Code:for file in *.txt; do sed 's/abc/abc\nxyz/' < "$file" > "$file.new" done
Be aware that this second one can be used with the first approach (the script could be invoked as "./script *.txt").Code:for file in "$@"; do sed 's/abc/abc\nxyz/' < "$file" > "$file.new" done
Does this make sense?DISTRO=Arch
Registered Linux User #388732
- 06-01-2010 #3Just Joined!
- Join Date
- Jun 2010
- Posts
- 5
Cabhan,
Perfect. The first script was just the example I needed.
Thanks
Tony


Reply With Quote