SED command using a detailed summary

Source: Internet
Author: User

Usage sed [option] ' Address Command ' yourfilee.g. Sed-n ' 2,13p ' Lineuser #打印文件lineuser中第2 the contents of the ~13 line,-n is option, 2,13 is address,p as commandaddress and command the middle of the space can also be omitted SED processing the file to be processed line by SED read into the pattern space, according to matching and corresponding processing command output display, by default, the content of each mode space will output sed common option-N: Lines that do not display the pattern space, only the rows that are processed by the match are displayed-I: Overwrite the end of the process directly with the original file-E script: You can specify multiple-e parameters, each processing command according to the most original file content processing, rather than one processing and then passed to the next processing-F script-file: Write command parameters to a file to call the-r: Table Use the extended Regular expression practice file [[email protected]ejin240 tmp]# Cat Lineuser1:root2:bin3:daemon4:adm5:gootee6:sync7:shutdown8:halt9: Mail10:uucp11:operator12:hadoop13:gopher14:admin15:nobody address matching method 1.ADDR1 match specifies a specific line # displays the third line of content [[email protected] tmp ]# sed-n ' 3p ' Lineuser3:daemon2.addr1,addr2 matching rows from addr1 to ADDR2#打印第四到六行 [[email protected] tmp]# sed-n ' 4,6p ' Lineuser4:adm5:gootee6:sync3./regexp/matches rows that contain regexpPrint all lines containing OO characters [[email protected] tmp]# sed-n '/oo/p ' lineuser1:root5:gootee12:hadoop 4./regexp1/,/regexp2/ Match lines from containing REGEXP1, to REGEXP2 # Print from lines containing Mon to rows containing ADM [[[email protected] tmp]# sed-n '/mon/,/adm/p ' lineuser3:daemon4: Adm 5.first~step match begins with first behavior, line # prints 1,3,5 with step interval .... Lines, that is, odd lines [[email protected] tmp]# sed-n ' 1~2p ' Lineuser1:root3:daemon5:gootee7:shutdown9:mail11:operator13: gopher15:nobody  #打印2, 4,6 .... line, i.e. even line [[email protected] tmp]# sed-n ' 2~2p ' lineuser2:bin4:adm6:sync8:halt10:uucp12:hadoop14:admin   6.addr1,+n matches the ADDR1 line and the following N rows [[email protected] tmp]# sed-n ' 3,+4p ' Lineuser3:daemon4:adm5:gootee6:sync7: Shutdown 7./regexp/,+n matches the row containing the regexp, and its subsequent N rows [[email protected] tmp]# sed-n '/adm/,+1p ' LINEUSER4:ADM5: Gootee14:admin15:nobody    command Command 1. A \text adds a line after the matched line # 5 to 8 lines after each row adds a row [[email protected] tmp]# sed ' 5,8a \nice to meet ' Lineuser1:root2:bin3:daemon4: Adm5:gooteenice to meet You6:syncnice to meet YOU7:SHUTDOwnnice to meet You8:haltnice to meet you9:mail10:uucp11:operator12:hadoop13:gopher14:admin15:nobody 2. I \text add a line before the matched line # Add one row per line before lines 5 through 8 [[email protected] tmp]# sed ' 5,8i \nice to meet you ' Lineuser1:root2:bin3:daemon4: Admnice to meet You5:gooteenice to meet You6:syncnice to meet You7:shutdownnice to meet You8:halt9:mail10:uucp11:operator1 2:hadoop13:gopher14:admin15:nobody  3. d Delete matching lines # Delete rows 5 to 13 [[email protected] tmp]# sed ' 5,13d ' lineuser1:root2:bin3:daemon4:adm14:admin15:nobody 4. The C \text replaces all the contents of the matching line # 13 to line # # to one line [[email protected] tmp]# sed ' 5,13c \nice to meet ' Lineuser1:root2:bin3:daemon4: Admnice to meet You14:admin15:nobody  5. P Print Matching line example has appeared above, no longer listing 6. R file inserts the contents of file after a matching line # Add the contents of the files after each line on line 3rd to 5th [[email protected] tmp]# cat namebeijingshenzhen[[email protected] tmp]# sed ' 3,5r name ' Lineuser1:root2:bin3:daemonBeijingShenzhen4:admBeijingShenzhen5:gooteeBeijingShenzhen6:sync7 : shutdown8:halt9:mail10:uucp11:operator12:hadoop13:gopher14:aDmin15:nobody 7 s/regexp/replacement/replaces regexp with replacement, by default only the first occurrence of each line is processed, and if all strings are to be processed, you need to add g at the end, If you need to ignore the case, you need to add i# at the end of the 3rd to 15th line where Oo is replaced by Nice to MEET you[[email protected] tmp]# sed ' 3,15s/oo/nice to MEET you/' Lineuser1:root2:bin3:daemon4:adm5:gNICE to MEET YOUtee6:sync7:shutdown8:halt9:mail10:uucp11:operator12:hadNICE to MEET youp13:gopher14:admin15:nobody      

SED commands use a detailed summary

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.