SoFunction
Updated on 2025-03-03

SED single-line script quick reference Chinese version (Unix stream editor)

English title: USEFUL ONE-LINE SCRIPTS FOR SED (Unix stream editor)
Original title: HANDY ONE-LINERS FOR SED (Unix stream editor)

Compiled by: Eric Pement - Email: pemente[at]northpark[dot]edu Version 5.5
Translator: Joe Hong - Email: hq00e[at]126[dot]com

The latest (English) version of this document can be found at the following address:
/
/sed/

Other language versions:
Chinese -/sed1line_zh
Czech -/sed1line_cz.html
Dutch -/sed1line_nl.html
French -/sed1line_fr.html
German -/sed1line_de.html

Portuguese -/sed1line_pt


Text interval:
--------

# Add an empty line after each line
sed G

# Delete all original empty lines and add one empty line after each line.
# In this way, there will be only one empty line after each line in the output text.
sed '/^$/d;G'

# Add two empty lines after each line
sed 'G;G'

# Delete all empty lines generated by the first script (that is, delete all even lines)
sed 'n;d'

# Insert an empty line before matching the line of the style "regex"
sed '/regex/{x;p;x;}'

# Insert an empty line after matching the line of the style "regex"
sed '/regex/G'

# Insert an empty line before and after the line matching the style "regex"
sed '/regex/{x;p;x;G;}'

serial number:
--------

# Number each line in the file (simple left alignment). "Tab" is used here
# (tab, see the description of the usage of '\t' at the end of this article) Instead of spaces to align edges.
sed = filename | sed 'N;s/\n/\t/'

# Number all lines in the file (line numbers are on the left and the text is aligned at the right end).
sed = filename | sed 'N; s/^/ /; s/ *\(.\{6,\}\)\n/\1 /'

# Number all lines in the file, but only the line numbers of non-blank lines are displayed.
sed '/./=' filename | sed '/./N; s/\n/ /'

# Calculate the number of rows (simulate "wc -l")
sed -n '$='

Text conversion and substitution:
--------

# Unix environment: convert the new line characters (CR/LF) of DOS to Unix format.
sed 's/.$//' # Assume that all rows end in CR/LF
sed 's/^M$//' # In bash/tcsh, change Ctrl-M to Ctrl-V
sed 's/\x0D$//' # ssed, gsed 3.02.80, and later

# Unix environment: convert Unix's new line characters (LF) to DOS format.
sed "s/$/`echo -e \\\r`/" # Commands used under ksh
sed 's/$'"/`echo \\\r`/" # Commands used under bash
sed "s/$/`echo \\\r`/" # Commands used under zsh
sed 's/$/\r/' # gsed 3.02.80 and later

# DOS environment: convert Unix new line characters (LF) to DOS format.
sed "s/$//" # Method 1
sed -n p # Method 2

# DOS environment: convert DOS new line characters (CR/LF) to Unix format.
# The following script is only valid for UnxUtils sed 4.0.7 and later. To identify the UnxUtils version
# sed can be accessed through its unique "--text" option. You can use the help option ("--help") to view
# Are there any "--text" items in it to determine whether the UnxUtils version is used. Other DOS
# version of sed cannot perform this conversion. But this transformation can be achieved with "tr".
sed "s/\r//" infile >outfile # UnxUtils sed v4.0.7 or later
tr -d \r <infile>outfile # GNU tr 1.22 or later

# Delete the "whitespace characters" (spaces, tabs) leading to each line
# Align it left
sed 's/^[ \t]*//' # See the description of the usage of '\t' at the end of this article

# Delete the "whitespace characters" (spaces, tabs) that are trailing on each line
sed 's/[ \t]*$//' # See the description of the usage of '\t' at the end of this article

# Delete leading and trailing whitespace characters in each line
sed 's/^[ \t]*//;s/[ \t]*$//'

# Insert 5 spaces at the beginning of each line (moving the full text by 5 characters to the right)
sed 's/^/ /'

# Right-align all text with 79 characters as width
sed -e :a -e 's/^.\{1,78\}$/ &/;ta' # 78 characters plus the last space

