Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The usage of sed under Unix (2)

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

1. Common methods

# add a blank line after each line

Sed G

# remove all the original blank lines and add a blank line after each line.

In this way, there will be only one blank line after each line in the output text.

Sed'/ ^ $/ dscape G'

# add two blank lines after each line

Sed'G'There G'

# remove all blank lines generated by the first script (that is, delete all even lines)

Sed'ntudd'

# insert a blank line before matching the line of the style "regex"

Sed'/ regex/ {xtertpterx;}'

# insert a blank line after matching the line of the style "regex"

Sed'/ regex/G'

# insert a blank line before and after matching the line of style "regex"

Sed'/ regex/ {x _ Tinct _ x _ TX _ G;}'

=

2. Serial number

# number each line in the file (simple left alignment). Instead of spaces, "tab" (see the description of the use of'\ t'at the end of this article) is used to align edges.

Sed = filename | sed'Numbent /\ n /\ t _

# number all lines in the file (the line number is on the left and the right end of the text is aligned).

Sed = filename | sed'n; s / ^ / /; s / *\ (.\ {6,\}\)\ n /\ 1 /'

# numbers all lines in the file, but only line numbers that are not blank are displayed.

Sed'/. / = 'filename | sed' /. / N; s /\ n / /'

# count rows (simulate "wc-l")

Sed-n'$='

=

3. Text conversion and substitution:

# Unix environment: convert the new line character (CR/LF) of DOS to Unix format.

Sed's _

Sed's / ^ M $/ /'# in bash/tcsh, change from press Ctrl-M to press Ctrl-V

Sed's /\ x0D hammer ssed'# ssed, gsed 3.02.80, and later

# Unix environment: convert the new line character (LF) of Unix to DOS format.

Sed "sswab commands / `echo-e\ r` /" # commands used under ksh

Sed's echo commands'"/ `command\ r` /" # commands used under bash

Sed "splanchpin commands / `echo\ r` /" # commands used under zsh

Sed's gsed gsed 3.02.80 and later

# DOS environment: convert Unix new line character (LF) to DOS format.

Sed "splanchpin" / "# 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 recognize the UnxUtils version of sed, you can use its unique "--text" option. You can use the help option ("--help") to see if there is a "--text" item to determine if you are using a UnxUtils version. Other DOS versions of sed cannot do this conversion. But you can use "tr" to achieve this transformation.

Sed "s /\ rbind /" infile > outfile # UnxUtils sed v4.0.7 or later

Tr-d\ r outfile # GNU tr 1.22 or later

# remove the leading "white space character" (space, tab) of each line to align it to the left

Sed's / ^ [\ t] * /'# see the description of the usage of'\ t'at the end of this article

# remove the "white space characters" (spaces, tabs) trailing each line

Sed's / [\ t] * $/'# see the description of the usage of'\ t'at the end of this article

# remove leading and trailing white space characters from each line

Sed's / ^ [\ t] * / /; s / [\ t] * $/ /'

# insert 5 spaces at the beginning of each line (move the full text 5 characters to the right)

Sed's / ^ / /'

# align all text to the right with a width of 79 characters

Sed-e: a-e's / ^. 1 ta' 78\} $/ & /; 78 characters plus the last space

# centers all text with a width of 79 characters. In method 1, spaces are filled in before and after each line to center the text. In method 2, only the spaces in front of the text are filled in during the process of living in the Chinese text, and eventually half of these spaces will be deleted. In addition, there are no blanks at the end of each line.

Sed-e: a-e's / ^.\ {1 ta' 77\} $/ & /; method 1

Sed-e: a-e's / ^.\ {1 ta' 77\} $/ & /; ta'-e's /\ (*\)\ 1 /\ 1ram'# method 2

# look for the string "foo" in each line and replace the found "foo" with "bar"

Sed's Placement FooCompact'# replaces only the first "foo" string in each line

Sed's Placement FooAccord 4'# replaces only the fourth "foo" string in each line

Sed's foo foo changes all the words "bar" in each line to "bar".

Sed's /\ (. *\) foo\ (. * foo\) /\ 1bar\ 2Universe'# replace the penultimate "foo"

Sed's /\ (. *\) foo/\ 1barmap'# replaces 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'/ bazqqqr / bazqqr / bazqqqr / bazqqqr / bazqqr / bazqqr / bazqqqr / bazqqr / bazqqqr /

# whether it is "scarlet", "ruby" or "puce", it will be replaced by "red"

Sed's scarlet scarlet, redbank, red, scarlet, scarlet, red, scarlet, red,

Gsed 's/scarlet\ | ruby\ | puce/red/g' # is only valid for GNU sed

