Linux“but”

command format

sed的命令格式:sed [options] 'command' file(s);

sed的脚本格式:sed [options] -f scriptfile file(s);

Options

 -e :直接在命令行模式上进行sed动作编辑,此为默认选项;

 -f :将sed的动作写在一个文件内,用–f filename 执行filename内的sed动作;

 -i :直接修改文件内容;

 -n :只打印模式匹配的行;

 -r :支持扩展表达式;

 -h或--help:显示帮助;

 -V或--version:显示版本信息。

parameter

Files: Specifies a list of text files to process.

sed common commands

 a\ 在当前行下面插入文本;

 i\ 在当前行上面插入文本;

 c\ 把选定的行改为新的文本;

 d 删除,删除选择的行;

 D 删除模板块的第一行;

 s 替换指定字符;

 h 拷贝模板块的内容到内存中的缓冲区;

 H 追加模板块的内容到内存中的缓冲区;

 g 获得内存缓冲区的内容,并替代当前模板块中的文本;

 G 获得内存缓冲区的内容,并追加到当前模板块文本的后面;

 l 列表不能打印字符的清单;

 n 读取下一个输入行,用下一个命令处理新的行而不是用第一个命令;

 N 追加下一个输入行到模板块后面并在二者间嵌入一个新行,改变当前行号码;

 p 打印模板块的行。 P(大写) 打印模板块的第一行;

 q 退出Sed;

 b lable 分支到脚本中带有标记的地方,如果分支不存在则分支到脚本的末尾;

 r file 从file中读行;

 t label if分支,从最后一行开始,条件一旦满足或者T,t命令,将导致分支到带有标号的命令处,或者到脚本的末尾;

 T label 错误分支,从最后一行开始,一旦发生错误或者T,t命令,将导致分支到带有标号的命令处,或者到脚本的末尾;

 w file 写并追加模板块到file末尾;

 W file 写并追加模板块的第一行到file末尾;

 ! 表示后面的命令对所有没有被选定的行发生作用;

 = 打印当前行号;

 # 把注释扩展到下一个换行符以前;

sed replace tag

 g 表示行内全面替换;

 p 表示打印行;

 w 表示把行写入一个文件;

 x 表示互换模板块中的文本和缓冲区中的文本;

 y 表示把一个字符翻译为另外的字符(但是不用于正则表达式);
 
 \1 子串匹配标记;

 & 已匹配字符串标记;

sed metacharacter set

 ^ 匹配行开始,如:/^sed/匹配所有以sed开头的行;
 
 $ 匹配行结束,如:/sed$/匹配所有以sed结尾的行;
 
 . 匹配一个非换行符的任意字符,如:/s.d/匹配s后接一个任意字符,最后是d;
 
 * 匹配0个或多个字符,如:/*sed/匹配所有模板是一个或多个空格后紧跟sed的行;
  
 [] 匹配一个指定范围内的字符,如/[ss]ed/匹配sed和Sed;
   
 [^] 匹配一个不在指定范围内的字符,如:/[^A-RT-Z]ed/匹配不包含A-R和T-Z的一个字母开头,紧跟ed的行;
  
 \(..\) 匹配子串,保存匹配的字符,如s/\(love\)able/\1rs,loveable被替换成lovers;
  
 & 保存搜索字符用来替换其他字符,如s/love/**&**/,love这成**love**;
  
 \< 匹配单词的开始,如:/\ 
 \> 匹配单词的结束,如/love\>/匹配包含以love结尾的单词的行;
 
 x\{m\} 重复字符x,m次,如:/0\{5\}/匹配包含5个0的行;
 
 x\{m,\} 重复字符x,至少m次,如:/0\{5,\}/匹配至少有5个0的行;
 
 x\{m,n\} 重复字符x,至少m次,不多于n次,如:/0\{5,10\}/匹配5~10个0的行;

sed user instance

Substitute operation: s command

Replace strings in text:

 sed 's/book/books/' file

Use the -n option with the p command to print only those lines where the substitution occurs:

 sed -n 's/test/TEST/p' file

Edit the file option -i directly , it will match the first book of each line in the file and replace it with books

 sed -i 's/book/books/g' file

full replacement mark g

Using the suffix /g flag replaces all matches on each line:

 sed 's/book/books/g' file

When you need to replace from the Nth match, you can use /Ng:

 echo sksksksksksk | sed 's/sk/SK/2g' 
 skSKSKSKSKSK
 echo sksksksksksk | sed 's/sk/SK/3g'
 skskSKSKSKSK  
 echo sksksksksksk | sed 's/sk/SK/4g'
 skskskSKSKSK 

delimiter

The character / in the above command is used as a delimiter in sed, or any delimiter can be used

 sed 's:test:TEXT:g' 
 sed 's|test|TEXT|g' 

Delimiters need to be escaped when they appear inside styles:

 sed 's/\/bin/\/usr\/local\/bin/g'

Delete operation: d command

Remove blank lines:

 sed '/^$/d' file

Delete line 2 of the file:

 sed '2d' file

Delete all lines from line 2 to the end of the file:

 sed '2,$d' file