# Center all text with 79 characters as width. In Method 1, in order to center the text before each line
# Both the header and the back are filled with spaces. In Method 2, only fill in front of the text during the centering text
# spaces, and eventually half of these spaces will be deleted. In addition, no spaces are filled after each line.
sed -e :a -e 's/^.\{1,77\}$/ & /;ta' # Method 1
sed -e :a -e 's/^.\{1,77\}$/ &/;ta' -e 's/\( *\)\1/\1/' # Method 2

# Find the string "foo" in each line and replace the found "foo" with "bar"
sed 's/foo/bar/' # Replace only the first "foo" string in each line
sed 's/foo/bar/4' # Replace only the fourth "foo" string in each line
sed 's/foo/bar/g' # Change all "foo" in each line to "bar"
sed 's/\(.*\)foo\(.*foo\)/\1bar\2/' # Replace the penultimate "foo"
sed 's/\(.*\)foo/\1bar/' # Replace the last "foo"

# Replace "foo" with "bar" only if the string "baz" appears in the line
sed '/baz/s/foo/bar/g'

# Replace "foo" with "bar", and only if the string "baz" does not appear in the line
sed '/baz/!s/foo/bar/g'

# Whether it is "scarlet", "ruby" or "puce", it will be replaced with "red"
sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g' #Effect for most sed
gsed 's/scarlet\|ruby\|puce/red/g' # only valid for GNU sed

# Invert all rows, the first row becomes the last row, and so on (simulate "tac").
# For some reason, HHsed v1.5 will delete empty lines in the file when using the following command
sed '1!G;h;$!d' # Method 1
sed -n '1!G;h;$p' # Method 2

# Arrange the characters in the line in reverse order, the first word becomes the last word,... (Simulate "rev")
sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'

# Concatenate every two lines into one line (similar to "paste")
sed '$!N;s/\n/ /'

# If the current line ends with a backslash "\", the next line is merged to the end of the current line
# and remove the backslash at the end of the line
sed -e :a -e '/\\$/N; s/\\\n//; ta'

# If the current line starts with an equal sign, merge the current line to the end of the previous line
# and replace the "=" of the original line with a single space
sed -e :a -e '$!N;s/\n=/ /;ta' -e 'P;D'

# Add comma-separated symbols to the numeric string, change "1234567" to "1,234,567"
gsed ':a;s/\B[0-9]\{3\}\>/,&/;ta' # GNU sed
sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' # Other sed

# Add comma separator (GNU sed) to values ​​with decimal points and negative signs
gsed -r ':a;s/(^|[^0-9.])([0-9]+)([0-9]{3})/\1\2,\3/g;ta'

# Add a blank line after every 5 lines (add a blank line after line 5, 10, 15, 20)
gsed '0~5G' # only valid for GNU sed
sed 'n;n;n;n;G;' # Other sed

Selectively display specific rows:
--------

# Show the first 10 lines in the file (simulate the behavior of "head")
sed 10q

# Display the first line in the file (simulate the "head -1" command)
sed q

# Show the last 10 lines in the file (simulate "tail")
sed -e :a -e '$q;N;11,$D;ba'

# Show the last 2 lines in the file (simulate the "tail -2" command)
sed '$!N;$!D'

# Show the last line in the file (simulate "tail -1")
sed '$!d' # Method 1
sed -n '$p' # Method 2

# Show the penultimate line in the file
sed -e '$!{h;d;}' -e x # When there is only one line in the file, enter an empty line
sed -e '1{$q;}' -e '$!{h;d;}' -e x # When there is only one line in the file, the line will be displayed
sed -e '1{$d;}' -e '$!{h;d;}' -e x # When there is only one line in the file, no output

# Show only lines matching regular expressions (simulate "grep")
sed -n '/regexp/p' # Method 1
sed '/regexp/!d' # Method 2

# Show only lines that "not" match regular expressions (simulate "grep -v")
sed -n '/regexp/!p' # Method 1, corresponding to the previous command
sed '/regexp/d' # Method 2, similar syntax

# Find "regexp" and display the previous line of the matching row, but does not display the matching row.
sed -n '/regexp/{g;1!p;};h'

# Find "regexp" and display the next row of the matching row, but does not display the matching row.
sed -n '/regexp/{n;p;}'

