Sed single-line Script Quick Reference Chinese version (Unix stream editor) _linux Shell

Source: Internet
Author: User
Tags compact html tags first row valid

English title: Useful one-line SCRIPTS for SED (Unix stream editor)
Original title: HANDY one-liners for SED (Unix stream editor)

Finishing: Eric pement-e-mail: pemente[at]northpark[dot]edu version 5.5
Translator: Joe-e-mail: hq00e[at]126[dot]com

The latest (English) version of this document can be found at the following address:
Http://sed.sourceforge.net/sed1line.txt
Http://www.pement.org/sed/sed1line.txt

Other language versions:
Chinese-http://sed.sourceforge.net/sed1line_zh-CN.html
Czech-http://sed.sourceforge.net/sed1line_cz.html
Dutch-http://sed.sourceforge.net/sed1line_nl.html
French-http://sed.sourceforge.net/sed1line_fr.html
German-http://sed.sourceforge.net/sed1line_de.html
-->
Portuguese-Http://sed.sourceforge.net/sed1line_pt-BR.html
-->

Text interval:
--------

# Add a blank line after each line
Sed G

# Remove all empty rows and add a blank line after each line.
# This will have a blank line after each line in the output text.
Sed '/^$/d; G

# Add two blank lines after each line
Sed ' G; G

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

# Inserts a blank line before the line that matches the style ' regex '
Sed '/regex/{x;p;x;} '

# Inserts a blank line after the line that matches the style ' regex '
Sed '/regex/g '

# Inserts a blank line before and after the line that matches the style ' regex '
Sed '/regex/{x;p;x; G;} '

Number:
--------

# number each line in the file (simple left alignment). "Tabs" are used here
# (tab, see the description of ' \ t ' usage at the end of this article) instead of the empty cells alignment edge.
sed = filename | Sed ' n;s/\n/\t/'

# The number of all the lines in the file (line number is on the left, the right side of the text is aligned).
sed = filename | Sed ' N; s/^//; s/*\ (. \{6,\}\) \n/\1/'

# numbers all the rows in the file, but only the line numbers for the nonblank lines.
Sed '/./= ' filename | Sed '/./n; s/\n//'

# Count rows (simulate "wc-l")
Sed-n ' $= '

Text Conversion and substitution:
--------

# UNIX Environment: a new line character (CR/LF) that converts DOS is in UNIX format.
Sed ' s/.$//' # assuming all rows end with CR/LF
Sed ' s/^m$//' # in Bash/tcsh, will press ctrl-m instead press Ctrl-v
Sed ' s/\x0d$//' # ssed, gsed 3.02.80, and later

# UNIX Environment: A new Line (LF) for converting UNIX is a DOS format.
Sed "s/$/' echo-e \\\r '/" # commands used under Ksh
Sed ' s/$ '/' echo \\\r '/' # The 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 character (LF) to DOS format.
Sed "s/$//" # method 1
Sed-n P # Method 2

# DOS Environment: Convert DOS New line character (CR/LF) to UNIX format.
# The following script is only valid for unxutils sed 4.0.7 and later. To identify the unxutils version of the
# sed is available through its unique "--text" option. You can use the Help option ("--help") to see
# there is no "--text" item to determine whether the Unxutils version is used. Other DOS
# version of SED is not able to do this conversion. However, you can use "TR" to implement this transformation.
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 leading "white space characters" (spaces, tabs) in each line
# Make it left aligned
Sed ' s/^[\t]*//' # See the description of ' \ t ' usage at the end of this article

# delete "White space characters" (spaces, tabs) trailing in each line
Sed ' s/[\t]*$//' # See the description of ' \ t ' usage at the end of this article

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

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

# 79 characters wide, align all text to the right
SED-E: A-e ' s/^.\{1,78\}$/&/;ta ' # 78 characters plus the last space

# is 79 characters wide, so that all text is centered. In Method 1, in order to center the text in front of each line
# The head and the back are filled with spaces. In Method 2, you only fill in the front of the text when you center the text
# spaces, and eventually half of these spaces will be deleted. In addition, no spaces are filled behind 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 row and replace the found ' foo ' with ' Bar '
Sed ' s/foo/bar/' only replaces the first "foo" string in each row
Sed ' S/FOO/BAR/4 ' only replaces the fourth "foo" string in each row
Sed ' s/foo/bar/g ' replaces all "foo" in each row with "bar"
Sed ' s/\ (. *\) foo\ (. *foo\)/\1bar\2/' # Replace the penultimate ' foo '
Sed ' s/\ (. *\) foo/\1bar/' # replaces the last "foo"

