November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Categories

November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

sed example

Text Interval:
——–

# Add a blank line after each line
sed G

# The original delete all blank lines and add a blank line after each line.
# So that each line of text output later and only a blank line.
sed ‘/ ^ $ / d; G’

# Add two blank lines after each line
sed ‘G; G’

# All blank lines generated by the first script to delete (ie remove all the even-numbered lines)
sed ‘n; d’

# Insert before matching pattern “regex” line a blank line
sed ‘/ regex / {x; p; x;}’

# In the matching pattern “regex” insert a blank row after row
sed ‘/ regex / G’

# Before matching pattern “regex” line and insert a blank line after each
sed ‘/ regex / {x; p; x; G;}’

Serial number:
——–

# For each line in the file number (simple left alignment). Here the use of “tabs”
# (Tab, see the end of this description of the ‘\ t’ of usage) instead of spaces to align the edges.
sed = filename | sed ‘N; s / \ n / \ t /’

# For all lines in the file number (line number in left, right-aligned).
sed = filename | sed ‘N; s / ^ / /; s / * \ (\ {6, \} \.) \ n / \ 1 /’

# All lines of the file number, but displays only non-blank line number.
sed ‘/./=’ filename | sed ‘/./N; s / \ n / /’

# Calculate the number of (simulated “wc -l”) line
sed -n ‘$ =’

Text conversion and substitution:
——–

# Unix environment: convert DOS newlines (CR / LF) to Unix format.
sed ‘s /.$//’ # assumes that all lines with CR / LF end
sed ‘s / ^ M $ //’ # in bash / tcsh, and will Ctrl-M to press Ctrl-V
sed ‘s / \ x0D $ //’ # ssed, gsed 3.02.80, and later

# Unix environment: convert Unix newlines (LF) to DOS format.
sed “s / $ /` echo -e \\\ r` / “# used in the ksh command
sed ‘s / $’ “/` echo \\\ r` / “# used in the bash command
sed “s / $ /` echo \\\ r` / “# used in zsh command
sed ‘s / $ / \ r /’ # gsed and later 3.02.80

# DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format.
sed “s / $ //” # 1
sed -n p # Method 2

# DOS ENVIRONMENT: convert DOS newlines (CR / LF) to Unix format.
# The following script only UnxUtils sed 4.0.7 and later effective. To identify UnxUtils version
# Sed through its unique “–text” option. You can use the help option (“–help”) see
# Where the presence of a “–text” items used in order to determine whether it is UnxUtils version. Other DOS
# Sed version of this conversion is not possible. But you can use “tr” to achieve this transformation.
sed “s / \ r //” infile> outfile # UnxUtils sed v4.0.7 or later
tr -d \ r outfile # GNU tr version 1.22 or higher

# Each line leading “whitespace” (spaces, tabs) Delete
# Make Zhizuo aligned
sed ‘s / ^ [\ t] * //’ # see note on ‘\ t’ usage description

# Each line trailing “whitespace” (spaces, tabs) Delete
sed ‘s / [\ t] * $ //’ # see note on ‘\ t’ usage description

# Whitespace from each line, remove leading and trailing
sed ‘s / ^ [\ t] * //; s / [\ t] * $ //’

# Insert five spaces at the beginning of each line (so that the full text moves to the right position 5 characters)
sed ‘s / ^ / /’

# To 79 characters for the width, all the text right-aligned
sed -e: a -e ‘s / ^ \ {1,78 \} $ / & /; ta.’ # 78 characters plus the last one space

# To 79 characters for the width, so that all the text centered. In method 1, in order to allow the front of each line of text is centered
# Head and behind are filled with blanks. In method 2, in front of the text is filled only during the middle of the text in
# Spaces, and eventually they will have half the space will be deleted. Also behind each row is not filled spaces.
sed -e: a -e ‘s / ^ \ {1,77 \} $ / & /; ta.’ # 1
sed -e: a -e ‘s / ^ \ {1,77 \} $ / & /; ta.’ -e ‘s / \ (* \) \ 1 / \ 1 /’ # method 2