# Show the line containing "regexp" and its previous and next lines, and precede the first line with "regexp"
# Line number in line (similar to "grep -A1 -B1")
sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h

# Show rows containing "AAA", "BBB", or "CCC" (any order)
sed '/AAA/!d; /BBB/!d; /CCC/!d' # The order of the strings does not affect the result

# Show rows containing "AAA", "BBB" and "CCC" (fixed order)
sed '/AAA.*BBB.*CCC/!d'

# Show rows containing "AAA", "BBB" or "CCC" (simulate "egrep")
sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d # Most sed
gsed '/AAA\|BBB\|CCC/!d' # works for GNU sed

# Show paragraphs containing "AAA" (paragraphs are separated by blank lines)
# HHsed v1.5 must add "G;" after "x;", and the next 3 scripts are like this
sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;'

# Display paragraphs containing three strings "AAA", "BBB" and "CCC" (any order)
sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;/BBB/!d;/CCC/!d'

# Display paragraphs containing any string of "AAA", "BBB", and "CCC" (any order)
sed -e '/./{H;$!d;}' -e 'x;/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d
gsed '/./{H;$!d;};x;/AAA\|BBB\|CCC/b;d' # Only valid for GNU sed

# Show lines containing 65 or more characters
sed -n '/^.\{65\}/p'

# Show lines containing less than 65 characters
sed -n '/^.\{65\}/!p' # Method 1, corresponding to the above script
sed '/^.\{65\}/d' # Method 2, a simpler method

# Show part of text—start from the line containing the regular expression to the end of the last line
sed -n '/regexp/,$p'

# Display part of text—Specify line number range (from line 8 to line 12, including line 8 and 12)
sed -n '8,12p' # Method 1
sed '8,12!d' # Method 2

# Show line 52
sed -n '52p' # Method 1
sed '52!d' # Method 2
sed '52q;d' # Method 3, more efficient when processing large files

# Starting from line 3, display every 7 lines
gsed -n '3~7p' # Only valid for GNU sed
sed -n '3,${p;n;n;n;n;n;n;}' # Other sed

# Display text between two regular expressions (included)
sed -n '/Iowa/,/Montana/p' # case sensitive

Selectively delete a specific row:
--------

# Show the entire document except the content between two regular expressions
sed '/Iowa/,/Montana/d'

# Delete adjacent duplicate lines in the file (simulate "uniq")
# Only the first row in the duplicate row is retained, and other rows are deleted.
sed '$!N; /^\(.*\)\n\1$/!P; D'

# Delete duplicate lines in the file, regardless of whether they are adjacent or not. Pay attention to the cache that hold space can support
# size, or use GNU sed.
sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P'

# Delete all rows except duplicate rows (simulate "uniq -d")
sed '$!N; s/^\(.*\)\n\1$/\1/; t; D'

# Delete the beginning of the 10 lines in the file
sed '1,10d'

# Delete the last line in the file
sed '$d'

# Delete the last two lines in the file
sed 'N;$!P;$!D;$d'

# Delete the last 10 lines in the file
sed -e :a -e '$d;N;2,10ba' -e 'P;D' # Method 1
sed -n -e :a -e '1,10!{P;N;D;};N;ba' # Method 2

# Delete multiple rows of 8
gsed '0~8d' # Only valid for GNU sed
sed 'n;n;n;n;n;n;n;d;' # Other sed

# Delete the rows that match styles
sed '/pattern/d' # Delete the line with pattern. Of course pattern
# can be replaced with any valid regular expression

# Delete all empty lines in the file (the same effect as "grep '.' ")
sed '/^$/d' # Method 1
sed '/./!d' # Method 2

# Only the first row of multiple adjacent blank rows are retained. And delete empty lines at the top and end of the file.
# (Simulate "cat -s")
sed '/./,/^$/!d' #Method 1, delete the empty line at the top of the file, allowing the tail to keep an empty line
sed '/^$/N;/\n$/D' #Method 2, allowing a blank line to be kept at the top and no blank line at the end

# Only the first two rows of multiple adjacent blank rows are retained.
sed '/^$/N;/\n$/N;//D'

# Delete all empty lines at the top of the file
sed '/./,$!d'