Delete the last line of the file:

 sed '$d' file

Delete all lines starting with test in the file:

 sed '/^test/'d file

Matched string token &

The regular expression \w\+ matches every word, replacing it with [&], and the & corresponds to the word that was matched before:

 echo this is a test line | sed 's/\w\+/[&]/g'
 [this] [is] [a] [test] [line] 

All lines starting with 192.168.0.1 will be replaced with itself plus localhost:

 sed 's/^192.168.0.1/&localhost/' file 192.168.0.1localhost

Substring matching token \1

Match part of the given style:

 echo this is digit 7 in a number | sed 's/digit \([0-9]\)/\1/' 
 this is 7 in a number

The digit 7 in the command is replaced with 7. The substring matched by the pattern is 7, \(..\) is used to match the substring, the first substring matched is marked as \1, and so on, the second result matched is \2, E.g:

 echo aaa BBB | sed 's/\([a-z]\+\) \([A-Z]\+\)/\2 \1/' 
 BBB aaa

love is marked as 1, all loveables will be replaced with lovers, and printed:

 sed -n 's/\(love\)able/\1rs/p' file

Combine multiple expressions

 sed '表达式' | sed '表达式'  等价于:  
 sed '表达式; 表达式'

quote

sed expressions can be quoted using single quotes, but double quotes are required if the expression contains variable strings.

 test=hello 
 echo hello WORLD | sed "s/$test/HELLO" 
 HELLO WORLD

Range of selected lines: , (comma)

All lines within the range determined by the templates test and check are printed:

 sed -n '/test/,/check/p' file

Print all lines starting from line 5 to the first containing line starting with test:

 sed -n '5,/^test/p' file

For lines between template test and west, replace the end of each line with the string aaa bbb:

 sed '/test/,/west/s/$/aaa bbb/' file

Multipoint editing: e command

The -e option allows multiple commands to be executed on the same line:

 sed -e '1,5d' -e 's/test/check/' file

The first command of the sed expression above deletes lines 1 to 5, and the second command replaces test with check. The order in which the commands are executed has an effect on the result. If both commands are substitution commands, the first substitution command will affect the result of the second substitution command.

The command equivalent to -e is --expression:

 sed --expression='s/test/check/' --expression='/love/d' file

Read from file: r command

The contents of the file are read in and displayed after the line matching test. If multiple lines are matched, the contents of the file will be displayed below all matching lines:

 sed '/test/r file' filename

Write to file: w command
 
In the example all lines containing test are written to file:

 sed -n '/test/w file' example

Append (below line): a\command

Append this is a test line to the line starting with test:

 sed '/^test/a\this is a test line' file

Insert this is a test line after line 2 of the test.conf file:

 sed -i '2a\this is a test line' test.conf 

Insert (on line):

The i\ command appends this is a test line to the line beginning with test:

 sed '/^test/i\this is a test line' file

Insert this is a test line before line 5 of the test.conf file:

 sed -i '5i\this is a test line' test.conf

Next: n command

If test is matched, move to the next line of the matching line, replace the aa of this line with bb, print the line, and continue:

 sed '/test/{ n; s/aa/bb/; }' file 

Morph: y command

Convert all abcde in lines 1 to 10 to uppercase. Note that regular expression metacharacters cannot be used with this command:

 sed '1,10y/abcde/ABCDE/' file

Exit: q command

After printing line 10, exit sed sed '10q' file hold and get: h and G commands While sed processes the file, each line is saved in a temporary buffer called the pattern space, unless the line is deleted Or the output is canceled, otherwise all processed lines will be printed to the screen. Then the pattern space is emptied, and a new line is stored for processing.

 sed -e '/test/h' -e '$G' file 

In this example, after a line matching test is found, it is stored in the pattern space, and the h command copies it and stores it in a special buffer called the hold buffer. The second statement means that when the last line is reached, the G command fetches the line that holds the buffer, puts it back into the pattern space, and appends to the end of the line that now exists in the pattern space. In this example it is appending to the last line. Simply put, any line containing test is copied and appended to the end of the file.

Keep and swap: h command and x command

Swap the pattern space and hold the contents of the buffer. That is, swap the lines containing test and check:

 sed -e '/test/h' -e '/check/x' file 

script scriptfile

A sed script is a list of sed commands, and the script filename is booted with the -f option when starting Sed. Sed is very picky about the commands entered in the script, there cannot be any whitespace or text at the end of the command, and if there are multiple commands on one line, separate them with semicolons. Lines starting with # are commented out and cannot span lines.

 sed [options] -f scriptfile file(s)

print odd or even lines

method 1:

 sed -n 'p;n' test.txt  #奇数行 sed -n 'n;p' test.txt  #偶数行 

Method 2:

 sed -n '1~2p' test.txt  #奇数行 sed -n '2~2p' test.txt  #偶数行 

print the next line of the matched string

 grep -A 1 SCC URFILE 
 sed -n '/SCC/{n;p}' URFILE 
 awk '/SCC/{getline; print}' URFILE

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324123819&siteId=291194637