# Find the string on each line “foo”, “foo” and find the replacement for “bar”
sed ‘s / foo / bar /’ # replaces only the first in each line “foo” string
sed ‘s / foo / bar / 4’ # replaces only every fourth row “foo” string
sed ‘s / foo / bar / g’ # each line all “foo” are replaced by “bar”
sed ‘s / \ (. * \) foo \ (. * foo \) / \ 1bar \ 2 /’ # replaces the penultimate “foo”
sed ‘s / \ (. * \) foo / \ 1bar /’ # replace the last one “foo”

Case # only line string “baz” appears in the “foo” replaced “bar”
sed ‘/ baz / s / foo / bar / g’

Under # will replace “foo” replaced “bar”, and only the line string “baz” does not appear in the case
sed ‘/ baz /! s / foo / bar / g’

# Whether “scarlet” “ruby” or “puce”, shall be replaced by “red”
sed ‘s / scarlet / red / g; s / ruby ??/ red / g; s / puce / red / g’ # sed are valid for most of
gsed ‘s / scarlet \ | ruby ??\ | puce / red / g’ # GNU sed only valid

# Upside down all the rows, the first row as the last row, and so on (analog “tac”).
# For some reason, use the following command HHsed v1.5 will delete blank lines in the file
sed ‘! 1 G; h;! $ d’ # method 1
sed -n ‘! 1 G; h; $ p’ # method 2

# The line of characters in reverse order, the first word to be the last word, …… (analog “rev”)
sed ‘/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//’

# The two lines each connected in a row (like “paste”)
sed ‘! $ N; s / \ n / /’

# If the current line with a backslash “\” end, then the next line and to the current end of the line
# And remove the original trailing backslash
sed -e: a -e ‘/ \\ $ / N; s / \\\ n //; ta’

# If the current line begins with an equal sign, the current line and onto the end of a line
# And a single space instead of the original first line of “=”
sed -e: a -e ‘$ N; s / \ n = / /; ta!’ -e ‘P; D’

# Add a comma delimited string of numbers, the “1234567” to “1,234,567”
gsed ‘: a; s / \ B [0-9] \ {3 \} \> /, & /; ta’ # GNU sed
sed -e: a -e ‘(. * [0-9] \) s / \ \ ([0-9] \ {3 \} \) / \ 1, \ 2 /; ta’ # other sed

# Is a number with a decimal point and negative sign of increased comma delimited (GNU sed)
gsed -r ‘: a; s / (^ | [^ 0-9.]) ([0-9] +) ([0-9] {3}) / \ 1 \ 2, \ 3 / g; ta ‘

# Add a blank line (5,10,15,20 in the first, and so increase the line after a blank line) after each line 5
gsed ‘0 ~ 5G’ # GNU sed only valid
sed ‘n; n; n; n; G;’ # other sed

To selectively display a particular row:
——–

# Display the file first 10 lines (analog “head” of behavior)
sed 10q

# Display the file in the first row (analog “head -1” command)
sed q

# Display the last 10 lines of a file (Analog “tail”)
sed -e: a -e ‘$ q; N; 11, $ D; ba’

# Display file last two lines (analog “tail -2” command)
sed ‘! $ N;! $ D’

# Display file last line (analog “tail -1”)
sed ‘$! d’ # method 1
sed -n ‘$ p’ # method 2

# Display file penultimate line
sed -e ‘$ {h; d;}!’ -ex # When the file only when one line, enter a blank line
sed -e ‘1 {$ q;}’ -e ‘$! {h; d;}’ -ex # When the file only when the line displays the line
sed -e ‘1 {$ d;}’ -e ‘$ {h; d;}!’ -ex # When the file only when the line is not output

# Show only rows matching regular expression (analog “grep”)
sed -n ‘/ regexp / p’ # method 1
sed ‘/ regexp /! d’ # method 2

# Only shows “no” regular expression matching lines (analog “grep -v”)
sed -n ‘/ regexp /! p’ # method 1, corresponds with the previous command
sed ‘/ regexp / d’ # method 2, similar syntax

