Basic usage of Linux sed command

1. sed (stream editor)

Non-interactive editor that processes content one line at a time. (powerful streaming text editor)

During processing, the currently processed line is stored in a temporary buffer, called “pattern space”, and then the sed command is used to process the contents of the buffer. After the processing is completed, the contents of the buffer are sent to the screen. Then process the next line, and repeat until the end of the file. The file contents are not changed unless you use redirection to store the output. Sed is mainly used to automatically edit one or more files; simplify repeated operations on files; write conversion programs, etc.

2. sed syntax

Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
Usage: sed [options]... {script (if no other script)} [input file]...
sed [options] 'command' file(s)
sed [options] -f scriptfile file(s)

3. sed options

 -n, --quiet, --silent
                 Cancel automatic printing mode space
  -e script, --expression=script
                 Add "script" to the program's run list
  -f script file, --file=script file
                 Add "script file" to the program's run list
  --follow-symlinks
                 Follow soft links when modifying files directly
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if SUFFIX supplied)
  -c, --copy
                 use copy instead of rename when shuffling files in -i mode
  -b, --binary
                 does nothing; for compatibility with WIN32/CYGWIN/MSDOS/EMX (
                 open files in binary mode (CR + LFs are not treated specially))
  -l N, --line-length=N
                 Specifies the expected length of newlines for the "l" command
  --posix
                 Turn off all GNU extensions
  -r, --regexp-extended
                 Using extended regular expressions in scripts
  -s, --separate
                 Treat input files as separate files rather than one long continuous input
  -u, --unbuffered
                 Read minimal data from input files and refresh output more frequently
  -z, --null-data
                 separate lines by NUL characters
  --help
                 display this help and exit
  --version
                 output version information and exit

If there is no -e, --expression, -f or --file option, the first non-option argument is considered
sed script. Other non-option parameters are treated as input files. If there is no input file, the program will start from the standard
Enter the read data. 

4. sed command mode

a\ # Insert text below the current line.
i\ # Insert text above the current line.
c\ # Change the selected line to new text.
d # Delete, delete the selected row.
D # Delete the first line of the template block.
s #Replace specified characters
h #Copy the contents of the template block to the buffer in memory.
H # Append the contents of the template block to the buffer in memory.
g # Get the contents of the memory buffer and replace the text in the current template block.
G # Get the contents of the memory buffer and append it to the text of the current template block.
l # List cannot print a list of characters.
n # Read the next input line and use the next command to process the new line instead of the first command.
N # Append the next input line after the template block and embed a new line between them, changing the current line number.
p # Print the lines of the template block.
P # (uppercase) prints the first line of the template block.
q #Exit Sed.
b labelable # Branch to the marked place in the script, or branch to the end of the script if the branch does not exist.
r file # Read lines from file.
t label #if branch, starting from the last line, once the condition is met or T, t command, it will cause branching to the command with the label, or to the end of the script.
T label # Error branch, starting from the last line. Once an error or T, t command occurs, it will cause the branch to the command with the label, or to the end of the script.
w file # Write and append the template block to the end of file.
W file # Write and append the first line of the template block to the end of file.
! # Indicates that the following commands will take effect on all unselected lines.
= # Print the current line number.
# # Extend comments until the next newline character. 

5. sed replacement tag

g # means full replacement within the line.
p # means print line.
w # means writing lines to a file.
x # means swapping the text in the template block and the text in the buffer.
y # means translating one character into another character (but not used in regular expressions)
\1 # Substring matching tag
 & amp; # Matched string tag

6. sed metacharacter set

