50 Best SED Commands in Linux/Unix with Examples

By | February 26, 2022
SED Commands in Linux

SED stands for Stream Editor and it is a feature in GNU/Linux that helps to perform various functions on a file, like insertion, deletion, searching, and find and replace, with the help of certain commands. Also, SED commands are extensively used for performing substitution or find and replace in UNIX.

The original file content is not changed by default since SED commands perform all modifications on it temporarily. Moreover, we can store the modified content of the original file in another file, if needed. Manual text processing takes lots of effort, so SED commands make it easier.

Syntax

The basic syntax of the SED command is as follows:

sed [options]... [script] [file]

In this tutorial, we will make you familiar with various SED commands and also give examples to help you understand how to use them with ease. So, let us get started.

Prerequisites to Run SED Commands

To run the SED commands on your system, you first need to ensure that SED is installed in your machine.

You can check the version of the SED installed on your system by using the following command:

Vamware
$ sed --version

Best SED Commands

1. Basic Substitution

This SED command helps search and replace a particular part of the text with another text.

Vamware

The code snippet shown below searches the word “Day” and replaces it with the word “Coding”.

Code

$ echo "Happy Day" | sed 's/Day/Coding/'

Output

The same snippet can also be used to edit a complete file.

Vamware

Code

$ sed 's/Swift/Swift is used in ios/' file.txt

Output

Here “Swift” is found in “file.txt” and is changed to “Swift is used in iOS”.

2. Replace all the instances of the match in a particular line of the file

‘g’ option in the SED command makes it possible to replace all the instances of the matching pattern.

In the example below, a file named file.txt is created that contains some text. The third line contains the word “Python”, which gets replaced with the word “R”.

Code

$ cat file.txt
$ sed '3 s/Python/R/g' file.txt

Output

The “cat” function displays the file contents, as shown below. The second code line, which is a SED command, will replace all instances of “Python” with “R”.

3. Replace the second instance of the match in each line

If a string appears in a line of text file multiple times, then a particular occurrence can be replaced using the SED command.

In the example below, we have a named file.txt with some text. Here, we will use the SED command to replace the second instance of “Python” with “Perl”.

Code

$ cat file.txt
$ sed 's/Python/Perl/g2' file.txt

Output

The “cat” command displays the file contents as shown below. After that, the SED command will replace the second instance of “Python” with “Perl” in each line. The line having only one instance of the word “Python” will remain unaffected.

4. Replace the last instance of the match in each line

If a string appears in a line of text file multiple times, then we can replace the last occurrence using the SED command.

In the following example, there is a file named file.txt having some text. Here, we will use the SED command to replace the last instance of “easy” with “hard” in each line.

Code

$ cat file.txt
$ sed 's/\(.*\)easy/\1hard/' file.txt

Output

The “cat” command displays the file contents as shown below. After that, we will replace the last instance of “easy” with “hard” in each line using a SED command.

5. Replace the first match in a file

We need to replace the first match of the text “hard” with the text “easy”. Here, we will use ‘1’ in the SED command to match the first instance of the pattern.

Code

$ cat file.txt
$ sed '1 s/easy/hard/' file.txt

Output

The “cat” function displays the file contents, and then we will replace the first instance of “easy” with “hard” using a SED command.

6. Replace the last match in a file

We need to replace the text “easy” with the text “hard”. Here, we will use the ‘$’ in the SED command to match the last instance of the pattern.

Code

$ cat file.txt
$ sed '$s/easy/hard/' file.txt

Output

The “cat” function displays the file contents in the first code, as shown below. The SED command will then replace the last instance of “easy” with “hard”.

7. Escaping backslash in replace commands to manage to search and replace of file paths

Here is a SED command to add a backslash in the file path. For searching and replacing, it is necessary to escape a backslash in the file path.

Code

$ echo "Desktop/8085Microprocessor/assignment3" | sed 's;/;\\/;g'

Output

The file path, “Desktop/8085Microprocessor/assignment3” is provided as input in the SED command, and the following output will appear after running the above command.

8. Replace all files full path with its filename

Using the following SED command, we will retrieve the filename from the full path of the file:

Code

$ echo "Desktop/8085Microprocessor/assignment3" | sed 's/.*\///'

Output

The SED command will extract “assignment3” from the full path “Desktop/8085Microprocessor/assignment3”.

9. Substitute text but only if the match is found in the string