# Find “regexp” and matching rows displayed on a single line, but does not display the matching lines
sed -n ‘/ regexp / {g; 1 p;!}; h’

# Find “regexp” the next line and matching rows displayed, but does not display the matching lines
sed -n ‘/ regexp / {n; p;}’

# Display a “regexp” line before and after the line, and before the first row plus “regexp” the
# Line line number (similar to “grep -A1 -B1”)
sed -n -e ‘/ regexp / {=; x; 1 p;! g; $ N;! p; D;}’ -e h

# Display a “AAA”, “BBB” or “CCC” of the row (in any order)
sed ‘/ AAA / d;!! / BBB / d;! / CCC / d’ # string does not affect the order of the results

# Display a “AAA”, “BBB” and “CCC” line (fixed order)
sed ‘/AAA.*BBB.*CCC/!d’

# Display a “AAA” “BBB” or “CCC” line (analog “egrep”)
sed -e ‘/ AAA / b’ -e ‘/ BBB / b’ -e ‘/ CCC / b’ -ed # majority sed
gsed ‘/ AAA \ | BBB \ |! CCC / d’ # GNU sed on effective

Paragraph (separated by a blank line between paragraphs) # display a “AAA” of
# HHsed v1.5 must be in the “x;” after joining the “G;”, the next three scripts are so
sed -e ‘/./{H;$!d;}’ -e ‘x; / AAA / d;!’

Paragraph (in any order) # display a “AAA” “BBB” and “CCC” three strings
sed -e ‘/./{H;$!d;}’ -e ‘x; / AAA / d;! / BBB / d;!! / CCC / d’

# Display a “AAA”, “BBB”, “CCC” to any one of the three passages of the string (in any order)
sed -e ‘/./{H;$!d;}’ -e ‘x; / AAA / b’ -e ‘/ BBB / b’ -e ‘/ CCC / b’ -ed
gsed ‘/./{H;$!d;};x;/AAA\|BBB\|CCC/b;d’ # GNU sed only valid

Line # Display contains 65 or more characters
sed -n ‘/^.\{65\}/p’

# Display line contains 65 characters or less
sed -n ‘/^.\{65\}/!p’ # method 1, corresponds with the above script
sed ‘/^.\{65\}/d’ # method 2, a little more simple way

# Display some of the text – from the row that contains the regular expression to the last line ends
sed -n ‘/ regexp /, $ p’

# Display some of the text – the specified line number range (from 8 to 12 lines, with 8 and 12 lines)
sed -n ‘8,12p’ # 1
sed ‘8,12! d’ # method 2

# Display line 52
sed -n ’52p’ # method 1
sed ’52! d ‘# method 2
sed ’52q; d’ # method 3, more efficient when dealing with large files

# From the beginning of the third line, the line is displayed once every 7
gsed -n ‘3 ~ 7p’ # GNU sed only valid
sed -n ‘3, $ {p; n; n; n; n; n; n;}’ # other sed

# Display between two regular text expression (including)
sed -n ‘/ Iowa /, / Montana / p’ # case sensitive manner

Selectively remove specific lines:
——–

# Display throughout the document, in addition to the content between two regular expressions
sed ‘/ Iowa /, / Montana / d’

# Delete duplicate files in adjacent rows (analog “uniq”)
# Retain only duplicate rows in the first row, the other rows deleted
sed ‘$ N;! /^\(.*\)\n\1$/!P; D’

# Delete duplicate lines in the file, regardless of whether the neighbor. Note hold space can support cache
# Size, or use GNU sed.
sed -n ‘G; s / \ n / && /;. / ^ \ ([- ~] * \ n \) * \ n \ 1 / d; s / \ n //; h; P’

# Delete all lines (analog “uniq -d”) except duplicate rows
sed ‘! $ N; s / ^ \ \ n \ 1 $ / \ 1 / (* \.); t; D’

# Delete files in the beginning of the 10 lines
sed ‘1,10d’

# Delete the last line in the file
sed ‘$ d’

