The SED for linux text processing

Source: Internet
Author: User

the SED for linux text processing

I introduced the text processing grep, and now let me introduce the second swordsman--sed of the Three Musketeers of text processing.

       sed is an older, more powerful stream editor for text processing, with support for regular expressions that allows for a large number of complex text editing operations. SED itself is a very complex tool, with special books explaining the specific uses of SED, but the individual feels that there is no need to learn every detail of it, and that is not particularly significant.

1.sed Usage

sed [-hnv][-e<script>][-f<script file >][text file]

2. Parameter description (option command)

-e<script> or --expression=<script> processes the input text file with the script specified in the options.

    •      -f<script file > or--file=<script file > process the input text file with the script file specified in the options.

    •      -H or--help display Help.

    •      -N or--quiet or--silent shows only the results after script processing.

    •      -V or--version displays version information.

    •       -i.bak backup file and edit

    •     &NBSP;&NBSP;-R can use extended regular

3. Action Description (Edit command)

  • A: New, a can be followed by a string, and these strings will appear on a new line (the next line)

  • C: Replace, C can be followed by strings, these strings can replace the line between N1,N2!

  • D: Delete, because is deleted, so D usually does not take anything after;

  • I: Insert, I can be followed by the string, and these strings will appear on a new line (the current line);

  • P: Printing, that is, printing a selected data. Normally p will run with the parameter Sed-n ~

  • S: Replace, can be directly replaced by the work, usually this s action can be paired with the formal notation

  • =: Prints line numbers for lines in pattern space

  • ! : Pattern space matching row take back processing

4. Address delimitation

A. About addressing:

                       

B. Digital addressing: digital Addressing is actually the number to specify the specific operation of the edited line, as follows:

[[Email protected] (yuaning) ~]# cat abc.txt Abcde[[email protected] (yuaning) ~]# sed-nr ' 4s/d/4/p ' abc.txt (Specify D of line 4th to replace with 4,- n default output, remember to add p at the end of the regular expression 4
[[Email protected] (yuaning) ~]# cat abc.txt 1linux2linux3linux4linux5linux[[email protected] (yuaning) ~]# sed-n ' 2,4s/ Linux/hello/p ' abc.txt 2hello3hello4hello (Specify line 2nd to 4th characters Linux replaces with Hello)
[[Email protected] (yuaning) ~]# sed-n ' 1,+2s/linux/hello/p ' abc.txt 1hello2hello3hello (Specify 1th to 1th + 2 lines of characters Linux replace with Hello)
[[Email protected] (yuaning) ~]# sed-n ' $s/linux/hello/p ' abc.txt 5hello ($ = last line)
[[Email protected] (yuaning) ~]# sed-n ' 2!s/linux/hello/p ' Abc.txt 1hello3hello4hello5hello (! Indicates a line other than the specified line, remember not to write the reverse)

C. Regular expression addressing: regular addressing use purpose and digital addressing exactly the same, using a different way, is through the matching of regular expressions to determine which rows to process the edit, the other rows do not require additional processing, as follows:

[[Email protected] (yuaning) ~]# cat abc.txt 1linux2linux3linux4linux5linux[[email protected] (yuaning) ~]# sed '/3linux/ d ' Abc.txt 1linux2linux4linux5linux (indicates that only the specified row is deleted and the other rows are unchanged)
[Email protected] (yuaning) ~]# cat abc.txt 1linux2linux3linux4linux5linux666[[email protected] (yuaning) ~]# sed '/^[[: Digit:]].*/d ' abc.txt [[email protected] (yuaning) ~]# sed '/^[[:d igit:]][[:lower:]]\+/d ' abc.txt 666 (select matching row delete with regular expression)

D. Digital and regular mix: regular expressions and digital combinations are more flexible

[[Email protected] (yuaning) ~]# cat abc.txt 1linux2linux3linux4linux5linux666[[email protected] (yuaning) ~]# sed ' 4,/^$ /d ' abc.txt 1linux2linux3linux666 (means delete from line 4th to blank line)

E. Advanced use: reads a data instruction from the first one to the last instruction, the instruction is separated, and then the next data loop, until the data is all executed once

mode space and hold space: The schema space is initialized to NULL, the line is processed to automatically output to the screen and the mode space is cleared, and the space is initialized to a blank line, which is the default

A \ n is not automatically cleared after processing. Mode space and maintain space, from the point of view of the program, is actually sed in the work of the time occupied some memory space and address,

SED will release the memory and return it to the operating system when it is finished.

G: Copy the contents of the hold space to the pattern space, and the original mode space will be zeroed out.

G: Appends the contents of the hold space to the pattern space \ n.

H: Copy the contents of the pattern space into the hold space, and the original hold space will be zeroed out.

H: Appends the contents of the pattern space to the hold space \ n.

D: Delete all rows in the pattern space and read in the next line.

D: Delete the first line of the pattern space, not the next line.

X: Exchange Content

N: Reads the next line that matches to the pattern space.

N: Reads the match to the next line after appending to the pattern space.

[[Email protected] (yuaning) ~]# seq 1 |sed-n ' n;p ' (print even line) 246810 (read to 1 in the mode space, then the n command immediately after the next line of 2 cover 1, and then p command printing)
[[Email protected] (yuaning) ~]# seq 1 5 |sed ' 1! G;h;$!d ' (reverse output) 54321[[email protected] (yuaning) ~]# seq 1 5 |sed-n ' 1! G;h, $p ' 54321
[[Email protected] (yuaning) ~]# seq 1 5 |sed ' n;d ' (Output last line) 5[[email protected] (yuaning) ~]# seq 1 5 |sed ' $!d ' 5
[[Email protected] (yuaning) ~]# seq 1 5 |sed ' $! n;$! D ' (output last two lines) 45
[[Email protected] (yuaning) ~]# seq 1 3 |sed ' G ' (add a blank line after each line) 123
[[Email protected] (yuaning) ~]# seq 1 |sed ' n;d ' (output odd line) 13579

There are many uses of SED, more need to be explored in later work, here is just a simple record.

The SED for linux text processing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.