Here is a file named “file.txt” that contains some content. We will use three SED commands to replace text. The text, “marks” will be replaced by 93 in the line that has the text, “A_score”, the text “marks” will be replaced by 97 in the line that has the text, “C_score” and the text “marks” will be replaced by 95 in the line that has the text, “D_score”.

Code

$ cat file.txt
$ sed -e '/A_score/ s/marks/93/; /C_score/ s/marks/97/; /D_score/ s/marks/95/;' file.txt

Output

10. Substitute text but only if the match is found in the string

Here is a SED command that will replace the “marks” value in the line that does not contain the text, “B_score”.The “file.txt” contains three lines that do not include the text “B_score”. So, the “marks” text will be replaced by “All scored same” in three lines.

Code

$ cat file.txt
$ sed -i -e '/B_score/! s/marks/All scored same/;' file.txt

$ cat file.txt

Output

The “cat” function displays the content of the file.

The second line of the code will substitute the text.

11. Add string before the matching pattern using ‘\1’

The sequence of matching patterns of the SED command is denoted by ‘\1’, ‘\2’, and so on. The below command will search the pattern “World” and insert the pattern “Digital” exactly before the matched pattern.

Code

 $ echo "Hello World" | sed 's/\(World\)/Digital \1 /'

Output

The output is shown below after executing the command:

12. Delete matching line

We can use the ‘d’ option in the SED command to delete a line from the file. The following command will delete the line having the word “Pascal” from the file named “file.txt”:

Code

 $ sed '/Pascal/d' file.txt

Output

The “cat” command displays the content of the file.

The SED command will delete the line that contains the word “Pascal”.

13. Delete the matching line and two lines after the match

The following SED command will delete the complete line containing the word “Pascal” from the file named “file.txt” and two lines following it will also be deleted:

Code

 $ sed '/Pascal/,+2d' file.txt

Output

The “cat” function displays the content of the file.

The SED command will delete three lines starting from the bar where the “Pascal” word is found.

14. Delete all spaces at the end of the line of text

The following SED command will delete all extra spaces at the end of the line:

Code

$ cat file.txt
$ sed 's/ *$//' file.txt

Output

The “cat” function displays the content of the file.

The SED command will delete all the extra spaces at the end of the line.

15. Delete all lines that have a match two times within a line

The following SED command will delete those lines having two instances of the same word:

Code

$ cat file.txt
$ sed 's/C++/dl/i2;t' file.txt | sed '/dl/d'

As seen in the output, the first and second lines have two instances of the word “C++” so both will be deleted from the text file.

Output

The “cat” function displays the content of the file.

The SED command will delete the line having two instances of the match.

16. Delete all empty lines

In the following command, we will be deleting empty lines:

Code

$ cat file.txt
$ sed '/^$/d' file.txt

The text in the file name file.txt has many empty lines. After executing the SED command mentioned above, all empty lines will be deleted.

Output

The “cat” function displays the content of the file.

The SED command will delete all the empty lines from the file.

17. Delete all non-printable characters

Some characters like ‘\t’ can’t pass directly into the “echo” function to overcome this type of problem; we first declare a variable and assign a non-printable character to the variable. An assigned variable is then passed in the “echo” command as a non-printable character, and the output of “echo” is sent to the SED command.

In the example below, the ‘\t’ character is assigned to a variable, and the $tab is used in the “echo” command. The output of the `echo` command is sent in the `sed` command that will remove the character, ‘\t’ from the output.

Code

$ tab=$'\t'
$ echo Hello"$tab"Coding
$ echo "Hello"$tab"Coding | LANG=c sed 's/[^[:print:]]//g'

Output

In the first line of the code, ‘\t’ is assigned to the tab variable. The second line of the code is to check the assigned variable. In the third line, the output of the echo command is served to the SED command that deletes a non-printable character.

18. If there is a match in a line, append something to the end of the line

Add something to the end of the line if there is a pattern match in line.

The following SED command will add the text “and problem solving” at the end of the third line having the word “competitive”:

Code

$ cat file.txt
$ sed '/competitive/ s/$/ and problem solving/' os.txt

Output

The “cat” function displays the content of the file.

The second line of code will add the text to the end of the line that contains the defined pattern.

19. If there is a match in the line, insert a line before that line

The following SED command will insert a text just before the line that contains the specified text:

Code

$ cat file.txt
$ sed '/C++ is easy to write/ s/^/C++ is used in problem solving.\n/' file.txt

In the output below, the SED command will first search for the text pattern “C++ is easy to write” and insert the text “C++ is used in problem solving” just before it.