# Delete all empty lines at the end of the file
sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' # Valid for all sed
sed -e :a -e '/^\n*$/N;/\n$/ba' # Same as above, but only valid for gsed 3.02.*

# Delete the last line of each paragraph
sed -n '/^$/{p;h;};/./{x;/./p;}'

Special Applications:
--------

# Remove nroff tag from man page (man page). Used under Unix System V or bash shell
# When using the 'echo' command, the -e option may be added.
sed "s/.`echo \\\b`//g" # Double brackets on the outer layer are required (Unix environment)
sed 's/.^H//g' # In bash or tcsh, press Ctrl-V and then Ctrl-H
sed 's/.\x08//g' # sed 1.5, GNU sed, ssed hexadecimal representation method used

# Extract the header of a newsgroup or e-mail
sed '/^$/q' # Delete all contents after the first empty line

# Extract the text section of a newsgroup or e-mail
sed '1,/^$/d' # Delete everything before the first empty line

# Extract "Subject" (title bar field) from the email header and remove the word "Subject:" at the beginning
sed '/^Subject: */!d; s///;q'

# Get reply address from the email header
sed '/^Reply-To:/q; /^From:/h; /./d;g;q'

# Get the email address. Based on the line of email generated by the previous script, further non-email will be added
# Partial shaved of address. (See the previous script)
sed 's/ *(.*)//; s/>.*//; s/.*[:<] *//'

# Add an angle bracket and space at the beginning of each line (quote information)
sed 's/^/> /'

# Remove angle brackets and spaces at the beginning of each line (dereference)
sed 's/^> //'

# Remove most HTML tags (including cross-row tags)
sed -e :a -e 's/<[^>]*>//g;/</N;//ba'

# Decode the uuencode file divided into multiple volumes. Remove file header information and only the uuencode encoding part is retained.
# Files must be passed to sed in a specific order. The following first version of the script can be entered directly on the command line;
# The second version can be placed in a shell script with execution permissions. (One by Rahul Dhesi
# scripts are modified. )
sed '/^end/,/^begin/d' file1 file2 ... fileX | uudecode # vers. 1
sed '/^end/,/^begin/d' "$@" | uudecode # vers. 2

# Sort the paragraphs in the file alphabetical order. Paragraphs are separated by (one or more lines) blank lines. GNU sed
# The character "\v" represents a vertical tab, and here it is used as a placeholder for newlines - of course you can
# Replace it with other characters that are not used in the file.
sed '/./{H;d;};x;s/\n/={NL}=/g' file | sort | sed '1s/={NL}=//;s/={NL}=/\n/g'
gsed '/./{H;d};x;y/\n/\v/' file | sort | sed '1s/\v//;y/\v/\n/'

# Compress each .TXT file separately, delete the original file after compression and convert the compressed .ZIP file
# Name the same name as the original one (just different extension). (DOS environment: "dir /b"
# Show filename without path).
echo @echo off >
dir /b *.txt | sed "s/^\(.*\)\.TXT/pkzip -mo \1 \/" >>


Use SED:Sed to accept one or more edit commands and apply them in turn after each line is read.
After reading the first line of input, sed applies all commands to it and outputs the result. Then read the second one
Enter the line, apply all the commands to it... and repeat the process. In the previous example, sed is set by standard input
Get input (i.e. command interpreter, usually in the form of pipeline input). Give one or more at the command line
When a file name is used as a parameter, these files replace the standard input device and become the input of sed. The output of sed will be
Send to standard output (monitor). therefore:

cat filename | sed '10q' # Use pipe input
sed '10q' filename # Same effect, but without using pipe input
sed '10q' filename > newfile # Transfer (redirect) the output to disk