# replace ' foo ' with ' bar ' if string ' Baz ' appears in rows only
Sed '/baz/s/foo/bar/g '

# replace "foo" with "bar" and only if the string "Baz" does not appear in the row
Sed '/baz/!s/foo/bar/g '

# whether it's "scarlet" or "Ruby" or "puce", replace it with "red."
Sed ' s/scarlet/red/g;s/ruby/red/g;s/puce/red/g ' #对多数的sed都有效
Gsed ' s/scarlet\|ruby\|puce/red/g ' only works for GNU SED

# Invert all rows, the first line becomes the last line, and so on (simulate "TAC").
# for some reason, hhsed v1.5 deletes empty rows from the file when you use the following command
Sed ' 1! G;h;$!d ' # method 1
Sed-n ' 1! G;h $p ' # method 2

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

# Connect 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 to the end of the current line
# and remove the backslash from the end of the line
SED-E: A-e '/\\$/n; s/\\\n//; Ta

# If the current line begins with an equal sign, the current line is at the end of the previous line
# and replace the original wardrobe with a single space ' = '
SED-E: A-e ' $! n;s/\n=//;ta '-e ' p;d '

# adds a comma-delimited symbol to the number string, changing "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

# adds a comma separator to a value with a decimal point and minus sign (GNU sed)
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 the line 5,10,15,20, etc.)
Gsed ' 0~5g ' only works for GNU SED
Sed ' n;n;n;n; G; ' # other SED

To selectively display a specific line:
--------

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

# Display the first line in 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 2 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 penultimate line in the file
Sed-e ' $! {h;d;} ' -E x # When there is only one row in the file, enter a blank line
Sed-e ' 1{$q;} '-E ' $! {h;d;} ' -E x # Displays the row 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 rows matching regular expressions are displayed (simulate "grep")
Sed-n '/regexp/p ' # method 1
Sed '/regexp/!d ' # method 2

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

# finds ' regexp ' and displays the previous line of matching rows, but does not display matching rows
Sed-n '/regexp/{g;1!p;}; H

# find ' regexp ' and display the next line of matching rows, but do not show matching rows
Sed-n '/regexp/{n;p;} '

# Displays the line that contains ' regexp ' and its front and back lines, plus the ' regexp ' before the first line
# line number of rows (similar to "GREP-A1-B1")
Sed-n-E '/regexp/{=;x;1!p;g;$! n;p;d;} ' -E H

# Displays lines containing "AAA", "BBB" or "CCC" (in any order)
Sed '/aaa/!d; /bbb/!d; The order of the/ccc/!d ' # string does not affect the result

# Displays lines containing "AAA", "BBB" and "CCC" (in Fixed order)
Sed '/aaa.*bbb.*ccc/!d '

# Displays lines containing "AAA" "BBB" or "CCC" (Analog "Egrep")
Sed-e '/aaa/b '-e '/bbb/b '-e '/ccc/b '-e D # most SED
Gsed '/aaa\| bbb\| Ccc/!d ' # is valid for GNU SED

# Displays the paragraph with "AAA" (separated by a blank line between paragraphs)
# hhsed v1.5 must be in ' X; ' Add "G;" and the next 3 scripts are like this
Sed-e '/./{h;$!d;} '-E ' x;/aaa/!d; '

# Displays paragraphs containing "AAA", "BBB" and "CCC" three strings (in any order)
Sed-e '/./{h;$!d;} '-E ' x;/aaa/!d;/bbb/!d;/ccc/!d '

# Show paragraphs containing "AAA", "BBB", "CCC", any of the three strings (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 ' # is only valid for GNU SED

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

# displays lines containing 65 characters
Sed-n '/^.\{65\}/!p ' # method 1, corresponding to the above script
Sed '/^.\{65\}/d ' # method 2, a simpler way

# Display part of text-from the start of 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 8th to 12th lines, with 8 and 12 rows)
Sed-n ' 8,12p ' # method 1
Sed ' 8,12!d ' # method 2

# Show Line 52nd
Sed-n ' 52p ' # method 1
Sed ' 52!d ' # method 2
Sed ' 52q;d ' # Method 3, more efficient when dealing with large files

# starting at line 3rd, every 7 lines are displayed once
Gsed-n ' 3~7p ' only works for GNU SED
Sed-n ' 3,${p;n;n;n;n;n;n} ' # other SED

# Displays text between two regular expressions (included)
Sed-n '/iowa/,/montana/p ' # case sensitive

To selectively delete a specific line:
--------