Output

The “cat” function displays the content of the file.

The second line of code will insert text before matched text pattern

20. If there is a match in the line, insert a text after that line

Here is a SED command to insert a text just after the line where the pattern is matched.

In the output below, the pattern “coding” is searched in the file, and “C++ can be used in problem solving” is inserted just after it.

Code

$ cat file.txt
$ sed 's/coding/&\nC++ can be used in problem solving/' file.txt

Output

The “cat” function displays the content of the file.

The second line of code will insert text after the line containing the matched text pattern.

21. If there is not a match, append something to the end of the line

Here is a SED command that will search those lines in “file.txt” that do not contain the text “is” and append the text “ and problem solving” at the end of each line. Here, the ‘$‘ symbol identifies the line where the new text will be appended.

Code

$ cat file.txt
$ sed '/is/! S/$/ and problem solving/' file.txt

Output

The “cat” function displays the content of the file.

The second line of code will insert text after the line that contains the specified text pattern.

22. If there is not a match, delete the line

From the file “file.txt”, we will delete all the lines that do not contain a specific pattern.

In the example below, the pattern “is” is not found in the third line; therefore, it is deleted.

Code

$ cat file.txt
$ sed '/is/!d/' file.txt

Output

The “cat” function displays the content of the file.

The second line of code will delete all the lines that do not contain the text pattern ‘is’.

23. Duplicate matched text after adding a space after the text

Here is a SED command that will search the word “very” in the file “file.txt”, and if the word exists, the same word will be inserted after the search word by adding space. Here, the ‘&’ symbol is used to append the duplicate text.

Code

$ cat file.txt
$ sed -e 's/very /& very /g' file.txt

Output

The “cat” function displays the content of the file.

The second line of code will add the duplicate word after the matched pattern.

24. Replace one list of strings with the new string

To understand this command, we will consider two files, namely “file.txt” and “file2.txt”, with each file containing some content.

The following SED command will match the first column of the two text files and replace the matching text with the value of the third column of the filefile2.txt”.

Code

$ cat file2.txt
$ cat file.txt
$ sed `cat file2.txt | awk '{print "-e s/"$1"/"$3"/"}'`<<<"` cat file.txt`"

Output

The “cat” function displays the content of the file.

The numbers 1001, 1002, 1003 from the first column of files “file.txt” and “file2.txt” are matched, so these are replaced by corresponding names of the third column of “file2.txt”.

25. Replace the matched string with another string

Here is a SED command that will search for the pattern “Python” in the output provided by the “echo” command. The matched pattern is replaced with “Python3”.

Code

$ echo "C++ C C# Python Swift Scala Pascal" | sed 's/Python/Python3/'

Output

“Python” is replaced with “Python3” after running the echo and SED commands.

26. Remove newlines from the file and insert ‘ | ’ at the end of each line

Here is a SED that command will replace each newline with ‘ | ’ in the “file.txt”. Here the “-z” option will separate the line by the NULL character.

Code

$ cat file.txt
$ sed -z 's/\n/ | /g' file.txt

Output

The “cat” function displays the content of the file.

The second line of the code will replace each newline with ‘ | ’.

27. Remove ‘,’ and add a newline to split the text into multiple lines

The following command will read ‘,’ character and replace it with the new line in the ‘file.txt’ file:

Code

$ cat file.txt
$ sed "s/, /\n/g" file.txt

Output

The “cat” function displays the content of the file.

The second line of the code will replace each ‘,’ character with the newline. Here ‘\n’ denotes a new line.

28. Find a case-insensitive match and delete line

We can use ‘I’ in the SED command for the case-insensitive match that indicates ignore case. The following SED command will search the line that contains the word ‘used’ and delete the line from the “file.txt” file.

Code

$ cat file.txt
$ sed '/used/Id' file.txt

Output

The “cat” function displays the content of the file.

After executing the second command, it will search for the line in the “file.txt” that matches with the pattern, i.e., “used”, for case-insensitive search will be deleted.

29. Find a case-insensitive match and replace it with new text

The following SED command will replace every case-insensitive match of “c++” with the word “Python” from the file “file.txt”:

Code

$ cat file.txt
$ sed 's/c++/Python/i' file.txt

Output

The “cat” function displays the content of the file.

The output for the SED command is shown below:

30. Find a case-insensitive match and replace all occurrences with its upper case of the exact text