To understand the instructions for using the sed command, including how to use these tasks through a script file (not from the command line).
Please see the second edition of sed & awk, author Dale Dougherty and Arnold Robbins
(O'Reilly, 1997;), "UNIX Text Processing", author
Dale Dougherty and Tim O'Reilly (Hayden Books, 1987) or a teaching written by Mike Arst
Process-The name of the compressed package is "" (found on many sites). To discover sed
The potential of , must have sufficient understanding of "regular expressions". The information about regular expressions can be viewed
"Mastering Regular Expressions" author Jeffrey Friedl (O'reilly 1997).
The man pages ("man") provided by Unix systems will also be helpful (try these commands
"man sed", "man regexp", or look at the section about regular expressions in "man ed", but
The information provided by the manual is relatively "abstract" - this is also what it has always criticized. However, it's not
It is a textbook for beginners to use sed or regular expressions, but only for those familiar with these tools
Some text references provided.

Parentheses syntax: The previous example basically uses single quotes ('...') instead of double quotes for sed commands
("...") This is because sed is usually used on Unix platforms. Unix shell under single quotes (command
The interpreter) does not interpret and execute dollar signs ($) and post-quotes (`...`). And under double quotes
The dollar sign will be expanded as the value of a variable or parameter, and the command in the quotes will be executed and replaced by the output result.
The content in the quotes afterwards. And when using exclamation mark (!) in "csh" and its derived shell, it needs to be preceded
Add a backslash for escape (like this:\!) to ensure that the example used above works normally
(Including in case of single quotes). The DOS version of Sed always uses double quotes ("...") instead of
Circle the command in quotes.

Usage of '\t': In order to keep the text in this article concise, we use '\t' in the script to represent a tab
symbol. But now most versions of sed cannot recognize the abbreviation of '\t', so when it is in the command line
When entering tab characters in a script, you should directly press the TAB key to enter the tab character instead of '\t'. The following work
All software supports '\t' as a regular expression character to represent tab characters: awk, perl, HHsed,
sedmod and GNU sed v3.02.80.

Different versions of SED: There will be some differences between different versions of sed. You can imagine that they are syntactical.
There will be differences. Specifically, most of them do not support the use of tags (:name) or sub-parameters in the middle of editing commands.
branch commands (b,t), unless placed at the end of those. In this document, we try to choose the most portable
syntax to enable most versions of sed users to use these scripts. However, the GNU version of sed allows
Use a more concise syntax. Imagine the mood when the reader sees a long command:

sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d

The good news is that GNU sed makes the command more compact:

sed '/AAA/b;/BBB/b;/CCC/b;d' # can even be written as
sed '/AAA\|BBB\|CCC/b;d'

Also, note that although many versions of sed accept such as "/one/s/RE1/RE2/" this comes with empty before 's'
but some of these versions do not accept such commands: "/one/! s/RE1/RE2/". At this time
Just remove the space in the middle.

Speed ​​optimization: When it needs to be improved for some reason (such as large input files, slower processor or hard disk, etc.)
When executing commands, you can consider adding an address expression before the replacement command ("s/.../...")
Increase the speed. For example:

sed 's/foo/bar/g' filename # Standard replacement command
sed '/foo/ s/foo/bar/g' filename # Faster
sed '/foo/ s//bar/g' filename # abbreviation

You can use "q" in your script when you only need to display the previous part of the file or if you need to delete the following content.
Command (exit command). This saves a lot of time when working with large files. therefore:

sed -n '45,50p' filename # Show lines 45 to 50
sed -n '51q;45,50p' filename # same, but much faster

If you have other single-line scripts that you want to share with you or you find the error in this document, please send a call
Submail to the author of this document (Eric Pement). Please remember to provide the sed version you are using in the email.
The operating system running by this sed and the appropriate description of the problem. The single-line script referred to in this article refers to the length of the command line
Sed scripts with a degree of 65 characters or less [Translation Note 1]. The various scripts in this document are listed below
Write or provide:

Al Aab # Created a "seders" mailing list
Edgar Allen # Many aspects
Yiorgos Adamopoulos # Many aspects
Dale Dougherty # sed & awk author
Carlos Duarte # Author of "do it with sed"
Eric Pement # Author of this document
Ken Pizzini # Author of GNU sed v3.02
. Ravenhall # Go to html tag script
Greg Ubben # has contributed a lot and helped a lot
-------------------------------------------------------------------------

Translator Note 1: In most cases, sed scripts can be written in a single line no matter how long they are (via the `-e' option and `;'
No.)—As long as the command interpreter supports it, the single-line script mentioned here can be written into one line and also has a length
Limited. Because the meaning of these single-line scripts is not that they appear in single-line form. Instead, let users
It is the point of being convenient to use these compact scripts on the command line.