# Displays the entire document, except for the contents of two regular expressions
Sed '/iowa/,/montana/d '

# Delete adjacent duplicate rows in a file (simulate "Uniq")
# only the first row in the duplicate row is preserved, and the other rows are deleted
Sed ' $! N /^\ (. *\) \n\1$/! P D

# deletes duplicate rows in the file, regardless of whether they are adjacent or not. Note 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 first 10 lines in the file
Sed ' 1,10d '

# Delete the last row 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 works for GNU SED
Sed ' n;n;n;n;n;n;n;d; ' # other SED

# Delete the line that matches the style
Sed '/pattern/d ' deletes lines containing pattern. Of course pattern
# can be replaced with any valid regular expression

# Delete all empty rows in the file (with grep. "Same effect)
Sed '/^$/d ' # method 1
Sed '/./!d ' # method 2

# preserves only the first row of multiple contiguous empty rows. and delete the empty lines at the top and bottom of the file.
# (Simulate "cat-s")
Sed '/./,/^$/!d ' #方法1, delete the blank line at the top of the file, allow the tail to keep a blank line
Sed '/^$/n;/\n$/d ' #方法2, allow the top to keep a blank line, the tail does not leave blank line

# keep only the first two rows of multiple contiguous empty rows.
Sed '/^$/n;/\n$/n;//d '

# Delete all blank 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 '} ' # is valid for all SED
SED-E: A-e '/^\n*$/n;/\n$/ba ' # ditto, but only for gsed 3.02.* valid

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

Special applications:
--------

# removes the Nroff tag in the man page (man pages). Under the Unix System v or bash shell
# You may need to add the-e option with the ' echo ' command.
Sed "s/' echo \\\b '//g" # The outer brackets are required (Unix environment)
Sed ' s/.^h//g ' # in Bash or tcsh, press ctrl-v and then press Ctrl-h
The hexadecimal notation used by the sed ' s/.\x08//g ' # sed 1.5,gnu sed,ssed

# extract newsgroup or e-mail headers
Sed '/^$/q ' # Delete all content after the first line of blank lines

# Extract the body part of a newsgroup or e-mail
Sed ' 1,/^$/d ' # delete everything before the first line of empty rows

# extract "Subject" from the message header (title bar field) and remove the beginning of "Subject:"
Sed '/^subject: */!d; S///;q '

# Get reply address from mail header
Sed '/^reply-to:/q; /^from:/h; /./d;g;q '

# Get the email address. Based on the line headers generated by the previous script, further non-email
# The part of the address is shaved. (see previous script)
Sed ' s/* (. *)//; s/>.*//; s/.*[:<] *//'

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

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

# Remove most HTML tags (including cross-row labels)
SED-E: A-e ' s/<[^>]*>//g;/</n;//ba '

# will be divided into multiple volumes of uuencode file decoding. Remove file header information, leaving only the Uuencode encoding portion.
# files must be passed to sed in a specific order. The first version of the script below can be entered directly at the command line;
# The second version can be placed in a shell script with execute permissions. (A Dhesi by Rahul
# a script is modified. )
Sed '/^end/,/^begin/d ' file1 file2 ... filex | UUDecode # vers. 1
Sed '/^end/,/^begin/d ' "$@" | UUDecode # vers. 2

# The paragraphs in the file are sorted in alphabetical order. Paragraphs are separated by blank lines (one or more lines). GNU SED uses
# character ' \v ' to represent a vertical tab, which is used as a placeholder for line breaks--you can, of course,
# replace it with other characters 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 individually. TXT file, compressed after the original file is deleted and will be compressed. ZIP file
# is named the same as the original name (only the extension is different). (DOS Environment: "Dir/b"
# displays a filename without a path.
echo @echo off >zipup.bat
dir/b *.txt | Sed "s/^\ (. *\) \. Txt/pkzip-mo \1 \1.txt/">>zipup.bat


Use sed:sed to accept one or more edit commands, and then apply the commands sequentially after each read.
When the first line of input is read, SED applies all the commands to it and then outputs the results. And then read into the second
Line input, apply all commands to it ... and repeat the process. In the previous example, SED is set by the standard input
Standby (that is, the command interpreter, usually in the form of a pipe input) gets the input. At the command line, give one or more
File names as arguments, these files replace the standard input devices as input to sed. The output of SED will be
Send to standard output (monitor). So:

Cat FileName | Sed ' 10q ' # using pipe input
Sed ' 10q ' filename # same effect, but not using pipe input
Sed ' 10q ' filename > NewFile # Transfer output (redirect) to disk

