Introduction
sed is an online editor that processes content one line at a time. When processing, store the currently processed line in a temporary buffer, called "pattern space", and then use the sed command to process the contents of the buffer. After the processing is complete, send the contents of the buffer to the screen. Then the next line is processed, and so on, until the end of the file. The contents of the file are not changed unless you use redirection to store the output or add the -i parameter. Sed is mainly used to automatically edit one or more files; simplify repeated operations on files; write conversion programs, etc.
grammar:
sed [-nefr] [action] Parameter Description: -n: Use silent mode. In normal sed usage, all data from stdin (standard input) Usually listed on the screen. But if you add the -n parameter, only the line that has been specially processed by sed will be is displayed (so this parameter is usually used for viewing eg: sed -n '1,6p' /etc/passwd) -e: sed action editing directly on the command line mode -f: Write the sed action directly in a file, -f filename can execute the sed action in filename The -r:sed action supports the syntax of extended regular expressions, but the default is the syntax of basic regular expressions -i: directly modify the content of the read file, rather than output by the screen Action description: [n1[,n2]] function n1, n2: These two parameters do not have to exist, generally represent the number of rows of the selected action, for example, if my action It needs to be performed between 10 and 20 lines, then this action is written like '10,20[action]' function: The value of function is as follows: a: new, a can be followed by strings, and these strings will appear on a new line (the current next line) c: replace, c can be followed by strings, these strings can replace the lines between n1 and n2 d: delete, because it is deleted, all d are usually not followed by any parameters i: insert, i can be followed by strings, and these strings will appear on a new line (the current previous line) p: print, that is to print a selected data on the screen. Usually run with the parameter sed -n s: Replace, can be replaced directly. Usually the action of this s can be matched with regular expressions!
This tool, sed, can't be understood just by looking at it. It needs to be practical. Like me, practice, practice with me!
1. Delete function by behavior unit
First copy a copy of /etc/passwd to the test directory as follows:
[root@bogon shell]# cp /etc/passwd ./
Example: List the contents of passwd and print the line number, at the same time, delete lines 2 to 5
[root@bogon shell]# nl 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:shutdown:/sbin:/sbin/shutdown 8 halt:x:7:0:halt:/sbin:/sbin/halt .....!
sed '2,5d' This '2,5d' is the action of sed, and the latter d is the deletion. Because the 2~5 lines were deleted for him, the displayed data does not have 2~5 lines. Remember to enclose the action after sed with single quotes (') or double quotes (").
What if only row 2 is deleted? As follows
[root@bogon shell]# nl passwd | sed '2d' 1 root:x:0:0:root:/root:/bin/bash 3 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 4 halt:x:7:0:halt:/sbin:/sbin/halt .......!
What if you want to delete lines 3 to the last? As follows
[root@bogon shell]# nl passwd | sed '3,$d' 1 root:x:0:0:root:/root:/bin/bash 2 sync:x:5:0:sync:/sbin:/bin/sync
That "$" represents the meaning of the last line!
2. Add function
Or use the copied passwd file to test
Example: Add "eksliang ni hao bang!" after the second line as shown below
[root@bogon shell]# nl passwd | sed '2a eksliang ni hao bang!' 1 root:x:0:0:root:/root:/bin/bash 2 sync:x:5:0:sync:/sbin:/bin/sync eksliang ni hao bang! 3 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown .......!
Example: Add "eksliang ni hao bang!" before the second line as shown below
[root@bogon shell]# nl passwd | sed '2i eksliang ni hao bang!' 1 root:x:0:0:root:/root:/bin/bash eksliang ni hao bang! 2 sync:x:5:0:sync:/sbin:/bin/sync ......!
3. Replacement function in units of behavior
Example: Replace lines 2~5 of passwd with "eksliang or ickes"
[root@bogon shell]# nl passwd | sed '2,5c eksliang or ickes' 1 root:x:0:0:root:/root:/bin/bash eksliang or ickes 6 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin .....
The function of finding and replacing some data, this is a high-end product, look carefully!
The search and replace of sed is quite similar to the vi editor. The syntax is as follows:
sed 's/string to be replaced/new string/gw' Parameter Description: s: informs sed that this is a replacement action w: output the replaced lines to a new file
Let's demonstrate by looking for ip, what is search and replace!
Step 1: Use ipconfig to query ip [root@bogon shell]# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:0C:29:A6:32:02 inet addr:192.168.238.133 Bcast:192.168.238.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fea6:3202/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:9248 errors:0 dropped:0 overruns:0 frame:0 TX packets:3738 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:895349 (874.3 KiB) TX bytes:529029 (516.6 KiB) Interrupt:19 Base address:0x2024 Step 2: Use keywords and grep to select a key line of data [root@bogon shell]# ifconfig eth0 | grep 'inet addr' inet addr:192.168.238.133 Bcast:192.168.238.255 Mask:255.255.255.0 Step 3: Delete the part in front of the ip [root@bogon shell]# ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g' 192.168.238.133 Bcast:192.168.238.255 Mask:255.255.255.0 #The above deletion, the key lies in the regular expression "s/^.*addr:" #means: replace inet addr: with empty Step 4: Delete the part after ip [root@bogon shell]# ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g' | sed 's/Bcast:.*$//g' 192.168.238.133 #The above deletion (replacement), the key lies in the regular expression Bcast:.*$
The above example is also the processing step when I operate complex business through script. Check it first, and then try it step by step. If there is something wrong, I will modify it. After the modification, I will test it, then continue the loop, and finally write my accurate code.
The following example demonstrates the function of the w parameter, that is, the ip will be redirected to the ip.txt file in the end
ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g' | sed 's/Bcast:.*$//gw ip.txt'
4, through the sed command to view
It is quite easy to check, it is nonsense to say too much, see the example
When there is no sed, to list data from 11 to 20 lines, the most commonly used command combination is: head -n 20 | tail -n 10
[root@bogon shell]# nl passwd | head -n 20 | tail -n 10 11 nobody:x:99:99:Nobody:/:/sbin/nologin 12 dbus:x:81:81:System message bus:/:/sbin/nologin 13 usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin 14 vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin 15 rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin 16 avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin 17 abrt:x:173:173::/etc/abrt:/sbin/nologin 18 pulse:x:498:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin 19 haldaemon:x:68:68:HAL daemon:/:/sbin/nologin 20 saslauth:x:497:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
With the sed tool, of course there is no need to look like this:
[root@bogon shell]# nl passwd | sed -n '11,20p' 11 nobody:x:99:99:Nobody:/:/sbin/nologin 12 dbus:x:81:81:System message bus:/:/sbin/nologin 13 usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin 14 vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin 15 rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin 16 avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin 17 abrt:x:173:173::/etc/abrt:/sbin/nologin 18 pulse:x:498:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin 19 haldaemon:x:68:68:HAL daemon:/:/sbin/nologin 20 saslauth:x:497:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
The above example adds the parameter "-n", this "-n" stands for "quiet mode"! If you don't have this -n, you can try it, and the data taken out is messy.
5. The combination of sed and regular expressions is called Beiershuang!
Example: Take out the line containing the MAN field in the file /etc/man.config, and remove the commented line and blank line with the # sign
I still follow the steps in 3 above to analyze the problem
Step 1: Use grep to remove the line containing 'MAN' [root@bogon shell]# cat /etc/man.config | grep 'MAN' # when MANPATH contains an empty substring), to find out where the cat # MANBIN /usr/local/bin/man .....! # Every automatically generated MANPATH includes these fields MANPATH /usr/man MANPATH /usr/local/share/man ......! Step 2: Delete the line commented with # [root@bogon shell]# cat /etc/man.config | grep 'MAN' | sed 's/#.*$//g' ......! MANPATH /usr/man MANPATH /usr/X11R6/man ......! MANPATH_MAP /bin /usr/share/man MANPATH_MAP /sbin /usr/share/man .....! Step 4: Delete blank lines [root@bogon shell]# cat /etc/man.config | grep 'MAN' | sed 's/#.*$//g' | sed '/^$/d' MANPATH /usr/man MANPATH /usr/share/man MANPATH /usr/local/man MANPATH /usr/local/share/man MANPATH /usr/X11R6/man MANPATH_MAP /bin /usr/share/man MANPATH_MAP /sbin /usr/share/man .....!
Seeing that there is no empty line deleted at the end, the syntax for working with regular expressions is as follows:
sed '/regex/action'
For example, add ickes after each line of the passwd file
[root@bogon shell]# sed '/$/a ickes' passwd root:x:0:0:root:/root:/bin/bash ickes sync:x:5:0:sync:/sbin:/bin/sync ickes shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown ickes
6. Add -i to directly modify the content of the file, which is also cool
Because the file can be modified directly, there is no need to use data stream redirection to redirect the modified content to other files at this time, but it is also a dangerous parameter. It is recommended not to use this command to modify the system configuration file.
Example: Delete all the lines starting with # and blank lines in the above man.config file (physically delete), but I will copy the man.config file for testing
The shell script is as follows:
[root@bogon shell]# cat delete.sh #/bin/bash sed -i 's/#.*$//g' man.config sed -i '/^$/d' man.config
After execution, check man.config without any comments and blank lines
Summarized as follows:
- Each action can be preceded by a number. For example: 1,20s/old/new/g, which means to replace old in lines 1~20 with old
- Each action can be combined with regular expressions, such as said in point 5 above
- Remember that the sed tool parses text in units of lines