Text processing command

xiaoxiao2021-03-06  60

test:

With redhat6.0 for the test environment, the SED command under Solaris is strong than Linux, but because there is no test environment, I only give the test under Linux here.

table of Contents:

★ Command line parameters ★ First, we have such a text file Sedst.txt ★ Output Specifier row P ★ Add a tab in front of each line (^ i) ★ Added in each line to increase - "display specified The line number of the pattern matching line [/ pattern /] = ★ Add text after matching line [/ pattern /] a / or [address] A / ★ Remove Matches [/ pattern /] D or [Address1] [, address2] D ★ Replace the match [/ pattern /] c / or [address1] [, address2] c / ★ Insert text in front of the matching line [/ pattern /] I / or [Address] I / ★ replace match string (note no longer Is a matching line) [Addr1] [, addr2] s / OLD / new / g ★ The pattern match after the limit range ★ Specify the first match in each row ★ & representative final match ★ Use the SED to modify the PATH environment variable ★ Test and improve the SED command Run Efficiency ★ Specify Output File [Address1] [, Address2] w OutputFile ★ Specify input file [Address] R INPUTFILE ★ Replace the corresponding character [Address1] [, address2] y / old / new / ★! Use ★ / C regular expression C ★ SED commands in the SED command complexity ★ Conversion MAN manual into ordinary text format (new) ★ SED MAN manual (used above the method)

★ Introduction to the command line parameters

Sed -e script Specifies the SED Edit Command -fiPTFile specified in the file being the SED editing command -n silent mode, suppressing redundant output information from the SED command execution, such as only those rows that are changed.

do not understand? Don't close, put these dirty to one side, go down, but the following introduction does not include the explanation of regular expressions, if you don't understand, it may be a bit trouble.

★ First, suppose we have such a text file Sedest.txt

CAT> sedest.txtsed is a stream editor -------------------- A stream editor is buy to perform ,,,,,,,,,,,, -------------------------------------------------- ------------------------- While In Some Ways Similar To An Editor Which Permits Scripted Edits (Such AS ED), ------- -------------------------------------------------- ------------------------- Sed Works by Making Only One Pass over the input (s), and is consequultly more ------- -------------------------------------------------- -------------------- Efficient. But it is sided's ability to filter text in a pipeline Which particular-------------- -------------------------------------------------- ---------------- ★ Outputs the specified range of rows P Other Types of Editors.

Sed -e "1,4p" -n sedest.txtsed -e "/ from / p" -n sedest.txtsed -e "1, / from / p" -n sedest.txt

★ Add a tab in front of each line (^ i)

Sed "S / ^ / ^ I / g" Sedtest.txt

Note that the input method of ^ i is Ctrl-V Ctrl-i

Single ^ represents the head

★ Added after each line

SED "S / $ / - END / G" Sedst.txt

Single $ represents the end

★ Display the line number of the specified mode match line [/ pattern /] =

Sed -e '/ is / =' sedest.txt

1SED is a stream editor ---------------------- 3a stream editor is used to perform Basic Text Transformations on An Input Stream --------- -------------------------------------------------- --------------------- While in Some Ways Similar To An Editor Which Permits Scripted Edits (Such AS ED), ----------- -------------------------------------------------- --------------------------------------------------- -------------------------------------------------- ---------------- 9EffIsent. But it is sida will ly1 felter text in a pipeline Which particular ------------------- -------------------------------------------------- ------------ meaning analyzing SedTest.txt, showing the line number of matching lines containing the IS string, pay attention to the IS string in 11 lines This output is stdout, if not Do a reactive processing, it does not affect the original sedest.txt ★ Add text after matching line [/ pattern /] A / or [Address] A / ^ D

Sed -f sedd.script sedest.txt

Sed is a stream editor

A stream editor is buy to perform ,,,,,,,,,,

While In Some Ways Similar To An Editor Which Permits Scripted Edits (Such AS ED), -------------------------------- -------------------------------------------------- Sed Works by Making Only One Pass over The Input (s), And is Consequently More ---------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------- Efficient. But it is IS Sed's Ability to Filter Text in a pipeline Which particular -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------ [SCZ @ / Home / SCZ / SRC]> SED -E "A / / ------------------------------- ----------------

Find rows that contain from strings, add in the next line of the line. This output is STDOUT, if you do not redirect processing, you don't affect the original sedest.txt Many people want to complete this operation directly on the command line instead of one Sedd.Script, unfortunately, this requires continuation symbol/,

[SCZ @ / Home / SCZ / SRC]> SED -E "/ from / A //> " SEDTEST.TXT

[SCZ @ / Home / SCZ / SRC]> SED -E "A / /> " Sedst.txt

Above this command will add a new line after all lines

[SCZ @ / home / SCZ / SRC]> SED -E "1 a //> " Sedst.txt

Use the following two lines of COPY / PASTE to a shell command line, the effect is the same

"Sedst.txt

[address] A / only accepts an address designation

For a command, single quotes can not be supported, only two quotes, and other commands such as D commands,