For instructions on how to use the SED command, including how to use the script file (not from the command line)
Please refer to the second edition of Sed & awk, author Dale Dougherty and Arnold Robbins
(O ' reilly,1997;http://www.ora.com), "UNIX Text processing", author
Dale Dougherty and Tim O ' Reilly (Hayden books,1987) or Mike Arst.
Process-the name of the compressed package is "u-sedit2." ZIP "(found on many sites). To discover the SED
Potential, you must have an adequate understanding of the "regular expression". The regular expression of the data can be seen
"Mastering Regular Expressions" author Jeffrey Friedl (O ' Reilly 1997).
The manual page ("Man") provided by the UNIX system will also help (try these commands
"Man sed", "man regexp", or "man Ed" in the part about regular expressions, but
The information provided by the Handbook is more "abstract"-and it has always been criticized. However, it was not
is used to teach beginners how to use SED or regular expressions, but only for those who are familiar with these tools.
provides some textual references.

Parentheses syntax: The preceding example uses single quotes (' ... ') rather than double quotes for the SED command.
("...") this is because SED is usually used on UNIX platforms. In single quotes, the Unix shell (command
Interpreter does not interpret and execute the dollar character ($) and the post quotation mark (' ... '). and in double quotes
The dollar character is expanded to the value of a variable or parameter, and the command in the quotation marks is executed and replaced with the result of the output
The contents of the post quotation marks. In the case of "CSH" and its derived shell, an exclamation point (!) needs to be used before the
Face plus escape backslash (like this: \!) to ensure that the example used above is working correctly
(including the use of single quotes). The DOS version of SED uses double quotes ("...") instead of
Quotes to circle the command.

' \ t ' usage: To keep this article simple, we use ' \ t ' in the script to represent a tabulation
Character. But now most versions of sed don't recognize ' \ t ' shorthand, so when you're on the command line
When a script enters a tab, you should simply press the TAB key to enter the tab character instead of the ' \ T '. The following works
Software supports ' \ t ' as a regular expression character to represent tabs: awk, Perl, hhsed,
Sedmod and GNU sed v3.02.80.

Different versions of SED: the sed between different versions is somewhat different, and it can be imagined that they are syntactically
There will be differences. Specifically, most of them do not support the use of a label (: name) or a minute in the middle of an edit command
Command (B,T), unless it is placed at the end of those. In this document, we try to choose a high portability
Syntax so that most versions of SED users can use these scripts. But the GNU version of SED allows the
With 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 can make the order more compact:

Sed '/aaa/b;/bbb/b;/ccc/b;d ' can even be written
Sed '/aaa\| bbb\| Ccc/b;d '

Also, please note that although many versions of SED accept such "/one/s/re1/re2/" with empty before ' s '
Lattice commands, but some of these versions do not accept such commands: "/one/! S/re1/re2/". Then
Just take the middle of the space to remove the line.

Speed optimization: When for some reason (such as a large input file, processor or hard drive slow) need to improve
When the command executes speed, consider adding an address expression to the substitution command ("s/.../.../") before you can
Improve 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 # abbreviated form

You can use "Q" in your script when you want to display only the previous section of the file or if you need to delete the following content
Command (Exit command). This can save a lot of time when working with large files. So:

Sed-n ' 45,50p ' filename # shows line 45th to 50th
Sed-n ' 51q;45,50p ' filename # like, but much faster

If you have other Single-line scripts you would like to share or you have found the error in this document, please generate electricity
Sub-mail to the author of this document (Eric pement). Please remember to provide the SED version you are using in the mail,
The operating system that the SED runs and the appropriate description of the problem. The Single-line script referred to in this article refers to the length of the command line
65-character or 65-below sed script (1). The various scripts in this document are listed in the following
Written or provided by:

Al Aab # establishes a "seders" mailing list
Edgar Allen # in many ways
Yiorgos Adamopoulos # Many aspects
Dale Dougherty # The author of Sed & awk
Carlos Duarte # "Do it with sed" author
Eric Pement # Author of this document
The author of Ken Pizzini # GNU sed v3.02
S.G. Ravenhall # Go to HTML tag script
Greg Ubben # has many contributions and offers a lot of help.
-------------------------------------------------------------------------

1: In most cases, the SED script can be written as a single line (via the '-e ' option and '; '), no matter how long.
)--As long as the command interpreter supports it, so the single line script here can be written in one line and the length
Limited. Because the meaning of these single-line scripts is not that they appear in a single line. But to allow users to
It makes sense to easily use these compact scripts on the command line.

Related Article

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.