$ cat test.txt line1line2line3line4
$ sed -i '/line/{n;d}' test.txt $ cat test.txt line1line2line3line4
$ sed -i '/line1/{n;d}' test.txt suzhaoqiang@suzhaoqiang-OptiPlex-380:~/android/source/linux_learned$ cat test.txt line1line2line3line4
$ cat test.txt line1line2line3line4line5
$ sed -i '/line1/{N;s/\n/ /}' test.txt $ cat test.txt line1 line2line3line4line5
$ sed -i '{N;s/\n/ /}' test.txt
$ cat test.txt line1line2line3line4line5$ sed 'N;/line3/d' test.txt line1line2line5
$ sed 'N;/line3/D' test.txt line1line2line4line5
$ cat test.txt line1line2line3line4line5$ sed -n 'N;/line/p' test.txt line1line2line3line4$ sed -n 'N;/line/P' test.txt line1line3
$ cat test.txtline1line2line3line4line5line6$ sed -n '/line1/{h;p;n;p;g;p}' test.txt line1line2line1
$ sed -n '1{h;N;G;p}' test.txt line1line2line1
man 寫道After the address (or address-range), and before the command, a ! may be inserted, which specifies that the command shall only be executed if the address (or
address-range) does not match.
$ cat test.txtline1line2line3line4line5line6$ sed -n '/[1-3]/!p' test.txt line4line5line6
$ sed -n '1!G;h;$p' test.txt line6line5line4line3line2line1$ tac test.txtline6line5line4line3line2line1
$ tac test.txtline6line5line4line3line2line1
]b [label]
$ sed '2,3b;s/line/Line/;s/Line/lines/' test.txt lines1line2line3lines4lines5lines6
$ sed -n '1b test;G;:test h;$p' test.txtline6line5line4line3line2line1
$ echo "This, is, a, test, to, remove, commas." | sed -n '{:start;s/,//p;b start}'This is, a, test, to, remove, commas.This is a, test, to, remove, commas.This is a test, to, remove, commas.This is a test to, remove, commas.This is a test to remove, commas.This is a test to remove commas.
$ echo "This, is, a, test, to, remove, commas." | sed -n '{:start;s/,//p;/,/b start}'This is, a, test, to, remove, commas.This is a, test, to, remove, commas.This is a test, to, remove, commas.This is a test to, remove, commas.This is a test to remove, commas.This is a test to remove commas.
$ echo "This, is, a, test, to, remove, commas." | sed -n '{:start;s/,//;/,/b start;p}'This is a test to remove commas.
$ cat test.txt line1line2line3line4line5line6$ sed 's/line[45]/Line/;t;s/line[0-9]/line/' test.txt linelinelineLineLineline
$ echo "This, is, a, test, to, remove, commas." | sed -n '{:start;s/,//;t start;p}'This is a test to remove commas.
$ cat test.txt line1line2line3line4line5line6$ sed 's/[0-9]/0&/' test.txt line01line02line03line04line05line06
$ sed 's/\([0-9]\)/0\1/' test.txt line01line02line03line04line05line06
$ echo "1234567" | sed ':start;s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;t start'1,234,567$ echo "12345678" | sed ':start;s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;t start'12,345,678$ echo "123456789" | sed ':start;s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;t start'123,456,789
String reg1 = "\\d(?=(\\d{3})+$)";System.out.println("123456789".replaceAll(reg1, "$0,"));System.out.println("12345678".replaceAll(reg1, "$0,"));System.out.println("1234567".replaceAll(reg1, "$0,"));
$ cat sed_test#!/bin/bashsed -n '1!G;h;$p' "$1"
$ cat test.txtline1line2line3line4line5line6$ sed_test test.txt line6line5line4line3line2line1
$ cat test.txtline1line2line3$ sed '$!G' test.txt line1line2line3
$ sed '/^$/d;$!G' test.txt
$ cat test.txtline1line2line3$ sed '=' test.txt | sed 'N;s/\n/ /'1 line12 line23 line3
$ sed -n '$p' test.txt
$ cat test.txt line1line2line3line4line5line6line7line8line9$ sed ':start;$q;N;4,$D;b start' test.txt line7line8line9
$ sed ':start;$q;N;1,4D;b start' test.txt line4line5line6line7line8line9
$ sed '/./,/^$/!d' test.txt
$ sed '/./,$!d' test.txt
$ sed '{> :start> /^\n*$/{$d;N;b start}> }' test.txt
,所有我們不能使用.*?這種形式,當然,下面這種直白的貪婪模式也肯定是錯的
$ sed 's/<.*>//g' test.html
我們簡單的類比一下惰性匹配:
$ sed 's/<[^>]*>//g' test.html
最後,如果你願意,可以加一個刪除空行的處理:
$ sed 's/<[^>]*>//g;/^$/d' test.html