linux-sed Tools

Source: Internet
Author: User
Tags ming

Let's talk about sed first, sed itself is also a pipeline command, you can analyze the standard input! And SED can also replace, delete, new, fetch specific lines and so on function! Very good ~ Let us first understand the use of SED, and then to talk about his use good!

 [[email protected] ~]# sed [-NEFR] [action]   options and Parameters:-N: Use Quiet (silent) mode. In the usage of general sed, all data from STDIN is generally listed on the screen. However, if you add the-n parameter, only the line (or action) that is specially processed by SED is listed. -E: Action editing of SED directly in command-line mode;-F: The action of SED is written in a file directly, and-f filename can run the SED action within filename;-r:sed's action supports the syntax of extended formal notation. 。 (The default is the basic formal notation of French)-I: Directly modifies the contents of the read file instead of being output by the screen. Action Description: [N1[,n2]]functionn1, N2: Not necessarily exist, generally represents "select the number of lines of action", for example, if my action is required between 10 to 20 rows, then the "10,20[action behavior" function bottom The following: A: New, a can be followed by a string, and these strings will appear in a new line (the current next line) ~c: Replace, C can be followed by strings, these strings can replace the line between N1,N2! D: Delete, because it is deleted, so d usually do not take any knock; I: Insert, I can be followed by a string, and these strings will appear on a new line (the current line); P: Print, that is, print out a selected data. Normally p will run with parameter sed-n ~s: Replace, can be directly replaced by work! Usually this s action can be paired with formal notation! For example 1,20s/old/new/g is! 
    • Add/Remove Features in behavioral units

Sed light is what you can't read! So I have to practice again! Let's play the Delete and new features now!

  example one: List the contents of the/etc/passwd and print the line number, and please delete the 2nd to 5th line!"  [[email protected] ~]# NL/ETC/PASSWD | Sed ' 2,5d '  1 root:x:0:0:root:/root:/bin/bash 6 Sync:x:5:0:sync:/sbin:/bin/sync 7 Shutdown:x:6:0:shut Down:/sbin:/sbin/shutdown .....
  (omitted later) ...  

Did you see that? Sed's action is ' 2,5d ', and that D is delete! Because 2-5 rows to him deleted, so the data displayed there is no 2-5 line Luo ~ In addition, note that the original should be issued SED-E only, no-e also line! Also note that the action behind the SED, be sure to enclose in ' two single quotes!

If the types of questions change, for example, if you just delete line 2nd, you can use the NL/ETC/PASSWD | Sed ' 2d ' to achieve, if you want to delete the 3rd to the last line, it is "nl/etc/passwd | Sed ' 3, $d ' ", the money- size" $ "stands for the last line!

  example two: the title, after the second line (that is, add in the third line) and add" drink tea? " Words!  [[email protected] ~]# NL/ETC/PASSWD | Sed ' 2a drink tea '  1 Root:x:0:0:root:/root:/bin/bash 2 Bin:x:1:1:bin:/bin:/sbin/nologindrink Tea 3 D Aemon:x:2:2:daemon:/sbin:/sbin/nologin .....
  (omitted later) ...  

Hey! The string added after a will appear on the second line. What if it's going to be before the second line? "NL/ETC/PASSWD | Sed ' 2i drink tea ' is right! is to turn "a" into "I". Adding a row is simple, so what if you want to add more than two lines?

 Span class= "TERM_HD" style= "FONT-SIZE:10PT; font-family: Fine Ming Body, Fixedsys,serif; Color:rgb (187,187,187) "> example three: Add two lines after the second line, such as" Drink tea or ... "and" Drink beer? " [[email protected] ~]# NL/ETC/PASSWD | Sed ' 2a Drink tea or ... \  > drink beer? '      1 Root:x:0:0:root:/root:/bin/bash 2 Bin:x:1:1:bin:/bin:/sbin/nologindrink tea or ... drink beer? 3 Daemon:x:2:2:daemon:/sbin:/sbin/nologin .....
 (omitted later) ...  

The focus of this example is "we can add more than one line!" Several lines can be added "but each row must be preceded by a backslash" \ "To add a new line! So, in the example above, we can find that there is a presence on the last side of the first line! That's for sure!

    • The substitution and display function of the behavior unit

Just introduced how to add and delete, then if the whole line to replace it? Look at the example below:

  example four: I would like to replace the contents of 2–5 into" No 2-5 number "?  [[email protected] ~]# NL/ETC/PASSWD | Sed ' 2,5c No 2-5 number '  1 root:x:0:0:root:/root:/bin/bashno 2-5 number 6 Sync:x:5:0:sync:/sbin:/bin/sync  .....
  (omitted later) ...  

In this way we can replace the entire line of data! It's very easy! SED also has the better use of the Dongdong! We wanted to list line 11th to 20th, we had to go through the Head-n 20 | Tail-n 10 "method to deal with, very troublesome ~ sed can simply take out the few lines you want! It was caught by the line number. Look at the following example first:

  example five: list only 第5-7 lines within the/etc/passwd file  [[email protected] ~]# NL/ETC/PASSWD | Sed-n ' 5,7p '  5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 6 Sync:x:5:0:sync:/sbin:/bin/sync 7 Shutdow N:x:6:0:shutdown:/sbin:/sbin/shutdown 

