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

Collation of sed usage

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

Share

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

Most of the following content comes from the network, the source can not be verified, please forgive me.

1. How do I delete a script in html text?

Temp$ sed'/ / d' cp-advancec > new

Displays the content between two regular expressions:

Sed-n'/ pattern1/,/pattern2/p' file

Displays the entire document, except for the content between two regular expressions:

Sed'/ pattern1/,/pattern2/d' file

two。 How do I delete the line of src contained in the html text? (the browser will request the content of the src, and I don't want it to request it)

Temp$ sed'/ src= "/ d'new > 2new

Temp$ sed'/ / d' 2new > new

Delete matching lines:

Sed'/ pattern/d' file

3. How do I replace the contents of a file and write back to the source file?

Wrong practice: sed's file file > file

This will cause the file to be emptied.

Sed-I: you can write the replaced file directly back to the source file, which is very practical.

The correct way to write it is: sed-I's file, n1max, n2max.

If you want to keep the original file,

Sed's file1 'file1 > file2

The following is from http://sed.sourceforge.net/sed1line_zh-CN.html:

Text interval:

-

# 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;}'

# number each line in the file (simple left alignment). Tabs are used here.

# (tab, see the description of the use of'\ t'at the end of this article) instead of spaces 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'$='

Text conversion and substitution:

-

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

Sed's Universe.Universe Universe'

# suppose all lines end with CR/LF

Sed's / ^ M $/ /'

# in bash/tcsh, change from Ctrl-M to Ctrl-V

Sed's /\ x0Dumbo Universe'

# ssed, gsed 3.02.80, and later

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

Sed "spockets / `echo-e\ r` /"

# commands used under ksh

Sed's Universe'"/ `echo\ r` /"

# commands used under bash

Sed "spockets" / `echo\ r` / "

# commands used under zsh

Sed's _

# gsed 3.02.80 and later

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

Sed "sswab" / "

# 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

# whether there is a "--text" entry to determine whether the UnxUtils version is being used. Other DOS

The # version 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 characters" (spaces, tabs) of each line

# 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, to center the text in front of each line

# the head and the back are filled with blanks. In method 2, only the front of the text is filled in in the process of living in the Chinese text.

# spaces, 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 / ^.\ {1cm77\} $/ & /; ta'

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

# replace only the first "foo" string in each line

Sed's Accord FooCompact 4'

# replace only the fourth "foo" string in each line

Sed's Accord FooCompact g'

# replace all "foo" in each line with "bar"

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

Sed's /\ (. *\) foo/\ 1barmap'

# 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'/ 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, red, red, scarlet, red, scarlet, red, red, scarlet, scarlet, red, red, scarlet, red, scarlet, red, red, and red.

Gsed 's/scarlet\ | ruby\ | puce/red/g'

# it works for most sed

# valid for GNU sed only

# invert all lines, the first line becomes the last line, and so on (simulate "tac").

# for some reason, HHsed v1.5 will delete the blank lines in the file when using the following command

Sed'1g / h / g / h / g / g / h / g / h / h / g / h / g / h / h / g / g / h / h / g / g / h / h

# method 1

Sed-n'1g / n'1g / h / s / p'

# method 2

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

Sed'/\ nUniverse GmbH /\ (.\)\ (. *\ n\) / &\ 2\ 1Greater Dexter Dexter.

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

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

# if the current line ends with a backslash "\", the next line ends at the end of the current line

# and remove the backslash at the end of the original line

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 ta' /\ B [0-9]\ {3\}\ > /, & /;

# GNU sed

Sed-e: a-e's /\ (. * [0-9]\)\ ([0-9]\ {3\}\) /\ 1,\ 2 sed

# add 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 Universe GTATA'

# add a blank line after every 5 lines (add a blank line after 5, 10, 15, 20, etc.)

Gsed '0,5G'

# valid for GNU sed only

Sed 'nterntern' nternternternternternternternternternternternternternternternternternternternternternternternternternternternTracternternTracternternTracternternThere, sed'nternternterternternternternternternternternternternternternternternternternternternternternternternternternternternternternternternternternternternternternternternternternternternternternternternternternternternternternterntern

# other sed

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

Sed-e'1 {$Q;}'- e'$! {hashing d;}'- e x

Sed-e'1 {$d;}'- e'$! {hashing d;}'- e x

# enter a blank line when there is only one line in the file

# when there is only one line in the file, display the line

# when there is only one line in the file, no output

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

Sed-n'/ regexp/p'

# method 1

Sed'/ regexp _

# method 2

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

Sed-n'/ regexp _ swap'

# method 1, corresponding 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

Gsed'/ AAA\ | BBB\ | CCC _

# most sed

# valid for GNU sed

# display 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'/. / {Hutchison qualified;}; x TincedAAA\ | BBB\ | CCC/b;d'

# valid for GNU sed only

# display lines with 65 or more characters

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

# display lines with less than 65 characters

Sed-n'/ ^.\ {65\} /! P'

Sed'/ ^.\ {65\} / d'

# method 1, corresponding to the script above

# 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 '8pm 12p'

# method 1

Sed '8JI 123d'

# method 2

# Show line 52 # method 1

Sed-n '52p' # method 2

Sed '52encrypd' method 3, which is more efficient when dealing with large files

Sed '52qwitd'

# starting from line 3, displayed every 7 lines # valid for GNU sed only

Gsed-n '3i7p' # other sed

Sed-n '3ptinction ${ptternternternternterntern;}'

# display the text between two regular expressions (included)

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

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 row in the duplicate row, and delete the other lines

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

# Delete duplicate lines in the file, whether adjacent or not. Note the caches that hold space can support

# size, or use GNU sed.

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

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

Sed'$! n; s / ^\ (. *\)\ n\ 1 $/\ 1max; t; D' # Delete the first 10 lines in the file

Sed '1cm10d' # 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 # method 1

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

Sed-n-e: a-e'1 recorder 10! {P / T / N / D;} /

# Delete multiple lines of 8 # valid only for GNU sed

Gsed '0,8d' # other sed

Sed'nposit nbsp; nbsp

# Delete lines that match the style

Sed'/ pattern/d'

# Delete the line with pattern. Of course, pattern.

# can be replaced with 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 a blank line at the end

Sed'/ ^ $/ Nscape /\ nCompact D'

# method 2, 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'/ ^\ ninvestors / {$d _ Ten _ b _ a'- e'}'

Sed-e: a-e'/ ^\ nThere's a lot of money.

# valid for all sed

# same as above, but only valid for gsed 3.02.*

# Delete the last line of each paragraph

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

Special applications:

-

# remove the nroff tag from the man page (man page). To use under Unix System V or bash shell

# you may need to add the-e option when using the 'echo' command.

Sed "s / .`echo\ b` / / g"

# double parentheses are required (Unix environment)

Sed's / .^ H / / g'

# in bash or tcsh, press Ctrl-V and then press Ctrl-H

Sed's _

# sed 1.5 representation of hexadecimal used by GNU sed,ssed

# extract the headers of newsgroups or e-mail

Sed'/ ^ $/ Q'

# all content after deleting the first blank line

# extract the body of a newsgroup or e-mail

Sed'1J / ^ $/ 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. On the basis of the line of headers generated by the previous script, it will be non-email.

# partial shaving of the address. (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