SED single-line script Quick Reference (Unix stream editor)

Source: Internet
Author: User
Article Title: SED single-line script Quick Reference (Unix stream editor ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Text interval:
--------

# Add an empty row after each row
Sed G

# Delete all original empty rows and add an empty row after each row.
# In this way, each row in the output text is followed by an empty row.
Sed '/^ $/d; G'

# Add two blank rows after each row
Sed 'G; G'

# Delete all empty rows generated by the first script (that is, delete all even rows)
Sed 'n'; D'

# Insert an empty row before matching the row with the style "regex"
Sed '/regex/{x; p; x ;}'

# Insert an empty row after matching the row with the style "regex"
Sed '/regex/G'

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

No:
--------

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

# Number of all rows in the file (the row number is left and the right of the text is aligned ).
Sed = filename | sed 'n'; s/^ //; s/* \ (. \ {6, \} \) \ N/\ 1 /'

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

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

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

# Unix environment: the new line character (CR/LF) for converting DOS is in Unix format.
Sed's/. $ // # assume that all rows end with 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 the Unix newline character (LF) to the DOS format.
Sed "s/$/'echo-e \ R'/" # Command Used in ksh
Sed's/$ '"/'echo \ R'/" # Command Used in bash
Sed "s/$/'echo \ R'/" # Command Used in zsh
Sed's/$/\ r/'# gsed 3.02.80 and later

# DOS environment: Convert Unix newline character (LF) to DOS format.
Sed "s/$ //" # method 1
Sed-n p # method 2

# DOS environment: Convert the DOS newline character (CR/LF) to Unix format.
# The following script is only valid for UnxUtils sed 4.0.7 and later versions. To identify the UnxUtils version
# Sed can use its unique "-- text" option. You can use the help option ("-- help") to view
# Whether there is a "-- text" item to determine whether the version is UnxUtils. Other DOS
# The sed version cannot perform this conversion. However, you can use "tr" to achieve this conversion.
Sed "s/\ r //" infile> outfile # UnxUtils sed v4.0.7 or later
Tr-d \ r Outfile # GNU tr 1.22 or later

# Delete the leading blank characters (spaces and tabs) of each line
# Align left
Sed's/^ [\ t] * // '# refer to the' \ t' usage description at the end of this article.

# Delete the blank characters (spaces and tabs) at the end of each line
Sed's/[\ t] * $ // # refer to the '\ t' usage description at the end of this article.

# Delete leading and trailing blank characters in each row
Sed's/^ [\ t] * //; s/[\ t] * $ //'

# Insert 5 spaces at the beginning of each line (to move the full text to the right five characters)
Sed's/^ //'

# Align all texts right with 79 characters in width
Sed-e: a-e's/^. \ {\} $/&/; ta '#78 characters plus the last space

# Use 79 characters as the width to center all texts. In method 1, to center the text before each row
# The header and the backend header are filled with spaces. In method 2, the text is only filled before the text in the center process.
# Spaces, and half of these spaces will be deleted. In addition, no spaces are filled in the backend of each row.
Sed-e: a-e's/^. \ {, 77 \} $/&/; ta '# method 1
Sed-e: a-e's/^. \ {\} $/&/; ta '-e's/\ (* \) \ 1/\ 1/' # method 2

# Search for the string "foo" in each row and replace the "foo" with "bar"
Sed's/foo/bar/'# Only Replace the first "foo" string in each row
Sed's/foo/bar/4' # Only Replace the fourth "foo" string in each row
Sed's/foo/bar/G' # Replace all "foo" in each row with "bar"
Sed's/\ (. * \) foo \ (. * foo \)/\ 1bar \ 2/'# Replace the last and second "foo"
Sed's/\ (. * \) foo/\ 1bar/'# Replace the last "foo"

# Replace "foo" with "bar" only when the string "baz" appears in the row"
Sed '/baz/s/foo/bar/G'

# Replace "foo" with "bar", and replace it only when "baz" is not displayed in the row.
Sed '/baz /! S/foo/bar/G'

# "Red" is used for both "scarlet", "ruby", and "puce"
Sed's/scarlet/red/g; s/ruby/red/g; s/puce/red/G' # effective for most sed
Gsed's/scarlet \ | ruby \ | puce/red/G' # only valid for GNU sed

# Invert all rows. The first line is the last line, and so on (simulate "tac ").
# For some reason, HHsed v1.5 deletes empty lines in the file when the following command is used
Sed '1! G; h; $! D' # method 1
Sed-n' 1! G; h; $ P' # method 2

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

# Concatenate each two rows into one line (similar to "paste ")
Sed '$! N; s/\ n //'

# If the current row ends with a backslash (\), the next row is added to the end of the current row.
# Remove the backslash at the end of the original line
Sed-e: a-e '/\ $/N; s/\ n //; ta'

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

# Add a comma separator for the numeric string and 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 a comma separator (GNU sed) for values with decimal points and negative numbers)
Gsed-R': a; s/(^ | [^ 0-9.]) ([0-9] +) ([0-9] {3})/\ 1 \ 2, \ 3/g; ta'

# Add a blank row after each 5 rows (add a blank row after rows 5, 10, 15, 20, and so on)
Gsed '0 ~ 5G '# only valid for GNU sed
Sed 'n'; n; G; '# other sed

Select to display specific rows:
--------

# Display the first 10 lines in the file (simulate the "head" behavior)
Sed 10q

# Display the first line of the file (simulate the "head-1" command)
Sed q

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

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

# Display the last line in the file (simulate "tail-1 ")
Sed '$! D' # method 1
Sed-n' $ P' # method 2

# Display the second and last lines in the file
Sed-e '$! {H; d;} '-e x # enter a blank line when there is only one row in the file.
Sed-e '1 {$ q;} '-e' $! {H; d;} '-e x # This row is displayed when there is only one row in the file.
Sed-e '1 {$ d;} '-e' $! {H; d;} '-e x # when there is only one row in the file, no output

# Only display rows matching Regular Expressions (simulate "grep ")
Sed-n'/regexp/P' # method 1
Sed '/regexp /! D' # method 2

# Show only the rows that do not match the regular expression (simulate "grep-v ")
Sed-n'/regexp /! P' # method 1, which corresponds to the preceding command
Sed '/regexp/d' # method 2, similar syntax

# Search for "regexp" and display the last line of the matched row, but not the matched row
Sed-n'/regexp/{g; 1! P;}; H'

# Search for "regexp" and display the next row of the matching row, but not the matching row
Sed-n'/regexp/{n; p ;}'

# Display the rows that contain "regexp" and the front and back rows, and add "regexp" before the first row
# Line number (similar to "grep-A1-B1 ")
Sed-n-e '/regexp/{=; x; 1! P; g; $! N; p; D;} '-e h

# Display rows containing "AAA", "BBB", or "CCC" (in any order)
Sed '/AAA /! D;/BBB /! D;/CCC /! D' # The string order does not affect the result

# Display rows containing "AAA", "BBB", and "CCC" (fixed order)
Sed '/AAA. * BBB. * CCC /! D'

# Display rows that contain "AAA" "BBB" or "CCC" (simulate "egrep ")
Sed-e '/AAA/B'-E'/BBB/B '-E'/CCC/B'-e d # majority of sed
Gsed '/AAA \ | BBB \ | CCC /! D' # valid for GNU sed

# Display the section containing "AAA" (separated by blank lines)
# HHsed v1.5 must add "G;" after "x;". This is the case for the next three scripts.
Sed-e '/./{H; $! D;} '-e' x;/AAA /! D ;'

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

# Display the section containing any string of "AAA", "BBB", and "CCC" (in 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

# Display rows containing 65 or more characters
Sed-n'/^. \ {65 \}/P'

# Display rows with less than 65 characters
Sed-n'/^. \ {65 \}/! P' # method 1, which corresponds to the above script
Sed '/^. \ {65 \}/d' # method 2, a simpler method

# Display part of the text-starting from the row containing the regular expression to the end of the last row
Sed-n'/regexp/, $ P'

# Display part of text -- specify the row number range (from 8th to 12th rows, including 8 and 12 rows)
Sed-n'8, 12p' # method 1
Sed '8, 12! D' # method 2

# Display rows 52nd
Sed-n '52p' # method 1
Sed '52! D' # method 2
Sed '52q; D' # method 3, which is more efficient in processing large files

# Display each 7 rows starting from 3rd
Gsed-n' 3 ~ 7p' # only valid for GNU sed
Sed-n' 3, $ {p; n;} '# other sed

# Display the text (inclusive) between two regular expressions)
Sed-n'/Iowa/,/Montana/P' # Case Sensitive Mode

[1] [2] Next page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.