Shell scripts weapon of --sed stream editor

Linux is a core idea: everything is a file. In these documents, text files occupy an important position, adjust to change all system settings, software parameters, as well as basic command line operations, are operating on the "text". To manipulate text, certainly can not be separated text editor.

In the Linux system, text editor divided into two categories, one is a full screen editor, such as: vi, vim, nano, etc., to open a text file, text completion operations such as editing and saving in full screen mode; other a class is a stream editor, such as: sed, grep, awk, etc., the file is not opened directly, the command line, through a series of commands to complete the operation of the text, including search, insert, replace, delete, etc. is added.

sed is an excellent stream editor, it is in units of processing text, its general process flow is as follows: First, read the first line of the file to be processed into a buffer (memory of an area), then follow to a command line for the operation, these operations include find, replace, delete, etc., the operation is completed, the output from the buffer to the display screen, to complete a cycle. Thereafter, further reading the second line of text into a buffer, the second cycle starts. And so on, until all rows have been processed. At this time the contents of the screen display from the buffer and overwrite the original file no. The above-mentioned buffer, scientific name: the pattern space. If you need to survive the modified content, there are two ways, one using the -i parameter, directly modify the original documents; the second is using a redirect, the modified text is stored in another file.

Although the text sed support for the operation of many functions, but the most common is to replace the function, this article focuses on the use of replacement features, more functionality and usage also refer to the manual.

Sed tool command format is:

sed 选项 子命令	文件1,文件2,文件3……

Common options:

-e		在命令行模式下进行sed动作编辑。默认值
-f		从文件中读取需要执行的sed动作。
-i		不经过缓冲区,直接修改原文件里的内容。
-n		只打印匹配到的,或者经过后续的指令处理过的行。
-r		增加支持扩展表达式

Common subcommand:
s / target text / new text / in each row, the first match to the target text [] is replaced with the new text []
s / target text / new text / g in each row, the match toall[Text] replaced certain new text []
s / target text / new text / Ng, in each row, the first matchedNAll target text [] is replaced with the new text [a] and after
s / target text / new text / N in each row, the first matchedNA target [text] Replace [new text]

File or the file list, sed support handle multiple files, you need to use a comma separated between the two files.

Application examples sed

Test text as follows:

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
saslauth:x:997:76:Saslauthd user:/run/saslauthd:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
  1. Alternatively to match the current row 1st Content:
$ sed 's/root/admin/' test_file
admin:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/admin:/sbin/nologin
  1. Matching the current line to the second content:
$ sed 's/root/admin/2' test_file 
root:x:0:0:admin:/root:/bin/bash
  1. Replace the entire contents of the current row to match:
$ sed 's/root/admin/g' test_file
admin:x:0:0:admin:/admin:/bin/bash
operator:x:11:0:operator:/admin:/sbin/nologin
  1. Matching the current line to the second and all subsequent content:
sed 's/root/admin/2g' test_file 
root:x:0:0:admin:/admin:/bin/bash

to be continued……

Published 13 original articles · won praise 0 · Views 205

Guess you like

Origin blog.csdn.net/sinat_28296423/article/details/104028749