Sed's essence

xiaoxiao2021-03-06  70

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 regular expressions ★ Convert MAN manual into ordinary text format (new) ★ SED MAN manual (used above the method) ★ Introduction to the command line parameter

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.txt Sed Is A Street Editor -------------------- A Street Editor Is Used to Perform Basic Text Transformations ON An NPUT Stream ---- -------------------------------------------------- -------------------------- While In Some Ways Similar To An Editor Which Permits Scripted Edits (Such AS ED), ------ -------------------------------------------------- ------------------------ Sed Works by Making Only One Pass over, and is consequultly more

-------------------------------------------------- ---------------------------

Efficient. But it is Sed's Ability To Filter Text In a Pipeline Which Particular L ★ Output Specifies Row P Other Types of Editors.

Sed -e "1,4p" -n sedest.txt sed -e "/ from / p" -n sedest.txt sed -e "1, / from / p" -n sedest.txt ★ Add one in front of each line Table character (^ i) SED "S / ^ / ^ I / g" sedest.txt Note ^ i's input method is Ctrl-V Ctrl-i single ^ represents the lead

★ Added after each line

SED "S / $ / - END / G" SedTest.txt single $ represents the end

★ Display the line number of the specified mode match line [/ pattern /] = sed -e '/ is / =' Sedst.txt 1

Sed is a stream editor ----------------------

3

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

-------------------------------------------------- ------------------------------

While In Some Ways Similar To An Editor Which Permits Scripted Edits (SUCH AS ED

)

,

-------------------------------------------------- ------------------------------

-

-

Seduce

Sed Works by Making Only One Pass over The Input (s), and is consequently more

-------------------------------------------------- ---------------------------

9

Efficient. But it is Sed's Ability To Filter Text In a Pipeline Which Particular

l

y

-------------------------------------------------- ------------------------------

-

-

It means analyzing Sedst.txt, showing the line number of the matching row containing the IS string, note that the IS string appears in 11 lines is stdout, if not redirection processing, does not affect the original sedest.txt

★ Add text after matching line [/ pattern /] A / or [Address] A / ^ D

sed -f sedadd.script sedtest.txt Sed is a stream editor A 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)

,

-------------------------------------------------- ------------------------------

-

-

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 Particularl

y

-------------------------------------------------- ------------------------------

-

-

