Linux Sed command is a stream editor that can perform basic text transformation on input stream. It can be used for editing in shell scripts for non-interactive editing. Sed can be used to find and replace a pattern on the input.
Examples
The syntax of the command is:
sed ‘s/ //’
echo “Hello there” | sed -e ‘s/there/here/’
Output is: Hello here
Replaces just first instance of pattern in a line.
Here, 2 instances of ‘one’ exist in the file. But sed has replaced only the first one |
one two three one
four two three one |
Replace all the instances of the pattern, with the ‘g’ command of sed: |
four two three four |
Replace all the instances of the pattern for multiple files within a folder |
|
Example script |
|
Insert and Append
A pattern can be inserted to the file with ‘i’ command and appended with ‘a’ command. This insert and append operation use a newline for inserting and appending.
Insert a new line above |
|
Append a new line at the end |
|
Suppress the output by running sed in quiet mode. |
|
Printing the output |
|
Find the pattern (like grep) root in the file /etc/passwd.
|
root:x:0:0:root:/root:/bin/bash
|
Deleting lines 1 to 3 from the given file.
|
|
Writing to a file |
|