^ # Match the beginning of the line, such as: /^sed/ Matches all lines starting with sed.
$ # Matches the end of a line, such as: /sed$/ matches all lines ending with sed.
. # Matches any character that is not a newline character, such as: /s.d/ matches s followed by any character, and finally d.
* # Match 0 or more characters, such as: /*sed/ Matches all lines where the template is one or more spaces followed by sed.
[] # Matches characters within a specified range, such as /[sS]ed/ matches sed and Sed.
[^] # Matches a character that is not within the specified range, such as: /[^A-RT-Z]ed/ Matches lines starting with a letter that does not contain A-R and T-Z, followed by ed.
\(..\) # Match substrings and save matching characters, such as s/\(love\)able/\1rs, loveable is replaced by lovers.
 & amp; # Save search characters to replace other characters, such as s/love/ ** & amp;** /, love becomes **love**.
\< # Matches the beginning of a word, such as:/\<love/ Matches lines containing words starting with love.
\> # Matches the end of a word, such as /love\>/ Matches lines containing words ending with love.
x\{m\} # Repeat character x, m times, such as: /0\{5\}/ matches lines containing 5 0s.
x\{m,\} # Repeat the character x at least m times, such as: /0\{5,\}/ matches lines with at least 5 0s.
x\{m,n\} # Repeat the character x, at least m times and no more than n times, such as: /0\{5,10\}/ matches lines with 5 to 10 zeros. 

7. sed usage examples

1. Sample file

[root@master srv]# cat test.txt
John Daggett, 341 King Road, Plymouth MA
Alice Ford, 22 East Broadway, Richmond VA
Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Terry Kalkas, 402 Lans Road, Beaver Falls PA
Eric Adams, 20 Post Road, Sudbury MA
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston MA

2. Replacement

1) Replace MA with MBA

The first two slashes are the content to be matched. You can use regular expressions. The middle of the last two slashes is the content to be replaced, which is plain text.

[root@master srv]# sed 's/MA/MBA/' test.txt
John Daggett, 341 King Road, Plymouth MBA
Alice Ford, 22 East Broadway, Richmond VA
Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Terry Kalkas, 402 Lans Road, Beaver Falls PA
Eric Adams, 20 Post Road, Sudbury MBA
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston MBA

2) Use multiple instruction replacement

[root@master srv]# sed 's/MA/KFC/ ; s/PA/TCP/' test.txt
John Daggett, 341 King Road, Plymouth KFC
Alice Ford, 22 East Broadway, Richmond VA
Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Terry Kalkas, 402 Lans Road, Beaver Falls TCP
Eric Adams, 20 Post Road, Sudbury KFC
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston KFC

3. -f (use script file)

[root@master srv]# cat cat namestate
s/MA/OKK/
[root@master srv]# sed -f namestate test.txt
John Daggett, 341 King Road, Plymouth OKK
Alice Ford, 22 East Broadway, Richmond VA
Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Terry Kalkas, 402 Lans Road, Beaver Falls PA
Eric Adams, 20 Post Road, Sudbury OKK
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston OKK

4. -n (prevents the automatic display of input lines)

[root@master srv]# sed -n 's/MA/PPP/p' test.txt
John Daggett, 341 King Road, Plymouth PPP
Eric Adams, 20 Post Road, Sudbury PPP
Sal Carpenter, 73 6th Street, Boston PPP

5. Save the output text

[root@master srv]# sed -n 's/MA/PPP/p' test.txt > case.txt

6. Delete text

1) Delete the nth line of the file

[root@master srv]# sed '1d' test.txt
Alice Ford, 22 East Broadway, Richmond VA
Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Terry Kalkas, 402 Lans Road, Beaver Falls PA
Eric Adams, 20 Post Road, Sudbury MA
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston MA

2) Delete the n to m lines of the file

[root@master srv]# sed '1,5d' test.txt
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston MA

3) Delete the nth line to the last line

[root@master srv]# sed '2,$d' test.txt
John Daggett, 341 King Road, Plymouth MA

4) Match characters and delete the line where the character is located

[root@master srv]# sed '/John/d' test.txt
Alice Ford, 22 East Broadway, Richmond VA
Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Terry Kalkas, 402 Lans Road, Beaver Falls PA
Eric Adams, 20 Post Road, Sudbury MA
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston MA