The following SED command will make the case-insensitive search for the word “competitive coding” and replace all its occurrences with “COMPETITIVE CODING” in the file named “file.txt”. Here ‘\U’ is used to convert any text to all uppercase letters.

Code

$ cat file.txt
$ sed 's/\(competitive coding\)/\U\1/Ig' file.txt

Output

The “cat” function displays the content of the file.

After executing the command, the word “competitive coding” is replaced with “COMPETITIVE CODING”.

31. Find a case-insensitive match and replace all occurrences with its lower case of the exact new line

The following SED command will make the case-insensitive search for the word “C++” and replace all its occurrences with “c++” from “file.txt”. Here ‘\L’ is used to convert any text to all uppercase letters.

Code

$ cat file.txt
$ sed 's/\(c++\)/\L\1/Ig' file.txt

Output

The “cat” function displays the content of the file.

After executing the SED command, every occurrence of the word “C++” is converted into “c++”.

32. Replace all uppercase characters of the text with lowercase characters

The following SED command will search all uppercase characters in the “file.txt” file and replace the characters with lowercase letters by using ‘\L’:

Code

$ cat file.txt
$ sed  's/\(.*\)/\L\1/' file.txt

Output

The “cat” function displays the content of the file.

After executing the command, every uppercase letter is converted to lowercase, as shown below.

33. Search for a number in line and append any currency symbol before the number

The following SED command will search for a number from “file.txt” and insert the ‘$’ symbol before each number:

Code

$ cat file.txt
$ sed 's/\([0-9]\)/$\1/g' file.txt

Output

The “cat” function displays the content of the file.

After executing the SED command, the ‘$’ symbol will be inserted before every number, as shown below.

34. Add commas to numbers that have more than three digits

Here we will describe a SED command that will take a number as input from the “file.txt” and add a comma after each group of three digits, counting from the right for every number in the file. Here, ‘:a’ indicates the label, and ‘ta’ is used to iterate the grouping process.

Code

$ cat file.txt
$ sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' file.txt

Output

The “cat” function displays the content of the file.

After executing the command, a comma will be put after each group of three digits counting from the right, as shown below.

35. Replace tab character with four space characters

Here is a SED command that will replace every tab character (\t) with four spaces. Here we take the output of the “echo” command as input for the SED command. To replace all tab characters, the ‘g’ option is used. ‘$’ symbol in the SED command is to match tab characters.

Code

$ echo -e "Hello\tWorld\t" | sed $'s/\t/ /g'

Output

Every tab character gets replaced by four spaces, as shown below.

36. Replace four consecutive spaces with a single space character

Here is a SED command that will replace every consecutive four spaces with a single space character.

Code

$ echo -e "Hello    World    !" | sed $'s/    */ /g'

Output

All four consecutive spaces will be replaced with a single space, as shown below.

37. Truncate all lines to the first ten characters

Here is a SED command that will delete all characters from every line except the first ten characters in each line.

Code

$ cat file.txt
$ sed 's/\(^.\{1,10\}\).*/\1/' file.txt

Output

In the file “file.txt” every line is truncated to 10 characters, as shown below.

38. Searching for a string and appending some standard text after it

Here is the SED command that will search “Programmers” in the input text and append “ and Coders” after that text.

Code

$ echo "Happy coding to all Programmers" | sed 's/\(Programmers\)/\1 and Coders/'

Output

The output of the SED command mentioned is shown below:

39. Search for string regex and append some text after the second match in each line

Here, we will mention a SED command that will append the text “(Add New Text)” after the second match of string “C++” for every line in the file “file.txt”.

Code

$ cat file.txt
$  sed 's/\(C++\)/\1 (Add New Text)/2' file.txt

Output

The following output will appear after executing the SED command mentioned above:

In the second and third lines, there are two instances of the word “C++” after the second “C++” new text “(Add New Text)” will be appended.

40. Running multi-line SED scripts from a file

Multiple SED commands can be stored in a file and executed simultaneously using a single SED command. For example, we will create a file named “sedcmd” that contains commands to change all instances of “C++” to “Python” and all instances of “Easy” to “Hard” in the file “file.txt”.

sedcmd

s/C++/Python/
s/Easy/Hard/

Code

$ cat sedcmd
$ sed -f sedcmd  file.txt

Output

The “cat” function displays the content of the file.

The second line of command will change all the instances of “C++” and “Easy” to “Python” and “Hard”, respectively, as shown below:

41. Match a multi-line pattern and replace it with new multi-line text