★ Delete Matches [/ Pattern /] D or [Address1] [, Address2] D

Sed -e '/ --------------------------------------------- / d 'sedest.txt

Sed is a stream editor

A stream editor is used to perform basic text transformations on an input streamWhile in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input (s), and is consequently moreefficient. But it is Sed's Ability To Filter Text In a Pipeline Which Particularl

y

Sed -E '6, 10d' sedest.txt Deletes 6-10 lines of content, including 6 and 10

Sed -E "2D" sedest.txt Deletes the contents of the second line

Sed "1, / ^ $ / d" Sedst.txt Deletes all content between the first line to the first space line Note that this command is easy to bring unexpected results, starting from the first line in Sedtest.txt There is no time, then Sed

Sed "1, / from / d" sedest.txt Deletes all content between the first line to the first row, including the first line containing the from string.

★ Replace matching [/ pattern /] c / or [address1] [, address2] C /

Sed -e "/ is / c // **********" Sedest.txt

Look for all matching lines that contain IS strings, replace **********

********* ---------------------- ********* -------- -------------------------------------------------- ---------------------- While In Some Ways Similar To An Editor Which Permits Scripted Edits (Such AS ED), ---------- -------------------------------------------------- ---------------------- ********* ------------------ -------------------------------------------------- --------- ********** ------------------------------- -------------------------------------------------- Sed -e "1,11c // **********" Sedst.txt ---------------------- in the 1-12 line Search all from strings, replace it separately in **** string

★ Mode match after the defined range

Sed "/ but / s / is / are / g" SedTest.txt For rows that contain but strings, put the IS to Are

Sed "/ IS / S / T / T / T / T /" Sedst.txt For rows that contain IS strings, replace each of the first T in T

Sed "/ while /, / from / p" sedest.txt -n outputs all content between the two mode matching lines

★ Specify the first one of the matches in each row

SED "S / IS / ARE / 5" Sedst.txt replaces the 5th appearance of each row of IS strings to Are

★ & representative final match

Sed "S / ^ $ / (&) /" SedTest.txt Add a pair of all space lines ()

Sed "S / IS / (&) / G" Sedtest.txt gives all IS strings from outside ()

Sed "S /.*/( )/" SedTest.txt adds a pair for all rows ()

Sed "/S/S/.*/( )/" Sedst.txt gives all the rows containing the IS string ()

★ Modify the path environment variable with SED

View Path Environment Variables First [SCZ @ / Home / SCZ / SRC]> Echo $ PATH / USR / BIN: / USR / BIN: / BIN: / USR / local / bin: / sbin: / usr / sbin: / usr / X11R6 / bin :.

Remove the tail {: / usr / x11r6 / bin :.} [scz @ / home / scz / src]> Echo $ PATH | SED "S / ^ / (. * /): // usr [/] x11r6 // bin: [.] $ // 1 / "/ usr / bin: / usr / bin: / bin: / usr / local / bin: / sbin: / usr / sbin

Remove the middle {: / bin:} [scz @ / home / scz / src]> Echo $ PATH | SED "S / ^ / (. * /): // bin: / (. * /) $ // 1 / 2 / "/ usr / bin: / usr / bin / usr / local / bin: / sbin: / usr / sbin: / usr / x11r6 / bin :.

[/] Indicates / Losing Special Significance // Similarly / Losing Significance / 1 Represents the first appearance / 2 represents the second appearance of the sub-match / (.) representator matching

