Question: Is it possible for me to combine multiple sed commands? Can I combine two sed commands and execute it as single sed command?
Answer: In our previous articles we learned sed with single commands — printing, deletion, substitute and file write.
In this article let us review how to combine multiple sed commands using option -e as shown below.
Syntax: #sed -e 'command' -e 'command' filename
Note: -e option is optional for sed with single command. sed will execute the each set of command while processing input from the pattern buffer.
Let us first create thegeekstuff.txt file that will be used in all the examples mentioned below.
# cat thegeekstuff.txt 1. Linux - Sysadmin, Scripting etc. 2. Databases - Oracle, mySQL etc. 3. Hardware 4. Security (Firewall, Network, Online Security etc) 5. Storage 6. Cool gadgets and websites 7. Productivity (Too many technologies to explore, not much time available) 8. Website Design 9. Software Development 10.Windows- Sysadmin, reboot etc.
1.Delete 4th and 2nd line from the input
This sed example deletes 4th and 2nd line from the file thegeekstuff.txt. Using “-e” option, you can give any number of commands with sed.
$ sed -e '4d' -e '2d' thegeekstuff.txt 1. Linux - Sysadmin, Scripting etc. 3. Hardware 5. Storage 6. Cool gadgets and websites 7. Productivity (Too many technologies to explore, not much time available) 8. Website Design 9. Software Development 10.Windows- Sysadmin, reboot etc.
2. Print the lines which matches the pattern1 and lines matches pattern2
This sed example prints all lines that matches either the pattern “Storage” or “Software”.
$ sed -n -e '/Software/p' -e '/Storage/p' thegeekstuff.txt 5. Storage 9. Software Development
3. Delete the first,last and all the blank lines from input
This sed example deletes the first line, last line and all the blank lines from input file.
$ sed -e '1d' -e '$d' -e '/^$/d' thegeekstuff.txt 2. Databases - Oracle, mySQL etc. 3. Hardware 4. Security (Firewall, Network, Online Security etc) 5. Storage 6. Cool gadgets and websites 7. Productivity (Too many technologies to explore, not much time available) 8. Website Design 9. Software Development
Comments on this entry are closed.
There’s also potential to use a semi-colon to combine multiple sed arguments.
For example, I use the following to watch httpd access logs:
RED=`echo -en ‘\e[32m’`
YELLOW=`echo -en ‘\e[93m’`
RESET=`echo -en ‘\e[00m’
sudo tail -f /mnt/netdata/_shares/logs/httpd/domain.com/$YEAR/$MON/domain-access-$MON-$YEAR.log |sed -e “s/^.*\]: //g;s/ http://www.domain.*\] \”/ – \”/g;s/ HTTP\/1\.[0-9]\”/\”/g;s/ [0-9][0-9][0-9] [0-9]* \”/ \”/g;s/http:\/\/www.domain.com//g;s/\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)/$RED\1$RESET/g;s/\(\”[^\”]*\”\)/$YELLOW\1$RESET/g;s/*169.254.254.254*//g”
yay cli! :]
Hi, Ramesh
I have been following your tutorial from the first tutorial of SED. It is really help me so much to understand better about SED.
Again, thanks very much.
Hi,
I have a file as 12000 lines like this:
a 1 2 3
a 2 3 4
a 5 6 9
…..
I would like to change “a” by “x” every 50 lines.
I know this command:
sed -e “s///”
I have to 2400 times.
Do you know easier way for taht?
To change “a” by “x” in every 50th line of a file, use the following.
$ sed ‘
:loop
$!N
s/^\(\([^\n]*\n\)\{49\}\)a\([^\n]*\)/\1x\3/
t
$!b loop
‘ filename
The command
sed ‘1~50s/a/x/g’ filename.txt >out.txt
also works
How do i use multiple sed in one file when I change words (i try to make a script). Like:
sed -e ‘s/cat/dog/g’ file.txt > file2.txt
sed -e ‘s/one/single/g’ file.txt > file2.txt
sed -e ‘s/car/bus/g’ file.txt > file2.txt
So I have a one file and i want another file which is otherwise same except those spesifix words has been changed to anothers?
HI is it possible to give the pattern to be replaced form a file for sed command.
the situation wat i have is like
input_file.txt
AAA 4
BBB 5
CCC 6
etc
and i need to replace all these patterns in a output_file.txt