# invert all lines, the first line becomes the last line, and so on (simulate "tac"). For some reason, HHsed v1.5 removes blank lines from the file when using the following command.

Sed'1 method 1

Sed-n'1 method 2

# arrange the characters in the line in reverse order, and the first word becomes the last word, …... (simulate "rev")

Sed'/\ nCompact Gumbai /\ (. \)\ (. *\ n\) / &\ 2\ 1Universe Dincus Dincus.Unix'

# concatenate every two lines into one line (similar to "paste")

Sed'$! n * * /\ n / /

# if the current line ends with a backslash "\", the next line is moved to the end of the current line, and the backslash at the end of the original line is removed

Sed-e: a-e'/\\ $/ N; s /\ nUniverse; ta'

# if the current line begins with an equal sign, add 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 * *

# add a comma separator to the numeric string and change "1234567" to "1234567"

Gsed': a GNU sed /\ B [0-9]\ {3\}\ > /, & /; ta' # GNU sed

Sed-e: a-e's /\ (. * [0-9]\)\ ([0-9]\ {3\}\) /\ 1,\ 2 other sed adds a comma separator (GNU sed) to values with decimal points and minus signs

Gsed-r': a Tacter / (^ | [^ 0-9.]) ([0-9] +) ([0-9] {3}) /\ 1\ 2,\ 3 A blank line is added after every 5 lines (in 5, 10, 15, 20, etc.)

Gsed '0room5G' # is only valid for GNU sed

Sed'ntternternternternternternternternternternternterntermittingnternterntermittingnternternternternternternternternternternTanternternternTen sed'# other sed

=

4. Selectively display specific lines:

# display 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

# display the last 10 lines in the file (simulate "tail")

Sed-e: a-e'$qtternternternt11authorDidentba'

# display the last 2 lines in the file (simulate the "tail-2" command)

Sed'$! n *

# 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'$! {hash d;}'- e x # enter a blank line when there is only one line in the file

Sed-e'1'-e'$! {hash d;}'- e x # displays a line when there is only one line in the file

Sed-e'1'-e'$! {hash d;}'- e x # does not output when there is only one line in the file

# display only lines that match regular expressions (simulate "grep")

Sed-n'/ regexp/p' # method 1

Sed'/ regexppqqqqqund' # method 2

# display only lines that "do not" match regular expressions (simulate "grep-v")

Method 1 of sed-n'/ regexpqqqqipp'#, which corresponds to the previous command

Sed'/ regexp/d' # method 2, similar syntax

# find "regexp" and display the previous line of the matching line, but not the matching line

Sed-n'/ regexp/ {GTX 1roomp;}; h'

# find "regexp" and display the next line of the matching line, but not the matching line

Sed-n'/ regexp/ {ntterp;}'

# display the line containing "regexp" and its front and back lines, and add "regexp" before the first line

# Line number on the line (similar to "grep-A1-B1")

Sed-n-e'/ regexp/ {=; x-t1 / n-e'- e h

# display lines containing "AAA", "BBB" or "CCC" (in any order)

The order of the string sed'/ AAAqUD; / BBUniDUD; / CCCUniqqd# does not affect the result.

# display lines containing "AAA", "BBB" and "CCC" (in fixed order)

Sed'/ AAA.BBB.CCC _

# display lines containing "AAA", "BBB" or "CCC" (simulate "egrep")

Sed-e'/ AAA/b'-e'/ BBB/b'-e'/ CCC/b'-ed # most sed

Gsed'/ AAA\ | BBB\ | CCC _ GNU sed is valid for CCC _

# displays paragraphs containing "AAA" (separated by blank lines), HHsed v1.5 must add "G;" after "x;", as is the case for the next three scripts

Sed-e'/. / {Htinct committed d;}'- e'x incl.

# display paragraphs containing the strings "AAA", "BBB" and "CCC" (in any order)

Sed-e'/. / {Hincadron;}'- e'x *

# display paragraphs containing any of the strings "AAA", "BBB" or "CCC" (in any order)

Sed-e'/ BBB/b'-e'/ {CCC/b'-ed; CCC/b'-e'/-e-e'/ AAA-ed

Gsed'/. / {HutchindAAA;}; x TincedAAA\ | BBB\ | CCC/b;d' # is only valid for GNU sed

# display lines with 65 or more characters

Sed-n'/ ^.\ {65\} / p'

# display lines with less than 65 characters

Sed-n'/ ^.\ {65\} /! P'# method 1, corresponding to the above script

Sed'/ ^.\ {65\} / d'# method 2, a simpler method

# display part of the text-from the line containing the regular expression to the end of the last line