Here is a SED command that will search the multi-line text “Python\nC++”, and if the pattern matches, the matching text will be replaced by the multi-line text, “Python3\nC” from the file “file.txt”. Also, we will use P and D in the SED command for multi-line processing.

Code

$ cat file.txt
$ sed '$!N;s/Python\nC++/Python3\nC/;P;D' os.txt

Output

The “cat” function displays the content of the file.

Here, the first and second lines get changed to “Python3” & “C” from “Python” & “C++”.

42. Replace order of two words in a text that match a pattern

Here is a SED command that will search for two words in the file “file.txt”, and if the pattern matches, it will swap the words. In the given instance, “Hello” is swapped with “World”.

Code

$ cat file.txt

$ sed -e 's/\([^ ]*\) *\([^ ]*\)/\2 \1/' file.txt

Output

The “cat” function displays the content of the file.

After the execution of the SED command, “Hello” is swapped with “World”.

43. Execute multiple SED commands from the command-line

Here is a SED command that will execute two SED commands at once for the file “file.txt”. “Hello” is replaced with “Happy” and “World” is replaced with “Coding.”. The ‘-e’ option in the SED command makes it possible to run multiple SED scripts from the command line.

Code

$ cat file.txt

$ sed -e 's/Hello/Happy/; s/World/Coding./' file.txt

Output

The “cat” function displays the content of the file.

The following is the output of the SED command mentioned above.

44. Combine SED with other commands

Here is a SED command that will combine the SED command with the “cat” command. The first SED command will take input from the “file.txt” file and send the output of the command to the second SED command after replacing the text “Python” with “Python3”. The second SED command will replace the text “Javascript” with “JavaScript”.

Code

$ cat file.txt
$ cat file.txt | sed 's/Python/Python3/'| sed 's/Javascript/JavaScript/i'

Output

The following output will appear after the execution of the SED command described above:

45. Insert empty line in a file

Here is a command to insert an empty line after every line in the file named “file.txt”. ‘G’ option is used to insert an empty line in a file.

Code

$ cat file.txt
$ sed G file.txt

Output

After running the above-mentioned SED command, an empty line is inserted after each line of the file, as shown below:

46. Replace all alpha-numeric characters by space in each line of a file

Here is a SED command that replaces all alpha-numeric characters with space in each line of a file. The “file.txt” is provided as input for the SED command.

Code

$ cat file.txt
$ sed 's/[A-Za-z0-9]//g' file.txt

Output

The following output will appear after running the aforementioned commands:

47. Use ‘&’ to print matched string

Here is a command to search the word starting with ‘L’ and replace the text by appending ‘Line starting from C is -’ with the matched word using the ‘&’ symbol. The flag ‘p’ is used to print the modified text.

Code

$ cat file.txt
$ sed -n 's/^C/Line starting from C is - &/p' file.txt

Output

The following output will appear after running the above commands:

48. Switch pair of words in a file

Create a text file named “file.txt” such that it contains the pair of words in each line.

The following command will switch the pair of words in each line of the file, “file.txt”:

Code

$ cat file.txt
$ sed  's/\([^ ]*\) *\([^ ]*\)/\2 \1/' file.txt

Output

The following output will appear after switching the pair of words in each line, as “C++” is swapped with “Sublime” and so on.

49. Capitalize the first character of each word

Here is a SED command that capitalizes every first letter of the word in the file “file.txt”.

Code

$ cat file.text
$ sed 's/\([a-z]\)\([a-zA-Z0-9]*\)/\u\1\2/g' file.txt

Output

The first letter of every word from file “file.txt” is capitalized as shown below:

50. Print line numbers of the file

SED command uses the ‘=’ symbol to print the line number before each line of a file. The following command will print the content of the “file.txt” file with the line number:

Code

$ sed '=' file.txt

Output

There are five lines in the “file.txt” file. So, the line number is printed before each line of the file, as shown below:

Conclusion

In this tutorial, we have covered 50 simple SED commands with examples that demonstrate their execution in GNU SED (Stream Editor). If you are a newbie to Linux and wish to learn the basics of SED commands to perform string manipulations, this article will definitely help you.

All in all, there are several SED commands that help you accomplish a wide range of tasks related to file manipulation, such as replacing a file content, deleting a file content, inserting an empty line, capitalizing characters, etc.

People are also reading: 

Author: Vinay Singh

I am a Full Stack Developer with a Bachelor's Degree in Computer Science, who also loves to write technical articles that can help fellow developers.

Leave a Reply

Your email address will not be published.