Remove the tail:, then add a new path Path = `Echo $ PATH | SED 'S / / (. . ★ Test and improve the operation efficiency of the sed command

Time SED -N "1, 12p" Webkeeper.db> / dev / nulltime sed 12q Webkeeper.db> / dev / null can be seen that the latter is higher than the former.

[address] q Exits SED execution when you hit the specified line

★ Specify output file [address1] [, address2] w OutputFile

Sed "1, 10W Sed.out" Sedst.txt -n writes the contents of 1-10 lines in SedTest.txt to the sed.out file.

★ Specify input file [address] R inputfile

Sed "1R sedappend.txt" sedest.txt attached the contents of the sedaplend.txt to the first line of the sedest.txt file

★ Replace the corresponding character [address1] [, address2] y / old / new /

Sed "Y / Abcdef / Abcdef /" SedTest.txt replaces all Abcdef lowercase letters in SedTest.txt to Abcdef capital letters.

★!

Sed -E '3, 7! d' Sedtest.txt Deletes all lines outside of 3-7 lines

Sed -e '1, / from /! d' Sedtest.txt finds rows that contain from strings, delete all of them

★ / C regular expression C use

Sed -e "/: from: D" Sedst.txt is equivalent to SED -E "/ from / d" sedest.txt

★ The complexity of regular expressions in the sed command

CAT> sedest.txt ^ // [}] {. *} [/ (] $ /) ^ D

How to replace this line into / (] /) // [}] {. *} ^ [

★ Convert MAN manual into ordinary text format (new)

Man SED | col -b> sed.txtsed -e "s / ^ h // g" -e "/ ^ $ / d" -e "s / ^ i / / g" -e "s / ^ I / / g "Sed.txt> sedman.txt Deletes all the retracted degeneration, the blank line, replaces the list of lists into 8 spaces, and the remaining formulas replace it with a space.

★ SED's MAN manual (used by the above method)

Name Sed - a stream editorsynopsis sed [-n] [-v] [--quiet] [- Silent] [--Version] [--help] [-e script] [-EXPIPRESSION = Script] [-f Script-file] [--file = script-file] [script-if-no-other-script] [file ...] Description Sed is a stream editor. A stream editor is buy to per- form Basic Text Transformations ON an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input (s), and is consequently more efficient . But it is sed's ability to filter text in a pipeline which particularly distinguishes it from other types of editors.OPTIONS sed may be invoked with the following command-line options: -V --version Print out the version of sed that is being run And a copyright notice, the exit. -h - Help Pr int a usage message briefly summarizing these command-line options and the bug-reporting address, then exit. -n --quiet --silent By default, sed will print out the pattern space at the end of each cycle through the script. These options disable this automatic printing, and sed will only produce output when explicitly told to via the p command. -e script --expression = script Add the commands in script to the set of commands to be run while processing the input. -f script -file --file =

Script-file address-file to the set of commists to be run while processing the infut. if no -e, -f, - expression, or --file options are given on the command-line , then the first non-option argument on the command line is taken to be the script to be executed. If any command-line parameters remain after processing the above, these parameters are interpreted as the names of input files to be processed. A file name of - refers to the standard input stream The standard input will pro- cessed if no file names are specified.Command Synopsis This is just a brief synopsis of sed commands to serve as a reminder to those who already know sed; other documenta-. Tion (Such As the Texinfo Document) Must Be Consulted for Fuller Descriptions. Zero-Address `` Commands '': Label Label for B And T Commands. #comment The Comment Extends Until The next newline (or the end of a -e script fragment).} The clock. Zero- or one- address commands =

Print the current line number. A / text Append text, which has each embedded newline pre- ceeded by a backslash. I / text Insert text, which has each embedded newline pre- ceeded by a backslash. Q Immediately quit the sed script without processing any more input, except that if auto-print is not diabled the current pattern space will be printed. r filename Append text read from filename. Commands which accept address ranges {Begin a block of commands (end with a}). b label Branch to label; if label is omitted, branch to end of script t label If as /// has done a successful substitution since the last input line was read and since the last t command, then branch to label;. if label is omitted, Branch to end of script. c / text replace the success lines with text, which has each embedded newline preceeded by a backslash. d Delete pattern space. Start next cycle. D Delete up to the first embedded newline in the pattern space. Start next cycle, but skip reading from the input if there is still data in the pattern space. h H Copy / append pattern space to hold space. g G Copy / append hold space to pattern space. x Exchange the contents of the hold and pattern spaces. l List out the current line in A `` Visually unambigu- us ''

form. n N Read / append the next line of input into the pattern space. p Print the current pattern space. P Print up to the first embedded newline of the current pattern space. s / regexp / replacement / Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement. The replacement may contain the special character & to refer to that portion of the pattern space which matched, and the special escapes / 1 through / 9 to refer to the corresponding matching sub-expressions in the regexp. w filename Write the current pattern space to filename. y / source / dest / Transliterate the characters in the pattern space which appear in source to the corresponding charac- ter in dest.Addresses Sed commands Can Be Gi ven with no addresses, in which case the command will be executed for all input lines; with one address, in which case the command will only be executed for input lines which match that address; or with two addresses, in which case the command will be executed for all input lines which match the inclusive range of lines starting from the first address and continuing to the sec- ond address Three things to note about address ranges:. the syntax is addr1, addr2 (ie, the addresses are sepa- rated By a comma; The Line Which Addr1 Matched Will Always Be Accepted, Even IF Addr2 Selects An Earlier Line;

and if addr2 is a regexp, it will not be tested against the line that addr1 matched. After the address (or address-range), and before the com- mand, a! may be inserted, which specifies that the com- mand shall only be executed if the address (or address- range) does not match The following address types are supported:... number match only the specified line number first ~ step match every step'th line starting with line first For example, `` Sed -n 1 ~ 2p '' Will Print All The ODD-NUMBERED LINES in The Input Stream, and The Address 2 ~ 5 Will Match Every Fifth Line, Starting with The Second. (this is a gnu extension) $ match the last Line. / Regexp / Match Lines Matching The Regular Expression Regexp. / Cregexpc Match Lines Matching The Regular Expression Regexp. The c May Be Any Character.Regular Expres sions POSIX.2 BREs should be supported, but they are not com- pletely yet The / n sequence in a regular expression matches the newline character There are also some GNU extensions [XXX FIXME:... more needs to be said At. The Very Least, a Reference To Another Document Which Describes What Is Supported Should Be Given.] Miscellaneous Notes this Version of Sed Supports A /

转载请注明原文地址:https://www.9cbs.com/read-106525.html

New Post(0)