《Linux命令列與shell指令碼編程大全》 第二十章 學習筆記

來源:互聯網
上載者:User

$ 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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.