There is an important option "-n" in the above command, according to the documentation, this-n represents "Quiet mode"! So why use Quiet mode? You can make your own sed ' 5,7p ' and know It (5-7 rows repeat output)! If you add-n parameters, the output data is a lot worse! You can get some line numbers in a file by using the SED's display function in the behavior unit! It's a great feature! Isn't it?

    • Part of the data search and replace the function

In addition to the entire line of processing mode, SED can also be used to perform part of the data search and replace the function Oh! Basically, the search for SED is similar to that of VI. He's kind of like this:

sed ' s/ to be substituted string / new string /g '

The part of the special font in the table above is the keyword, please write it down! Up to three slashes divided into two columns is the replacement of old and new strings! We use the bottom of this example to obtain IP data, a section of the process to you to see, let you know what is our so-called search and replace it!

Step One: First observation of the original information, using/sbin/ifconfig query IP? [Email protected] ~]#/sbin/ifconfig eth0Eth0 Link encap:ethernet HWaddr 00:90:cc:a6:34:84inet addr:192.168.1.100 bcast:192.168.1.255 mask:255.255.255.0Inet6 addr:fe80::290:ccff:fea6:3484/64 scope:link up broadcast RUNNING multicast mtu:1500 metric:1..... (omitted below) ... # because we haven't talked about IP yet, here you have a concept! Our focus is on the second line, # that's 192.168.1.100 's line! First use the keyword to catch the line! Step Two: Use the keyword with grep to extract a critical row of data[Email protected] ~]#/sbin/ifconfig eth0 | grep ' inet addr '          inet Addr: 192.168.1.100 bcast:192.168.1.255 mask:255.255.255.0# Only one line left on the spot! Next, we will start to addr: All Delete, is like this: # inet addr:192.168.1.100 bcast:192.168.1.255 mask:255.255.255.0# above the deletion of the key in the "^ . *inet addr: "Now! The formal notation appears! ^_^Step Three: Delete the previous part of IP[Email protected] ~]#/sbin/ifconfig eth0 | grep ' inet addr ' | \>sed ' s/^.*addr://g '192.168.1.100 bcast:192.168.1.255 mask:255.255.255.0# Take a closer look at the previous step and the front part is gone! The next step is to delete the following sections, i.e.: # 192.168.1.100 bcast:192.168.1.255 mask:255.255.255.0# At this time the formal notation required is: "bcast.*$" is! Step four: Remove the parts behind the IP[Email protected] ~]#/sbin/ifconfig eth0 | grep ' inet addr ' | \>sed ' s/^.*addr://g ' | sed ' s/bcast.*$//g '192.168.1.100

The practice of this example also suggests that you follow this step to study your commands! Is the first observation, and then a layer of trial, if there is wrong place, the first to be modified, after the test, after successful, and then continue to test down. In the introduction of bird brother above, that a large series of orders to do four steps! Right! ^_^

Let's continue to study the combination of SED and formal notation! Let's say I just have a few lines of data that man exists, but I don't want any of the notes that contain #, and I don't need any blank lines! What should be handled at this time? These steps can be used to look at the following:

Step One: First use grep to take the keyword man row out[Email protected] ~]#cat/etc/man.config | grep ' man '# when MANPATH contains a empty substring), to find out where the cat# manbin pathname# MANPATH manpath_element [corresponding_catdir]# manpath_map path_element manpath_element# MANBIN/ usr/local/bin/man# every automatically generated MANPATH includes these Fieldsmanpath/usr/man.... (omitted later) ....Step Two: Delete the data after the annotations! [Email protected] ~]#cat/etc/man.config | grep ' man ' | sed ' s/#.*$//g 'Manpath/usr/man.... (omitted later) .... # from the above can be seen from the original annotated data has become blank line! So, the next step is to delete the blank line[Email protected] ~]#cat/etc/man.config | grep ' man ' | sed ' s/#.*$//g ' | \>sed '/^$/d 'Manpath/usr/manmanpath/usr/share/manmanpath/usr/local/man.... (omitted later) ....
    • Modify file contents directly (Dangerous action)

Do you think SED has the ability to do this? That's not it! Sed can even modify the contents of the file directly! Instead of using pipeline commands or data flow redirection! However, because this action will be directly modified to the original file, so please do not take the system configuration file to test Oh! Let's use your downloaded regular_express.txt file to test it out!

example six: using SED to regular_express.txt the end of each line in a row. Then replace it![Email protected] ~]#sed-i ' s/\.$/\!/g ' regular_express.txt# The top of the-I option allows your sed to directly modify the contents of subsequent files instead of the screen output! # This paradigm is used in lieu! Please do your own cat this file to check the results! example seven: using sed to add "# This is a test" directly on the last line of Regular_express.txt[Email protected] ~]#sed-i ' $a # This is a test ' regular_express.txt# since $ represents the last line, and A's action is new, so the file is finally added ROM! 

Sed "-i" option can directly modify the file content, this feature is very helpful! For example, if you have a 1 million-line file that you want to add some text to in line 100th, using VIM at this point may go insane! Because the file is too big! What do you do? Just use sed! You don't even need to use VIM to revise the features directly modified/replaced by SED! It's great!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

linux-sed Tools

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.