The Linux learning sed grep

Source: Internet
Author: User


    1. The most important function of the SED command is to replace it with the following document content as an example

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M01/8F/76/wKiom1jfGBGwKTdoAAAhAmip6nE421.png-wh_500x0-wm_ 3-wmp_4-s_3702725782.png "title=" qq picture 20170401110112.png "alt=" Wkiom1jfgbgwktdoaaahamip6ne421.png-wh_50 "/>

In Vim's command-line mode, you can use:%s/some/more/to replace some in the document with more

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/8F/76/wKiom1jfGSaQLweqAAAWMOu3SWo181.png-wh_500x0-wm_ 3-wmp_4-s_3179469492.png "title=" qq picture 20170401110551.png "alt=" Wkiom1jfgsaqlweqaaawmou3swo181.png-wh_50 "/>

The SED command is much more convenient: sed ' s/some/more/g ' data.txt--there is no change to the original document, up to a preview

Sed-i ' s/some/more/g ' data.txt-------have the I parameter to save the changes directly in the document


Sed ' s/^/&id/g ' data.txt add id+ spaces in each line of the document

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M00/8F/76/wKiom1jfG-ODMDK4AAAfICFz25U286.png-wh_500x0-wm_ 3-wmp_4-s_4195172997.png "title=" Qq20170401111735.png "alt=" Wkiom1jfg-odmdk4aaaficfz25u286.png-wh_50 "/>

Cat Data.txt | Grep-v ' ^$ ' | Sed ' s/^/&id/g '---the line of non-whitespace lines in each row of the outfit add id,^ symbol to start with

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/8F/74/wKioL1jfHCuwRhB_AAAcgQvF0vQ789.png "title=" Qq20170401111827.png "width=" 720 "height=" "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:720PX;HEIGHT:89PX; "alt=" Wkiol1jfhcuwrhb_aaacgqvf0vq789.png "/>

You can see the space line is not there, it is grep-v ' ^$ ' cause, grep will be in the following

Sed ' s/$/& id/g ' data.txt adds a space +id,$ at the end of each line of the document to indicate the end



Sed '/some/i ########################## ' data.txt add a row before the row with some #,i parameter

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/8F/74/wKioL1jfHhTig6dKAAAbOCxW_Tg052.png-wh_500x0-wm_ 3-wmp_4-s_200546023.png "title=" Qq20170401112646.png "alt=" Wkiol1jfhhtig6dkaaabocxw_tg052.png-wh_50 "/>

Sed '/some/a ########################## ' data.txt add a line after the row with some #,a parameter


Sed-n '/some/p ' data.txt print rows containing some,-n is for line operation, p parameter is agreed, SED is responsible for selecting

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/8F/74/wKioL1jfH1aTKGN9AAAaEEOlV0s342.png-wh_500x0-wm_ 3-wmp_4-s_453851705.png "title=" Qq20170401113217.png "alt=" Wkiol1jfh1atkgn9aaaaeeolv0s342.png-wh_50 "/>

You can also select the specified line: sed-n ' 1,5p ' data.txt print 1-5 lines


Here's how to specify the document in conjunction with other commands:

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M00/8F/77/wKiom1jfIUHSB7wvAAAYTIHWQAE088.png-wh_500x0-wm_ 3-wmp_4-s_249867676.png "title=" Qq20170401114031.png "alt=" Wkiom1jfiuhsb7wvaaaytihwqae088.png-wh_50 "/>

The above is the original document, the ultimate goal is to select the largest and smallest number in the document

Cat Number.txt | Sed ' s//\n/g ' | Grep-v "^$" | Sort-nr | Sed-n ' 1p; $p '

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M00/8F/77/wKiom1jfImfT_0XdAAARFuCcv8k043.png-wh_500x0-wm_ 3-wmp_4-s_1417829472.png "title=" Qq20170401114525.png "alt=" Wkiom1jfimft_0xdaaarfuccv8k043.png-wh_50 "/>

Now to dismantle these commands.

Cat Number.txt | Sed ' s//\n/g '--replace the characters in the document with a newline character so that all the numbers become a column

Cat Number.txt | Sed ' s//\n/g ' | Grep-v "^$" will be lined up in a column of blank lines to remove, excluding the first is empty

Cat Number.txt | Sed ' s//\n/g ' | Grep-v "^$" | Sort-nr sort all numbers from big to small

Cat Number.txt | Sed ' s//\n/g ' | Grep-v "^$" | Sort-nr | Sed-n ' 1p; $p ' Print out the first line and the last line;

Where the $ represents the end of the; indicates that the front and back are separate printing is not continuous use, it is continuous printing multi-line sed-n ' 1,5p ' such


The role of the 2.GREP command is primarily to match characters

Cat Number.txt | grep "4" indicates that the line containing 45 is matched

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/8F/75/wKioL1jfJJHxq93DAAALZ_nYJdE641.png-wh_500x0-wm_ 3-wmp_4-s_892795648.png "title=" Qq20170401115439.png "alt=" Wkiol1jfjjhxq93daaalz_nyjde641.png-wh_50 "/>

Cat Number.txt | Grep-v "4" means excluding the line containing the 4 character, and there is no difference between "" and "


grep command to view: RPM-QA When installing software | grep mysql---Check out some of the relevant installation packages for MySQL

Use SED command to modify certain files: Sed-i '/selinux/s/enforcing/disabled/'/etc/selinux/config---permanently off SELINUX


This article is from the "Sed for linux commands" blog, so be sure to keep this source http://beforce.blog.51cto.com/3335637/1912347

The Linux learning sed grep

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.