Sed command with examples in Linux

Sed command with examples

Sed stands for stream editor. A stream editor performs the text transformation from input stream and the input stream may be a file or input from a pipeline.

#cat editing.txt
hi
hello
welcome
how are you
hope you are fine

. . .Sed command to print lines. . .

1.#sed ‘1p’ editing.txt
hi
hi
hello
welcome
how are you
hope you are fine

==> 1p which tells that print the first line and show the remaining lines. so we can see from the output hi is priented twice.

2.#sed -n ‘1p’ editing.txt
hi

===> -n options is nothing but noprint, (i.e) It prints only the first line and didn’t show the other lines.

3.#sed -n ‘2,4p’ editing.txt
hello
welcome
how are you

===> The above command is used to print only a certain range of lines.

4.#sed -n ‘$p’ editing.txt
hope you are fine

===> The above command displays only the last line. The ‘$’ symbol indicated the last line and ‘-n’is used for no print and it combines with option ‘p’ to displays only the last line .

. . .Sed Command to delete lines. . .

1. #sed ‘1d’ editing.txt

hello
welcome
how are you
hope you are fine

===> The above command used to delete only the fisrt line and display the remaining lines.

2. #sed ‘2,4d’ editing.txt
hi

===> The above command is used to delete a range of lines.

. . .Sed Command for search and replace. . .

1.#sed ‘s/hi/changed/’ editing.txt
changed
hello
welcome
how are you
hope you are fine

==> The above command search for hi and replace to changed and print the remaining lines

2.#sed -n ‘s/hi/changes/p’ editing.txt
changes

===> The above command search for hi and replace to changed and don’t print the remaining lines

3.#sed ‘1,4s/are/is/’ editing.txt
hi
hello
welcome
how is you
hope you are fine

===> The above command used to do search and replace only for a range of lines.

. . .How to delete empty lines using sed command. . .

#cat new_file
hi

The above line is empty

1.#cat new_file | sed -e ‘/^$/d’
hi
The above line is empty

2.#sed -e ‘/^$/d’ new_file
hi
The above line is empty

===> The above two commands produce the same results, It search for empty line and delete it. ‘^’ is nothing but starting of the line and ‘$’ tells the end of the line, so from the starting to ending of the line is empty means delete it.

. . .How to remove space from a word. . .

#cat remove.txt
s p a c e.

#cat remove.txt | sed ‘s/ //g’
space

# sed ‘s/ //g’ remove.txt
space

===> It nothing but search and replace, the above command search for empty space and replace it with nothig, so then space becomes nospace.

. . .How to remove a lines permanently from a file. . .

#cat new_file
hi

The above line is empty

#sed -i ‘2d’ new_file

===> This above command will delete the line number 2 permanently from the file new_file.

. . .How to assign numbers for lines using sed command. . .

#cat editing.txt
hi
hello
welcome

#sed ‘=’ editing.txt
1
hi
2
hello
3
welcome

===> In the above command ‘=’ symbol is used to assign number for each lines, it works like as same as ‘nl’ command.

. . .How to use Word Boundaries using sed command. . .

Word Boundaries – \<\>

#cat first_text
raj
rajkumar
i am rajkumar

#sed -e ‘s/\<raj\>/kumar/g’ first

kumar
kumarkumar
i am kumarkumar

===> Normally if u use search and replace it will replace any word which contains raj to kumar, if ‘g’ is specified.

#sed -e ‘s/\/kumar/g’ first_text
kumar
rajkumar
i am rajkumar

===> If u use Word Boundaries means the only exact work will be searched and get replaced.(Its same like as ‘grep -w’ command)

. . .How to include files in sed command. . .

#cat commands
raj
linux

#vim lol
{
s/raj/kumar/g
s/linux/linuzzzzzzzz/g
}

#sed -f lol commands
kumar
linuzzzzzzzz

===> The ‘ -f ‘ option is used to include files in command prompt.




Leave a Reply

Your email address will not be published. Required fields are marked *