分类:
2008-05-28 13:17:15
sed [-n] [-e] 'command' file(s) sed [-n] -f scriptfile file(s)
zhyfly@zhyfly:~$ sed 'some-sed-commands' input-file>output-file
[address [,address]]w filename
zhyfly@zhyfly:~/bash$ cat test.txt The honeysuckle band played all night long for only $90. It was an evening of splendid music and company. Too bad the disco floor fell through at 23:00. The local nurse Miss P.Neave was in attendance. zhyfly@zhyfly:~/bash$ sed -e '1,2w test.bak' test.txt The honeysuckle band played all night long for only $90. It was an evening of splendid music and company. Too bad the disco floor fell through at 23:00. The local nurse Miss P.Neave was in attendance. zhyfly@zhyfly:~/bash$ cat test.bak The honeysuckle band played all night long for only $90. It was an evening of splendid music and company.
address r filename
zhyfly@zhyfly:~/bash$ cat test.txt The honeysuckle band played all night long for only $90. It was an evening of splendid music and company. Too bad the disco floor fell through at 23:00. The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -e '1p' test.txt The honeysuckle band played all night long for only $90. The honeysuckle band played all night long for only $90. It was an evening of splendid music and company. Too bad the disco floor fell through at 23:00. The local nurse Miss P.Neave was in attendance. zhyfly@zhyfly:~/bash$ sed -n -e '1p' test.txt The honeysuckle band played all night long for only $90.
zhyfly@zhyfly:~/bash$ sed -n -e '2p' test.txt It was an evening of splendid music and company.
zhyfly@zhyfly:~/bash$ sed -n -e '2,3p' test.txt It was an evening of splendid music and company. Too bad the disco floor fell through at 23:00.
zhyfly@zhyfly:~/bash$ sed -n -e '/company/p' test.txt It was an evening of splendid music and company.
zhyfly@zhyfly:~/bash$ sed -n -e '2,/23:00/p' test.txt It was an evening of splendid music and company. Too bad the disco floor fell through at 23:00.
zhyfly@zhyfly:~/bash$ sed -n -e '2,3!p' test.txt The honeysuckle band played all night long for only $90. The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -e '=' test.txt 1 The honeysuckle band played all night long for only $90. 2 It was an evening of splendid music and company. 3 Too bad the disco floor fell through at 23:00. 4 The local nurse Miss P.Neave was in attendance. zhyfly@zhyfly:~/bash$ sed -n -e '=' test.txt 1 2 3 4 zhyfly@zhyfly:~/bash$ sed -n -e '/music/p' test.txt It was an evening of splendid music and company. zhyfly@zhyfly:~/bash$ sed -n -e '/music/=' test.txt 2 zhyfly@zhyfly:~/bash$ sed -n -e '/music/p' -e '/music/=' test.txt It was an evening of splendid music and company. 2 zhyfly@zhyfly:~/bash$ sed -e '=;p' test.txt 1 The honeysuckle band played all night long for only $90. The honeysuckle band played all night long for only $90. 2 It was an evening of splendid music and company. It was an evening of splendid music and company. 3 Too bad the disco floor fell through at 23:00. Too bad the disco floor fell through at 23:00. 4 The local nurse Miss P.Neave was in attendance. The local nurse Miss P.Neave was in attendance. zhyfly@zhyfly:~/bash$ sed -n -e '=;p' test.txt 1 The honeysuckle band played all night long for only $90. 2 It was an evening of splendid music and company. 3 Too bad the disco floor fell through at 23:00. 4 The local nurse Miss P.Neave was in attendance. zhyfly@zhyfly:~/bash$ sed -n -e '=' -e 'p' test.txt 1 The honeysuckle band played all night long for only $90. 2 It was an evening of splendid music and company. 3 Too bad the disco floor fell through at 23:00. 4 The local nurse Miss P.Neave was in attendance. zhyfly@zhyfly:~/bash$
zhyfly@zhyfly:~/bash$ sed -e 'a\this line will be added to the end of each line!oooooooooo' test.txt The honeysuckle band played all night long for only $90. this line will be added to the end of each line!oooooooooo It was an evening of splendid music and company. this line will be added to the end of each line!oooooooooo Too bad the disco floor fell through at 23:00. this line will be added to the end of each line!oooooooooo The local nurse Miss P.Neave was in attendance. this line will be added to the end of each line!oooooooooo zhyfly@zhyfly:~/bash$ sed -e '/music/a\this line will be added to the end of the matching line!oooooooooo' test.txt The honeysuckle band played all night long for only $90. It was an evening of splendid music and company. this line will be added to the end of the matching line!oooooooooo Too bad the disco floor fell through at 23:00. The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -e 'i\this line will be inserted to the begin of each line!oooooooooo' test.txt this line will be inserted to the begin of each line!oooooooooo The honeysuckle band played all night long for only $90. this line will be inserted to the begin of each line!oooooooooo It was an evening of splendid music and company. this line will be inserted to the begin of each line!oooooooooo Too bad the disco floor fell through at 23:00. this line will be inserted to the begin of each line!oooooooooo The local nurse Miss P.Neave was in attendance. zhyfly@zhyfly:~/bash$ sed -e '/music/i\this line will be inserted to the begin of the matching line!oooooooooo' test.txt The honeysuckle band played all night long for only $90. this line will be inserted to the begin of the matching line!oooooooooo It was an evening of splendid music and company. Too bad the disco floor fell through at 23:00. The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -e 'c\this line will be modified to the each line!oooooooooo' test.txt this line will be modified to the each line!oooooooooo this line will be modified to the each line!oooooooooo this line will be modified to the each line!oooooooooo this line will be modified to the each line!oooooooooo zhyfly@zhyfly:~/bash$ sed -e '/music/c\this line will be modified to the matching line!oooooooooo' test.txt The honeysuckle band played all night long for only $90. this line will be modified to the matching line!oooooooooo Too bad the disco floor fell through at 23:00. The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ cat test.txt The honeysuckle band played all night long for only $90. It was an evening of splendid music and company. Too bad the disco floor fell through at 23:00. The local nurse Miss P.Neave was in attendance. zhyfly@zhyfly:~/bash$ sed -e '/music/d' test.txt The honeysuckle band played all night long for only $90. Too bad the disco floor fell through at 23:00. The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ cat test.txt The honeysuckle band played all night long for only $90. It was an evening of splendid music and company. Too bad the disco floor fell through at 23:00. The local nurse Miss P.Neave was in attendance. zhyfly@zhyfly:~/bash$ sed -e 's/h/ooooo/' test.txt Toooooe honeysuckle band played all night long for only $90. It was an evening of splendid music and company. Too bad toooooe disco floor fell through at 23:00. Toooooe local nurse Miss P.Neave was in attendance. zhyfly@zhyfly:~/bash$ sed -e 's/h/ooooo/g' test.txt Toooooe ooooooneysuckle band played all nigooooot long for only $90. It was an evening of splendid music and company. Too bad toooooe disco floor fell tooooorougooooo at 23:00. Toooooe local nurse Miss P.Neave was in attendance. zhyfly@zhyfly:~/bash$ sed -e '1,2s/h/ooooo/' test.txt Toooooe honeysuckle band played all night long for only $90. It was an evening of splendid music and company. Too bad the disco floor fell through at 23:00. The local nurse Miss P.Neave was in attendance. zhyfly@zhyfly:~/bash$ sed -e '1,2s/h/ooooo/g' test.txt Toooooe ooooooneysuckle band played all nigooooot long for only $90. It was an evening of splendid music and company. Too bad the disco floor fell through at 23:00. The local nurse Miss P.Neave was in attendance. zhyfly@zhyfly:~/bash$ sed -e '/\$/s/h/ooooo/g' test.txt Toooooe ooooooneysuckle band played all nigooooot long for only $90. It was an evening of splendid music and company. Too bad the disco floor fell through at 23:00. The local nurse Miss P.Neave was in attendance. zhyfly@zhyfly:~/bash$ sed -e '/^It/,/23:00/s/l/ooooo/g' test.txt The honeysuckle band played all night long for only $90. It was an evening of spoooooendid music and company. Too bad the disco fooooooor feoooooooooo through at 23:00. The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ cat test.txt The honeysuckle band played all night long for only $90. It was an evening of splendid music and company. Too bad the disco floor fell through at 23:00. The local nurse Miss P.Neave was in attendance. zhyfly@zhyfly:~/bash$ sed -e 's/$90/&230/g' test.txt The honeysuckle band played all night long for only $90230. It was an evening of splendid music and company. Too bad the disco floor fell through at 23:00. The local nurse Miss P.Neave was in attendance. zhyfly@zhyfly:~/bash$ sed -e 's/90/120,&/g' test.txt The honeysuckle band played all night long for only $120,90. It was an evening of splendid music and company. Too bad the disco floor fell through at 23:00. The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ echo $PWD /home/zhyfly/bash zhyfly@zhyfly:~/bash$ echo $PWD|sed -e 's:/:@:g' @home@zhyfly@bash
zhyfly@zhyfly:~/bash$ cat test This is what I meant. zhyfly@zhyfly:~/bash$ sed -e 's/<.*>//g' test This meant.
zhyfly@zhyfly:~/bash$ sed -e 's/<[^>]*>//g' test This is what I meant.
zhyfly@zhyfly:~/bash$ echo "hello\hh\haha" hello\hh\haha zhyfly@zhyfly:~/bash$ echo "hello\hh\haha"|sed -e 's/\\/'/g' > > bash: unexpected EOF while looking for matching `'' bash: syntax error: unexpected end of file zhyfly@zhyfly:~/bash$ echo "hello\hh\haha"|sed -e 's/\\/\'/g' > > bash: unexpected EOF while looking for matching `'' bash: syntax error: unexpected end of file
zhyfly@zhyfly:~/bash$ echo "hello\hh\haha"|sed -e 's/\\/'"'"'/g' hello'hh'haha zhyfly@zhyfly:~/bash$
[:alnum:] 字母数字 [a-z A-Z 0-9] [:alpha:] 字母 [a-z A-Z] [:blank:] 空格或制表键 [:cntrl:] 任何控制字符 [:digit:] 数字 [0-9] [:graph:] 任何可视字符(无空格) [:lower:] 小写 [a-z] [:print:] 非控制字符 [:punct:] 标点字符 [:space:] 空格 [:upper:] 大写 [A-Z] [:xdigit:] 十六进制数字 [0-9 a-f A-F]
/^\([0-9]*\)\([A-Z]*\)\([0-9]*\)/\1 \2 \3/
zhyfly@zhyfly:~/bash$ cat file1.txt 1C2 1C3 1C31 1C32 1C4 2C3 2C4 1D1 1D10 1D12 1D2 1D3 1D31 1RC2 1RC20 1RC21 1RC3 1RC31 1WR1 1WR2 1WR20 1WR21 1WR23
zhyfly@zhyfly:~/bash$ sed -e 's/^\([0-9]*\)\([A-Z]*\)\([0-9]*\)/\1 \2 \3 /g' file1.txt 1 C 2 1 C 3 1 C 31 1 C 32 1 C 4 2 C 3 2 C 4 1 D 1 1 D 10 1 D 12 1 D 2 1 D 3 1 D 31 1 RC 2 1 RC 20 1 RC 21 1 RC 3 1 RC 31 1 WR 1 1 WR 2 1 WR 20 1 WR 21 1 WR 23
zhyfly@zhyfly:~/bash$ sed -e 's/^\([0-9]*\)\([A-Z]*\)\([0-9]*\)/\1 \2 \3 /g' file1.txt|sort +1 -2 +2n +0 -1 1 C 2 1 C 3 2 C 3 1 C 4 2 C 4 1 C 31 1 C 32 1 D 1 1 D 2 1 D 3 1 D 10 1 D 12 1 D 31 1 RC 2 1 RC 3 1 RC 20 1 RC 21 1 RC 31 1 WR 1 1 WR 2 1 WR 20 1 WR 21 1 WR 23
/-\([0-9]\)-/-0\1-/ #月 /-\([0-9]\) /-0\1 / #日
zhyfly@zhyfly:~/bash$ cat file.txt 123456 345678 2005-05-06 123456 123456 234567 2003-5-6 234567 345555 987644 2003-4-23 543333 555555 999999 2004-11-5 999999 zhyfly@zhyfly:~/bash$ sed -e 's/-\([0-9]\)-/-0\1-/g' -e 's/-\([0-9]\) /-0\1 /g' file.txt 123456 345678 2005-05-06 123456 123456 234567 2003-05-06 234567 345555 987644 2003-04-23 543333 555555 999999 2004-11-05 999999 zhyfly@zhyfly:~/bash$ sed -e 's/-\([0-9]\)-/-0\1-/g;s/-\([0-9]\) /-0\1 /g' file.txt 123456 345678 2005-05-06 123456 123456 234567 2003-05-06 234567 345555 987644 2003-04-23 543333 555555 999999 2004-11-05 999999 zhyfly@zhyfly:~/bash$ sed -e :a -e 's/-\([0-9]\)\([- ]\)/-0\1\2/;ta' file.txt 123456 345678 2005-05-06 123456 123456 234567 2003-05-06 234567 345555 987644 2003-04-23 543333 555555 999999 2004-11-05 999999
zhyfly@zhyfly:~/bash$ sed -e '2,3s/a/ooooo/g' -e '2,3s/d/ddddd/g' test.txt The honeysuckle band played all night long for only $90. It wooooos ooooon evening of splendddddiddddd music ooooonddddd compooooony. Too boooooddddd the dddddisco floor fell through ooooot 23:00. The local nurse Miss P.Neave was in attendance. zhyfly@zhyfly:~/bash$ sed -e '2,3s/a/ooooo/g;2,3s/d/ddddd/g' test.txt The honeysuckle band played all night long for only $90. It wooooos ooooon evening of splendddddiddddd music ooooonddddd compooooony. Too boooooddddd the dddddisco floor fell through ooooot 23:00. The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ cat sub.sed #!/bin/sed -f 2,3s/a/ooooo/g 2,3s/d/ddddd/g zhyfly@zhyfly:~/bash$ sudo chmod +x sub.sed zhyfly@zhyfly:~/bash$ cat test.txt The honeysuckle band played all night long for only $90. It was an evening of splendid music and company. Too bad the disco floor fell through at 23:00. The local nurse Miss P.Neave was in attendance. zhyfly@zhyfly:~/bash$ ./sub.sed test.txt The honeysuckle band played all night long for only $90. It wooooos ooooon evening of splendddddiddddd music ooooonddddd compooooony. Too boooooddddd the dddddisco floor fell through ooooot 23:00. The local nurse Miss P.Neave was in attendance. zhyfly@zhyfly:~/bash$ sed -f sub.sed test.txt The honeysuckle band played all night long for only $90. It wooooos ooooon evening of splendddddiddddd music ooooonddddd compooooony. Too boooooddddd the dddddisco floor fell through ooooot 23:00. The local nurse Miss P.Neave was in attendance.