# Delete files in the last two lines
sed ‘N; $ P;! $ D;! $ d’

# Delete files in the last 10 lines
sed -e: a -e ‘$ d; N; 2,10ba’ -e ‘P; D’ # 1
sed -n -e: a -e ‘1,10 {P; N; D;}; N; ba!’ # method 2

# Delete multiple rows 8
gsed ‘0 ~ 8d’ # GNU sed only valid
sed ‘n; n; n; n; n; n; n; d;’ # other sed

# Delete the rows matching style
sed ‘/ pattern / d’ # delete the row containing the pattern is. Of course pattern
# Can be replaced with any valid regular expression

# Delete files all blank lines (with “grep ‘.'” The same effect)
sed ‘/ ^ $ / d’ # method 1
sed ‘/./!d’ # method 2

# Keep only the first line of multiple adjacent blank lines. And delete files at the top and tail of blank lines.
# (Analog “cat -s”)
sed ‘/./,/^$/!d’ # method 1, the empty rows to delete files at the top, allowing the tail to keep a blank line
sed ‘/ ^ $ / N; / \ n $ / D’ # method 2, allows the top to retain a blank line, the tail does not leave blank lines

# Only the first two lines keep multiple adjacent blank lines.
sed ‘/ ^ $ / N; / \ n $ / N; // D’

# Delete all blank lines at top of file
sed ‘/./,$!d’

# Delete all files trailing blank lines
sed -e: a -e ‘/ ^ \ n * $ / {$ d; N; ba’ -e ‘}’ # sed valid for all
sed -e: a -e ‘/ ^ \ n * $ / N; / \ n $ / ba’ # above, but only for gsed 3.02 * valid.

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

Special applications:
——–

# Remove manual page (man page) in nroff mark. In the Unix System V or bash shell so
May need to add -e option # use ‘echo’ command.
sed “s / .`echo \\\ b` // g” # outer pair of parentheses is required (Unix environment)
sed ‘s /.^ H // g’ # in bash or tcsh, press Ctrl-V and then press Ctrl-H
sed ‘s /. \ x08 // g’ # sed 1.5, GNU sed, ssed uses hexadecimal representation

# Extract newsgroup or e-mail message header
sed ‘/ ^ $ / q’ # delete all the contents of the first row after row of empty

# Extract the body of the news group or e-mail the
sed ‘1, / ^ $ / d’ # delete all content blank lines before the first line of

# Extract the “Subject” (the title bar field) from the message header, and remove the beginning of the “Subject:” words
sed ‘! / ^ Subject: * / d; s ///; q’

# Get the return address from the message header
sed ‘/ ^ Reply-To: / q; / ^ From: / h; /./d;g;q’

# Get the e-mail address. On the basis of that line messages generated by a script on the head of a further non-email
Part # address shave. (See a script)
sed ‘s / * //; s />.*//; s /.* (*.) [: <] * //' # At the beginning of each line with a sharp brackets and spaces (reference information) sed 's / ^ /> /’

# At the beginning of each line angle brackets and spaces removed (dereference)
sed ‘s / ^> //’

# Remove most HTML tags (including interbank label)
sed -e: a -e ‘s / <[^>] *> // g; / zipup.bat
dir / b * .txt | sed “s / ^ \ \ TXT / pkzip -mo \ 1 \ 1.TXT / (* \.).” >> zipup.bat

Use SED: Sed takes one or more editing commands, and each line of input in order to apply these commands.
When reading the first line of the input after, sed their applications all command, and outputs the result. Then read the second
Line input, their applications all command …… and repeat the process. Sed on an example from the standard input device
Equipment (ie, command interpreter, usually in the form of piped) to obtain input. Or give a command line
When a file name as an argument, the files become sed to replace the standard input device input. sed the output will be
Sent to standard output (monitor). As such:

cat filename | sed ’10q’ # use piped input
sed ’10q’ filename # same effect, but does not use the pipe input
sed ’10q’ filename> newfile # The output transfer (redirect) to disk