5) Match characters and delete from the line where the character is located to the nth line

[root@master srv]# sed '/John/,3d' test.txt
Terry Kalkas, 402 Lans Road, Beaver Falls PA
Eric Adams, 20 Post Road, Sudbury MA
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston MA

6) Delete odd-numbered lines (1~2: starting from line 1, delete one line every 2 lines including itself)

[root@master srv]# sed '1~2d' test.txt
Alice Ford, 22 East Broadway, Richmond VA
Terry Kalkas, 402 Lans Road, Beaver Falls PA
Hubert Sims, 328A Brook Road, Roanoke VA
Sal Carpenter, 73 6th Street, Boston MA

7) Delete even-numbered rows

[root@master srv]# sed '0~2d' test.txt
John Daggett, 341 King Road, Plymouth MA
Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Eric Adams, 20 Post Road, Sudbury MA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA 

7.sed can read content from stdin (standard input)

[root@master srv]# cat test.txt | sed 's/MA/KL/'
John Daggett, 341 King Road, Plymouth KL
Alice Ford, 22 East Broadway, Richmond VA
Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Terry Kalkas, 402 Lans Road, Beaver Falls PA
Eric Adams, 20 Post Road, Sudbury KL
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston KL

8. -i (replace the original file with modified data)

[root@master srv]# sed -i 's/MA/KL/' test.txt
[root@master srv]# cat test.txt
John Daggett, 341 King Road, Plymouth KL
Alice Ford, 22 East Broadway, Richmond VA
Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Terry Kalkas, 402 Lans Road, Beaver Falls PA
Eric Adams, 20 Post Road, Sudbury KL
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston KL
# Since it is more dangerous when using the -i parameter, when we use the i parameter and add .bak at the end, a backup file will be generated.
[root@master srv]# sed -i.bak 's/MA/KL/' test.txt
[root@master srv]#ll test.txt.bak
-rw-r--r-- 1 root root 345 Nov 8 20:08 test.txt.bak

9. -g (global replacement)

[root@master srv]# sed 's/MA/GT/g' test.txt
John Daggett, 341 King Road, Plymouth KL
Alice Ford, 22 East Broadway, Richmond VA
Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Terry Kalkas, 402 Lans Road, Beaver Falls PA
Eric Adams, 20 Post Road, Sudbury KL
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston KL
# The g mark allows sed to match characters after the Nth time to be replaced.
[root@master srv]# echo "thisthisthisthis" | sed 's/this/THIS/2g'
thisTHISTTHISTHIS

10. Replace the delimiter, the s mark will consider the following character as the delimiter

[root@master srv]# sed 's:MA:GT:' test.txt
[root@master srv]# sed 's#MA#GT#' test.txt 

11. Add a line before and after the matched part in the file

1) Insert content in the next line matching the line starting with John

[root@master srv]# sed '/^John/a\hello world/' test.txt
John Daggett, 341 King Road, Plymouth KL
hello world/
Alice Ford, 22 East Broadway, Richmond VA
Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Terry Kalkas, 402 Lans Road, Beaver Falls PA
Eric Adams, 20 Post Road, Sudbury KL
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston KL

2) Insert content in the previous line matching the line starting with John

[root@master srv]# sed '/^John/i\hello world/' test.txt
hello world/
John Daggett, 341 King Road, Plymouth KL
Alice Ford, 22 East Broadway, Richmond VA
Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Terry Kalkas, 402 Lans Road, Beaver Falls PA
Eric Adams, 20 Post Road, Sudbury KL
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston KL

12. Replace the content between the equal signs in the text

[root@master srv]# cat test.txt
1 = 2
[root@master srv]# sed "s/\(.*\) = \(.*\)/\2 = \1/" test.txt
2 = 1

13. Delete comments and blank lines in the file

[root@master srv]# cat test.txt
1 = 2

#This is a file

[root@master srv]# sed -r '/^#.*/d;/^$/d' test.txt
1 = 2