Sed-n'/ regexp/,$p'

# display partial text-specify line number range (from lines 8 to 12, including lines 8 and 12)

Sed-n'8 12p'# method 1

Sed '8JI 120d' # method 2

# Show line 52

Sed-n '52p' # method 1

Sed '52encrypd' # method 2

Method 3 of sed '52qposition d' # is more efficient when dealing with large files

# starting from line 3, displayed every 7 lines

Gsed-n '3room7p' # is only valid for GNU sed

Sed-n'3 sed ${ptinct / ntra / other sed

# display the text between two regular expressions (included)

Sed-n'/ Iowa/,/Montana/p' # case sensitive

=

5. Selectively delete specific lines

# display the entire document, except for the content between two regular expressions

Sed'/ Iowa/,/Montana/d'

# Delete adjacent duplicate lines in the file (simulate "uniq"), keep only the first line in the duplicate lines, and delete other lines

Sed'$! n; / ^\ (. *\)\ n\ 1 $/! P; D'

# Delete duplicate lines in the file, whether adjacent or not. Note the cache size that hold space can support, or use GNU sed.

Sed-n'G; s /\ nAccording to a raceme; / ^\ ([- ~] *\ n\). *\ n\ 1Universe d; s /\ nUniple; h; P'

# Delete all lines except duplicate lines (simulate "uniq-d")

Sed'$! n; s / ^\ (. *\)\ n\ 1 $/\ 1Universe; t; D'

# Delete the first 10 lines in the file

Sed'1m 10d'

# Delete the last line in the file

Sed'$d'

# Delete the last two lines in the file

Sed'N / O / P / P / D / P / P / D / P / D

# Delete the last 10 lines in the file

Sed-e: a-e'$d Tinct Nitt2 million 10ba'-e'Pten D' # method 1

Sed-n-e: a-e'1 recorder 10! {P _ tinct _ D;}; N _ tansba' # method 2

# Delete a multiple line of 8

Gsed '0room8d' # is only valid for GNU sed

Sed 'nterntermit.ntermit.nter.There are some other sed

# Delete lines that match the style

Sed'/ pattern/d' # deletes the line with pattern. Of course, pattern can be converted into any valid regular expression.

# Delete all blank lines in the file (same as "grep'.")

Sed'/ ^ $/ d'# method 1

Sed'/. /! d'# method 2

# keep only the first line of multiple adjacent blank lines. And delete the blank lines at the top and tail of the file. (simulate "cat-s")

Sed'/. /, / ^ $/! d'# method 1, delete the blank line at the top of the file and allow the trailer to leave a blank line

Method 2 of sed'/ ^ $/ Nbot /\ nblank planner D' #, which allows a blank line at the top and no blank line at the tail

# retains only the first two lines of multiple adjacent blank lines.

Sed'/ ^ $/ Niptic /\ nUniverse Universe / NitterUniverse D'

# Delete all blank lines at the top of the file

Sed'/. /, $! D'

# Delete all blank lines at the end of the file

Sed-e: a-e'/ ^\ nvalid for all sed

Sed-e: a-e'/ ^\ nIt is valid only for gsed 3.02.* as above

# Delete the last line of each paragraph

Sed-n'/ ^ $/ {ptinct h;}; /. / {xscape Universe.Unip;}'

6. Special applications

# remove the nroff tag from the man page (man page). You may need to add the-e option when using the 'echo' command under Unix System V or bash shell.

The outer parentheses of sed "s / .`echo\ b` / / g" # are required (Unix environment)

Sed's / .^ H / / g'# in bash or tcsh, press Ctrl-V and then press Ctrl-H

The representation of the hexadecimal used by sed's sed,ssed.\ x08Accord GN'# sed 1.5.

# extract the headers of newsgroups or e-mail

Sed'/ ^ $/ Q'# everything after deleting the first blank line

# extract the body of a newsgroup or e-mail

Sed '1authoring / ^ $/ d' # delete everything before the first blank line

# extract "Subject" (title bar field) from the header and remove the word "Subject:" at the beginning

Sed'/ ^ Subject: * /! d

# get the reply address from the header

Sed'/ ^ Reply-To:/q; / ^ From:/h; /. / d Ting g Ten Q'

# get the email address. Further shave the part of the non-email address based on the line of headers generated by the previous script. (see the script above)

Sed's / * (. *) / /; s / >. * /; sUnip. * [: /'

# remove angle brackets and spaces at the beginning of each line (dereferencing)

Sed's / ^ > / /'

# remove most HTML tags (including cross-line tags)

Sed-e: a-e's /] * > / / GTX /

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report