To understand the instructions for use sed command, including how to use via a script file (rather than from the command line) these life
Order, please refer to “sed & awk” second edition, 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 write to teach Mike Arst
Cheng – the name of the archive is the “U-SEDIT2.ZIP” (on many sites can find it). To explore sed
Potential, it must be on the “regular expression” sufficient understanding. Information on regular expressions can be seen
“Mastering Regular Expressions” author Jeffrey Friedl (O’reilly 1997).
Unix systems provide manual page (“man”) would also be helpful (try these commands
“Man sed”, “man regexp”, or see “man ed” in the section on regular expressions), but
Comparative information booklet provided “abstract” – this is it’s always been criticized. However, it should not have
Is used to teach beginners how to use regular expressions sed or materials, but only for those who are familiar with these tools
Some texts provide references.

Bracket syntax: The preceding examples sed command basically use single quotation marks (‘…’) instead of double quotes
(“…”) This is because sed is typically used on Unix platforms. Under the single quotes, Unix’s shell (command
Interpreter) does the dollar sign ($) and post quotes (`…`) be interpreted and executed. And in double quotes under
Dollar sign will be expanded to the value of a variable or parameter, after the command is executed in quotation marks and replace output results
After the quotes content. Need to use an exclamation point when its front in “csh” shell and derivatives of the (!)
Escaped with a backslash plus side (like this:! \) To ensure that the above example can be used in normal operation
(Including the use of single quotation marks under circumstances). DOS version of Sed then always use double quotes (“…”) rather than
Quotes to enclose command.

‘\ t’ Usage: For clarity in documentation, we use the script ‘\ t’ to indicate a tab
Character. However, most versions of sed do not recognize the ‘\ t’ shorthand, so when the command line is
When the script Input tabs, you should press the TAB key to enter tabs instead of typing ‘\ t’. The following work
With software support ‘\ t’ character as a regular expression to represent tabs: awk, perl, HHsed,
sedmod and GNU sed v3.02.80.

Different versions of the SED: sed between different versions will be some differences, imagine in syntax between them
Vary. Specifically, the majority of them do not support the use of labels in the edit command (: name) or sub-
Branch commands (b, t), except those at the end. This document we try to use a high portability
Syntax, so most versions of sed users can use these scripts. However, the version of GNU sed allow
With a more concise syntax. Imagine the feeling when readers see a very long time the command:

sed -e ‘/ AAA / b’ -e ‘/ BBB / b’ -e ‘/ CCC / b’ -e d

The good news is GNU sed command enables more compact:

sed ‘/ AAA / b; / BBB / b; / CCC / b; d’ # even be written
sed ‘/ AAA \ | BBB \ | CCC / b; d’

Also, please note that although many versions of sed accept as “/ one / s / RE1 / RE2 /” this before ‘s’ with an empty
Georgia’s commands, but some of these versions do not accept this command: “/ one / s / RE1 / RE2 /!”. Then
Just need to get rid of the middle of the space on the line.

Speed ??optimization: When for some reason (for example, a large input file, the processor is too slow, or hard disk) the need to improve
When the command execution speed, consider replacing the command (“s /…/…/”) preceded by address expressions
Increase speed. For example:

sed ‘s / foo / bar / g’ filename # standard replace command
sed ‘/ foo / s / foo / bar / g’ filename # Faster
sed ‘/ foo / s // bar / g’ filename # shorthand

When you need to display only the front part of the file or delete the contents of the back when needed, you can use the “q” in the script
Command (Exit command). When dealing with large files, this will save a lot of time. As such:

sed -n ’45, 50p ‘filename # Showing 45-50 OK
sed -n ’51q; 45,50p’ filename # same, but faster

If you have another one-line script would like to share with you or you find in this document the wrong place, please power
E-mail to the author of this document (Eric Pement). Please remember to provide the message sed version you are using,
Operating system and proper description of the sed running of the problem. One-line script referred to herein refers to the command line length
Degrees in 65 characters or less 65 Annotation [1] sed script. Various scripts in this document is made as listed below
Who wrote or provide:

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>