[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 not redirection processing, does not affect the original sedest.txt

Many people want to complete this operation directly in the command line instead of one sedadd.script, unfortunately, this needs to be used

Continue /,

[SCZ @ / home / scz / src]> sed -e "/ from / A //

> "Sedst.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 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 Sed's Ability To Filter Text In a Pipeline Which Particular

l

y

Sed -e '6, 10d' Sedst.txt

Delete 6-10 lines, including 6 and 10

Sed -e "2D" sedest.txt

Delete the contents of the second line

Sed "1, / ^ $ / d" sedest.txt Deletes all content between the first line to the first space line

Note that this command is easy to bring unexpected results, and the SED is not empty from the first line from the first line in Sedtest.txt.

Sed "1, / from / d" sedest.txt

Delete all content between the first line to the first line containing the Row of the FROM string, including the first included

The row of the from string.

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

Sed -e "/ IS / c //

********** "Sedst.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 //

********** "Sedest.txt ----------------------

Search all from strings within 1-12 lines, replace it into **** string

★ Mode match after the defined range

Sed "/ but / s / is / are / g" Sedst.txt

For those rows containing the BUT string, put the IS to Are

Sed "/ IS / S / T / T /" sedest.txt

For those rows that contain IS strings, replace each of the first T, 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

Replace the 5th appearance of each line of IS strings to Are

★ & representative final match

SED "S / ^ $ / (&) /" sedest.txt

Add a pair for all space lines ()

SED "S / IS / (&) / G" Sedtest.txt

Added to all IS strings ()

Sed "S /.*/( )/" Sedtest.txt

Add a pair of ()

Sed "/is/s/.*/( )/" Sedtest.txt

Add a pair of 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: / sbin: / usr / sbin: / usr / x11r6 / bin:

Remove the {: / 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 of {: / 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 :.

[/] Means / lose special meaning

/ / Also indicate / lose meaning

/ 1 indicates the first appearance of the child matches

/ 2 Represents the second appearance of the child matches

/(.*/) Represents the child match

Remove the tail:, then add a new path

Path = `Echo $ PATH | SED 'S / / (.*/):" 1 /' `: $ HOME / SRC

Pay attention to the distinction between the tricks `and single quotes'.

★ Test and improve the operation efficiency of the sed command

Time SED -N "1, 12p" Webkeeper.db> / dev / null

Time Sed 12Q Webkeeper.db> / dev / null

It 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" sedest.txt -n

Write the contents of 1-10 lines in Sedtest.txt to the sed.out file.

★ Specify input file [address] R inputfile

Sed "1R sedappend.txt" Sedst.txt "

Adding the contents of the sedappend.txt to the first line of the sedest.txt file

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

Sed "Y / Abcdef / Abcdef /" Sedst.txt

Replace all Abcdef lowercase letters in SedTest.txt replace it into an Abcdef capital letter.

★!

Sed -E '3, 7! d' Sedst.txt

Delete all lines outside of 3-7 lines

Sed -e '1, / from /! d' Sedtest.txt

Find rows that contain from strings, delete all of them

★ / C regular expression C use

Sed -e "/: from: d" Sedst.txt

Equivalent of sed -e "/ from / d" sedest.txt

★ The complexity of regular expressions in the sed command

CAT> sedest.txt

^ // [}] {. *} [/ (] $ /)

^ D

How can I replace this line?

/ (] /) // [}] {. *} ^ [

★ Convert MAN manual into ordinary text format (new)

Man Sed | col -b> sed.txt

Sed -e "s / ^ h // g" -e "/ ^ $ / d" -e "s / ^ i / / / g" -e "s / ^ I / / g" sed.txt> sedman

.

TXT

Delete all the retracted keys, blank lines, replace the list of tables 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 editor

Synopsis

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 ipper

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 used by it?

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, Then EXIT.

-H

--help print a usage message briefly summarizing these

Command-line Options and the bug-reporting address,

.

NN

--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 commnd.

-e script

--Expression = Script

Add The Commands in Script to the set of commists

TO BE RUN While Processing the INPUT.

-f script-file

--file = script-file

Add The Commands Contained in The File Script-file

To the set of commists to be run while processing

The INPUT.

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 instance processing the

Above, Thase 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 commms.

#comment

The Comment Extends Until the next newline (or the

End of a -e script fragment).

The closing bracket of a {} block.

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 welst 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 a S /// HAS DONE A SUCCESSFUL SUBSTIN SINCE

The Last Input Line Was Read and Since The Last T of THE

Command, Then Branch to label; if Label is omitted,

Branch to end of script.

C /

Text Replace The SELECTED LINES with TEXT, WHICH HAS

Each Embedded Newline Preceded by a backslash.

D Delete Pattern Space. Start next cycle.

D Delete Up to the First Embedded Newline In The PAT

Tern Space. Start Next Cycle, But Skip Reading

From the input if there is still data in the PAT

Tern 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 patternspaces.

l List out the current line in a `` Visually unambigu-

OUS '' FORM.

N n read / append the next line of infut into the pattern

Space.

P Print The Current Pattern Space.

P whped Up to the first Embedded Newline of the Cur

Rent 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 SPE

Cial 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 File-

Name.

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 Given with no Addresses, in which case

THE COMMAND WILL BE EXECUTED For All INPUT LINES;

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 THE COMMAND

All Input Lines Which Match The Inclusive Range of Lines

Starting from the first address and continuous to the second

Ond Address. Three Things to Note About Address Ranges:

The syntax is addr1, addr2 (i.e., 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 tsted 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 AresuTed:

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

(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 Expressions

POSIX.2 BRES SHOULD BE Supported, But They Aren't 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 be given.]

Miscellaneous Notes

This Version of Sed Supports A / Sequence in All

Regular Expressions, The Replacement Part of A Substitution

(s) Command, and in the source and dest parts of a

TRANSLITERATE (Y) Command. THE / IS Stripped, and the

NEWLINE IS Kept.

See Also

AWK (1), ED (1), EXPR (1), Emacs (1), Perl (1), TR (1), VI (1),

Regex (5) [Well, One Ough To Be Written ... xxx], sed.info,

Any of Various Books on Sed, The Sed FAQ

(http://www.wollery.deMon.co.uk/seedtut10.txt

http://www.ptug.org/sed/sedfaq.htm.

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

New Post(0)