Text Manipulation in Linux - you should immediately know grep, awk, sort, sed, and cut. The sed command manipulates text files directly from the Linux command line without even opening the file. The most commonly used command for substitution.
In this tutorial, we learn about sed command in Linux with its usage examples.
sed command
The Linux sed command is a stream editor which is used to process text file content like searching for patterns, finding and replacing, insertion, and deletion. The biggest advantage is that the sed command doesn't need to open the file to perform the above functions.
The sed command is most commonly used to search, find and substitute content in a file. When compared to the grep command, it offers more text processing options like substitution. The grep command is only capable of pattern matching, finding, and printing to the console.
Syntax
sed [options ...] script [file ...]
Different optional flags extend the behavior of the sed command. When the input file is missing, it accepts a stream of characters from the standard input. In addition, the script argument specifies the pattern to match each line of the input.
Let's input the text 'HelloWorld' to the sed command and substitute the 'World' with 'Folks!'.
echo HelloWorld | sed 's/World/Folks/'
The 's' specifies the substitution rule for the sed command which replaces the next string 'World' with the 'Folks!'.
Sed Example
In the following sections, we will look into practical examples of how to replace, delete, print text, and use regular expressions with the sed command.
Replace String
The sed command can be used with the substitution rule('s') to match and replace a given string in various ways. The sedexample1.txt file has been used as the input for every example. Let's inspect the content of the sedexample1.txt file.
cat sedexample1.txt
Replace a string
In this example, we replace the string 'World' with the 'Folks!' of each line as follows. It will substitute exactly one occurrence of the 'World' in each line.
sed 's/World/Folks!/' sedexample1.txt
Replace all occurrences of a string
We can use the 'g' option with the sed command to replace all the occurrences of a given string with another string in a line as follows. We will use the sedexample2.txt file here.
sed 's/World/Folks!/g' sedexample2.txt
Replace Nth occurrence of a string
It is possible to specify the occurrence of a given string along with the 'g' option to substitute it with another string in each line. In the following command, every second occurrence of the 'World' text will replace with the 'Folks!'.
sed 's/World/Folks!/2g' sedexample2.txt
Replace string on a specific line number
The following command substitutes the 'World' text in the fifth line with the text 'Folks!'. If you specify the rule like '3s/', this will do the substitution only in the third line.
sed '5s/World/Folks!/' sedexample2.txt
Printing replaced lines
By default, the sed command prints everything to the console. Hence, we can suppress it with the -n option. The p option within the script will print only the replaced lines as follows.
sed -n '5s/World/Folks!/p' sedexample2.txt
Replace string on a range of lines
The sed command can be used to substitute a given text in multiple lines. The following sed command replaces the text 'World' with the word 'Folks!' from the second line to the fifth line.
sed '2,5s/World/Folks!/' sedexample2.txt
Replace multiple strings
Multiple strings can be matched and replaced using multiple substitute expressions as shown in the following.
sed 's/World/Folks!/g; s/Hello/Hi/g' sedexample2.txt
sed 's/\(Hello\|World\)/Hey,Folks!/g' sedexample2.txt
Delete Lines
The rule 'd' can be used with the sed command to delete specified line/s from a text file. In the coming examples, we will use the sedexample3.txt file with the following content.
cat sedexample3.txt
Specific lines from a particular file
In the following command, the third line has been deleted. The 'd' option is used within the sed command script.
sed '3d' sedexample3.txt
To Delete the last line
The last line of the text file can be deleted using the $ sign as follows. The $ sign indicates the last line of a file.
sed '$d' sedexample3.txt
To Delete line from range x to y
It is possible to specify a range of line numbers to delete multiple lines. In the following example, lines from the third to sixth will be removed and the remaining lines will be printed.
sed '3,6d' sedexample3.txt
To Delete from nth to last line
Since the $ sign denotes the last line of a file, the sed command can be used to delete lines from a given line number to the last line without counting the number of lines.
sed '2,$d' sedexample3.txt
To Delete pattern matching line
A pattern can be specified in the sed command to match and delete. The following command deletes the line that matches the pattern 'thir'. This will delete the third line in the file.
sed '/thir/d' sedexample3.txt
Deleting Odd Lines
The sed command is used to delete a range of lines from an input text file. In the following example, the sed command deletes the odd lines and prints the remaining lines.
sed '1~2d' sedexample3.txt
The above result can be saved to a file by redirecting the result to the output.txt file using the > operator. You can check the content of the output.txt by executing the cat command.
sed '1~2d' sedexample3.txt > output.txt
Show file Content
The sed command behaves similarly to the cat command when it is used with an empty script as follows.
sed '' sedexample3.txt
Print Lines
By default, the sed command prints the text file content to the console. If you specify the option p in the script, it prints each line explicitly. This causes to print duplicate lines as follows.
sed 'p' sedexample3.txt
Print specific line
We can print a specific line by specifying the line number along with the p option. To avoid duplicates, we suppress the automatic printing of lines by the sed command.
sed -n '6p' sedexample3.txt
Print range of lines
A range of lines can be printed by specifying the range along with the p option as follows. Again, the -n option has been specified to suppress the automatic printing.
sed -n '2,3p' sedexample3.txt
sed -n '2,+3p' sedexample3.txt
Print every other line
The range can be specified to print every other line using the ~ sign between line numbers. The following command will print the text in odd line numbers.
sed -n '1~2p' sedexample3.txt
Edit the source file
We can use the -i option to edit the original file and save the result to itself as follows.
sed -i '1~2d' sedexample3.txt
As shown in the above output, the sedexample3.txt file content has been altered.
Create backup and edit the source file
Editing the original file should be done with care. Hence, the sed command can be used to create a backup of the original file while editing it. The backup file name will be the same as the original file but with the specified extension.
sed -i.bak '1~2d' sedexample3.txt
The following command displays the contents of the backup file whose name is sedexample3.txt.bak.
As shown in the following output, the original file sedexample3.txt has been altered.
Remove special character
In this example, we use the specialchars.txt file as the input file. Let's inspect the content inside it using the CAT command.
cat specialchars.txt
We can use the following regular expression pattern to remove the * from each line.
sed 's/\*//g' specialchars.txt
Ignore Case
Usually, the sed command is case-sensitive when matching texts. The i flag can be specified to ignore the case sensitivity as shown in the following example. The text 'asia' is in uppercase in the case.txt file. In the sed command, we use i and matching lower case text 'asia'.
sed 's/asia/Asian Continent/i' case.txt
Use regular expression
We can use the regex command to manage text found in text files and string patterns used to create patterns that match or find text. Regex can detect patterns that contain strings, and these string patterns can use to match or locate text. It is often used in programming languages like Python, Perl, and Java. Regex is supported by text-manipulating tools such as sed, awk, and grep. Sed commands are used for simple sorting and searching tasks but with Regex, sed can perform advanced text-matching tasks.
For example, let's match all words start and end with specific characters. To do this, you must use the * symbol between the characters. However, you'll note that it only prints words that begin with Ns and conclude with R. Given below are song names from a text document that starts with N and ends with R. this command will skip all the empty lines and prints strings that start with an N and end with an R.
sed -n '/A*N/p' names.txt
Let's execute another example. Imagine you need to find strings starting from the @ symbol in a large text file with different types of strings, blank lines, and characters. Insert the following command line options. We can call these commands extended regular expressions.
sed -n '/^@/p' strings.txt
sed command options
The sed command accepts several options that are listed in the following table.
Options | Description |
---|---|
-n | Suppress the automatic printing of lines |
-i | Write or Modify the original file |
-e | Make multiple selections where you can write multiple expressions to select multiple pieces from the text content. |
Conclusion
When you think about text processing in Linux, the 3 best tools are grep, sed, and awk. Of course, sed is more powerful than grep as its offers more options such as substitute commands.
For text editing, a few additional commands to keep in mind are tr and cut.
Thanks for reading, please leave your suggestions and feedback in the below comment section.
Comments