The Stream Editor(sed) is a text manipulation program, that takes the input from stdin and from the text files, It writes to the stdout and modifies the input files accordingly. The text manipulation means deleting characters and words; Inserting text into the source file on the fly.
This is a transformation operation and quiet a handy skill to have for someone working in linux shell.
The sed
comprises of two operations, The first one is a regex search and match operation and the second one is replace operation accordingly. This combines the greater power of search and replace of text from stdin and from the flat files.
Here is general syntax of sed
command is:
# sed [-n] -e 'options/commands' files
# sed [-n] -f sed-scriptfile
# sed -i filename -e 'options/commands'
-e is the edit option used on the cli.
-f to take the sed commands from the scriptfile
-n or –quiet option supresses the output unless specified with -p or -s
We will look at some of the notable options the sed offers.
Some practical usecases, But before that we take at our sample README.txt.
Substitute and Replace with sed:
sed command offers the -s option which is exclusive for search and replace operation also known as search and substitution.
[vamshi@node02 sed]$ echo Welcome to LinuxCent | sed -e 's/e/E/' WElcome to LinuxCent
This replaces the e to E in the input received and prints to stdout.
We can apply the same to the Text file and achieve the same results.
[vamshi@node02 ~]$ sed -e 's|u|U|' README.txt centos debian redhat Ubuntu
But the important thins to be noted is that the first occurring pattern match per line is only replaced. In out case only 1 letter per line as the letter u is replaced in ubuntu by U.
Substitute and replace globally using the option -g
.
We run the below command stdin input stream as show below:
[vamshi@node02 sed]$ echo Welcome to LinuxCent | sed -e 's/e/E/g' WElcomE to LinuxCEnt
Running the global option g
on the fileinput as shown below.
[vamshi@node02 ~]$ sed -e 's/u/U/g' README.txt centos debian redhat UbUntU
Substitute the later occurrences using sed. We search for the 3rd occurrence of letter u and if matched replace it with U.
[vamshi@node02 ~]$ sed -e 's/u/U/3g' README.txt centos debian redhat ubuntU
In the above case we have seen the lowercase u has been replaced with Uppercase U at the third occurrence.
Now let us append the word to the end of the each line using the below syntax:
[vamshi@node02 ~]$ sed -e 's/$/ Linux/' README.txt centos Linux debian Linux redhat Linux ubuntu Linux
Adding text to the file data at the beginning of each line and writing to the stdout.
[vamshi@node02 Linux-blog]$ sed -e 's/^/Distro name: /' Distronames.txt Distro name: centos Linux Distro name: debian Linux Distro name: redhat Linux Distro name: ubuntu Linux
sed Interactive Editor: How to write the modified sed data into the same text file?
We can use the -i
Interactive Editor option in combination with most other sed options, the input file content is directly modified according to the command pattern.
Example Given.
[vamshi@node02 sed]$ sed -e 's/e/E/g' -i intro.txt [vamshi@node02 sed]$ cat intro.txt WElcomE to LinuxCEnt
We use the -i
option to append some text to a file as demonstrated as follows:
[vamshi@node02 Linux-blog]$ sed -i 's/$/ Linux/' README.txt [vamshi@node02 Linux-blog]$ cat README.txt centos Linux debian Linux redhat Linux ubuntu Linux
Here we append the words Linux to end of the each line
Alternate to -i
you can also use the output redirection to write to a new fileĀ as shown below.
[vamshi@node02 ~]$ sed -e 's/$/ Linux/' README.txt > OSnames.txt
Delete Operations with sed
Delete all the lines containing the pattern:
[vamshi@node02 ~]$ sed -e /ubu/d README.txt centos Linux debian Linux redhat Linux
Here we matched the word ubuntu and hence have deleted that line from output.
We can use the !
inverse operator with the delete, demonstrated as follows:
[vamshi@node02 Linux-blog]$ sed -e '/ubu/!d' Distronames.txt ubuntu Linux
Using the Ranges in sed
Extracting only the specific /BEGIN and /END pattern using sed.
[vamshi@node02 Linux-blog]$ cat Distronames.txt | sed -n -e '/^centos/,/^debian/p' centos Linux debian Linux
Substitution of Range of lines
[vamshi@node02 Linux-blog]$ sed -e '1,3s/u/U/' Distronames.txt centos LinUx. debian LinUx. redhat LinUx. ubuntu Linux.
Delete the . at the end of each line
[vamshi@node02 Linux-blog]$ sed -e 's/.$//' Distronames.txt
Print only the lines containing the word “hat”
[vamshi@node02 Linux-blog]$ sed -n -e '/hat/p' Distronames.txt redhat Linux
Use sed to Match the pattern insert text.
Insert the lines before the matched pattern in file
[vamshi@node02 Linux-blog]$ cat README.txt | sed -e '/centos/i\Distro Names ' Distro Names centos debian redhat ubuntu
The above scenario we have inserted the sentence “Distro Names” before the occurrence of the work centos.
[vamshi@node02 Linux-blog]$ cat Distronames.txt | sed -e '1a\------------' Distro Names ------------ centos debian redhat ubuntu
The ———— are appended to the text after the 1st line