In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to use the SED command, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Sed (stream editor)
Function description: use script to deal with text files.
Syntax: sed [- hnV] [- e] [- f] [text file]
Supplementary note: sed can process and edit text files in accordance with the instructions of script.
Parameters:
-e or-- expression= processes the input text file with the script specified in the option.
-f or-- file= processes the input text file with the script file specified in the option.
-h or-- help displays help.
-n or-- quiet or-- silent displays only the results after script processing.
-V or-- version displays version information.
How sed works:
Sed is a non-interactive stream editor. The so-called non-interactive, means that using sed can only input editing commands under the command line to edit the text, and then view the output on the screen; while the so-called stream editor means that sed only reads one line from the file (or input) at a time, then carries on the specified processing on that line, and outputs the results to the screen (unless the screen output is canceled and does not explicitly use the print command), and then reads on the next line. The whole file is processed line by line like a pipeline and then output line by line.
Let's take a look at how sed works.
Sed does not process directly on the original input, but first puts the read-in lines into the buffer, processes the contents of the buffer, and does not write back to the original file after processing (unless the result is saved by shell's output redirection), but outputs directly to the screen. During the operation of sed, two buffers are maintained, one is the active "pattern space (pattern space)", and the other is the auxiliary "temporary buffer (holding space)". In general, whenever you run sed,sed, the first line is loaded into the pattern space, processed and output to the screen, and then the second line is loaded into the pattern space to replace the original content in the pattern space, and then processed, and so on.
In general, temporary buffers are not used, but there are special commands to exchange data between schema space and temporary buffers, which will be described later. Because all sed operations on the text are done in the buffer, it does not cause any damage to the original file.
Sed command format
The command format for sed is as follows:
Sed [- Options] ['Commands'] filename
Where Command is a sed command, and the sed command must be enclosed in a pair of single quotation marks to avoid being interpreted by shell. The format is as follows:
[address-range] [sed-command] or
[Pattern-to-match] [sed-command]
Address-range refers to the range of lines to be processed, also known as the address range; pattern-to-match is a pattern to match, is a regular expression, and sed-command is a sed command used to process specified lines. Here is a simple example:
Sed-n'1J 3p 'students
This command prints lines 1 through 3 of the file students to the screen. Note that there is no space between the address range and the sed command, and if you add a space, sed ignores it. The parameter-n is used to cancel the default output. By default, every line sed reads into the pattern space, whether it is processed or not, outputs the contents of the pattern space to the screen before reading the next line. The parameter-n can be used to cancel this default output, and the specified line is output to the screen only when the user uses the command p. If the p command is executed on the specified line without the argument-n, the lines will be printed twice.
The address range can be a number that represents a line number, or a range of two numbers separated by commas (including these two lines). The range can be a number, a regular expression, or a combination of both.
Pattern-to-match is a pattern to match, and sed will execute sed-command on all matching rows. In fact, the pattern-to-match here can also be thought of as an address, which is the line number of all lines that match the specified pattern. Therefore, the format of sed can be summarized as one:
Sed [- Options]'[address-range] [sed-command] 'filename
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 [odd] 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;}'
Serial number:
# 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 [top], and the right [left] 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 _
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 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 / ^.\ {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\ 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 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 will delete the blank lines in 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'/\ 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 GNU sed /\ B [0-9]\ {3\}\ > /, & /; ta' # 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 '0room5G' # is only valid for GNU sed
Sed'ntternternternternternternternternternternternterntermittingnternterntermittingnternternternternternternternternternternTanternternternTen sed'# 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 # enter a blank line when there is only one line in the file
Sed-e'1 {$Q;}'- e'$! {hash d;}'- e x # displays a line when there is only one line in the file
Sed-e'1 {$d;}'- 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 _
# 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'/. / {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
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'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 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 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;}'
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.
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. 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.