5-2-rhel6.3-sed Stream Editing Tool (Red Hat Enterprise Linux Server6.3) @ Tree Bag Wandering

Source: Internet
Author: User



Sed stream editing tools (most of which are located in behavioral operating units)



SED does not directly modify the source file by default, but instead copies it to a buffer called pattern space through a pipeline or a file read, and its instructions are completed and printed on the screen in this pattern space.



Syntax format:



The above refers to the positioning in the end of the unit, then the line is defined as the address, that is, manipulating the object



The scope of the address has a centralized format



N# specifying a unique line number



Two or #Specify starting from line 1, spaced 2 is 1,3,, 5, by analogy


$ #Specify the last line


/ Regular Expression / # Matches the operand of the regular expression

\ a Regular expression a # matches regular expressions between \ a and a

3,5 # matches 3-5 rows


1, + 10 # matches 1 and 1 followed by 10 lines of content

sed [options [Script instruction] [input file]

Option: -n # silent output, does not print the contents of the mode space, is automatically printed by default

-E Script # allows multiple script directives to be executed

-f # reading instructions from a file

-l # output of the length of the president

-r # Extended Regular Expressions

-i # Modifying source files directly

directive: A (Append) #Append

D (delete) #Delete

s (substitution) #Replace



C # changes



y# Replace by character



p# Printing



q# exit



i# Insertion



l# Printing (non-printing characters displayed)



l# printing (displaying print characters)



r# read in file contents



650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/70/F3/wKioL1XB1E_B3dA_AAEHd_ZzfD4807.jpg "/>



Example: Append "This is a example" after the first line






[[Email protected] ~]# SED "1a This is a example"/opt/fstab_mrpan






Example: Append "This is a example" before line 6th #注意源文件发生改变了






[Email protected] ~]# Sed-i "6i This is a example"/opt/fstab_mrpan



650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/70/F6/wKiom1XB0lbzqctjAACbL0Tl_8s565.jpg "/>





Example: Replace command



[[Email protected] ~]# sed "s/defaults/mr.pan/g"/opt/fstab_mrpan






[Email protected] ~]# sed-i "3s/fstab/fstab_mr.pan/g"/opt/fstab_mrpan



Example: Changing a command



[[Email protected] ~]# sed "/pan/c exit"/etc/passwd | More



Example: using regular expressions



Match line units that contain fstab



[Email protected] ~]# Sed-i "/fstab/a This is a example"/opt/fstab_mrpan



[Email protected] ~]# sed-n "/^$/d"/opt/fstab_mrpan



You can use the E F or delimiter semicolon when you need to execute multiple instructions, but you can also use the-f file, which is easy to modify and not error-prone.



Example: Delete a row containing example, replace all 0 for 1



(1), [[email protected] ~]# sed-e "/example/d"-E "s/0/1/g"/opt/fstab_mrpan



(2), [[email protected] ~]# sed "/example/d;s/0/1/g"/opt/fstab_mrpan



(3), as shown:



650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/70/F3/wKioL1XB1E_xiHY0AAFSxUv8mVc037.jpg "/>



(4), write a file



650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/70/F6/wKiom1XB0lezRbYgAAB8cMZOTTs253.jpg "/>



Example: File contents are as follows



650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/70/F3/wKioL1XB1E-yWa6HAADVUpZT9Bk272.jpg "/>



650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/70/F6/wKiom1XB0lfRLp7fAABIQfUU_-c306.jpg "/>






650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/70/F6/wKiom1XB0lew6SsKAAFB_3U5DN0865.jpg "/>






Here's a summary of the SED format:



[Match line] {



Command 1



Command 2



Command 3



}



Example: Delete a space in a file



First step: Create a space environment



[Email protected] ~]# Cat/etc/group >> sed_d2_example



[Email protected] ~]# sed g/sed_d2_example i



Step two: Write the file vim sed.sh as shown below



650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/70/F3/wKioL1XB1FDDaJR7AAAoqsYtglQ959.jpg "/>



Step three: [[email protected] ~]# sed-f./sed.sh./sed_d2_example i# Remove spaces






Example: inserting a blank line on a matching line



[[Email protected] ~]# sed "/sshd/{x;p;x;}"/etc/group



Example: inserting a blank line under a matching line



<>

5-2-rhel6.3-sed Stream Editing Tool (Red Hat Enterprise Linux Server6.3) @ Tree Bag Wandering



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.