参考了
http://www.chinaunix.net/jh/24/307045.html sed
http://bbs.chinaunix.net/viewthread.php?tid=316482 awk
这两篇文章,感觉关于Sed还有东西可以写.顺便考察考察自己的Sed水平.
增加了<<sed 与 unix 命令等价代码鉴赏>>中没有的命令, 对其中大部
分原有命令做了修改。有些写的不好,水平有限,难免有错误,大家不要见笑
哈.欢迎大家批评指正:mrgreen:
Basic Command
basename sed 's/(.*)/([^/]*)/2/' or sed 's,.*/,,'
cat sed '' or sed -n '1,$p' or sed '1,$!d'
cat -s sed '/./,/^$/!d'
cat -n sed '=' | sed 'N;s/ / /;s/^/ &/' or sed '=' | sed '$!N;s/ / /'
cat -E sed 's/$/$/'
cat -t sed 's/ /^I/g'
cut -c n sed 's/(.){n}.*/1/' or sed 's/^.{(n-1)}//g;s/(.)(.*)/1/g'
cut -c x-y sed 's/(^.{y})(.*)/1/g;s/^.{(x-1)}//'
cut -d| -f6 sed 's/(([^|]*)|){6}.*/2/'
cp file1 file2 sed 'w file2' file1
expand -t 1 sed 's/ / /g'
dirname sed 's/(.*)/([^/]*)/1/' or sed 's,[^/]*$,,'
grep patten sed -n '/patten/p' or sed '/patten/!d'
grep -v patten sed -n '/patten/!p' or sed '/pateen/d'
grep -n patten sed -n '/patten/{=;p}'| sed 'N;s/ /:/'
head sed -n '1,10p'
head -1 sed -n '1p' or sed 'q'
head -Number sed '1,Number!d' or sed 'Numberq'
paste -s file1 file2 sed ':a;N;s/ / /;ba;' file1 file2 | sed 's/ / /'
paste -sdstr sed ':a;N;s/ /str/;ba'
rev sed '/ /!G;s/(.)(.* )/&21/;//D;s/.//'
tac sed -n '1! G;$p;h' or sed -n 'G;$p;h'
tail -1 sed -n '$p' or sed '$!d'
tail -Number sed ':t;$q;N;(Number+1),$D;bt'
tail -f sed -u '/./!d'
tr " " " " sed ':a;N;s/ / /;ba'
tr "A-Z" "a-z" sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'
tr "a-z" "A-Z" sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'
tr -d " 12" sed ':a;N;s/ //;ba' or sed ':a;N;s/(^.)* (.*)/21/;ba'
tr -s 'x' sed 's/(x)(x{1,})/1/'
tr -s ' ' sed 's/ +/ /g'
uniq -u sed '$b;N;/^(.*) 1$/ ! {P;D};:c;$d;s/.* //;N;/^(.*) 1$/{bc};D'
uniq sed 'N;/^(.*) 1$/!P;D'
wc -l sed -n '$='
wc -c sed ':a;s/./& /;P;D;/.{2,} /ba' t|sed -n '$='
wc -w sed 's/ / /g' | sed -n '$='
xargs sed ':a;N;s/ / /;ba' or sed -e ':a' -e '$!N;s/ / /;ta'
Gawk and Others
awk -F':' '{print $1}' sed 's/([^:]*):(.*)/1/'
awk -F':' '{print $2}' sed 's/([^:]*):([^:]*):([^:]*)/2/'
awk -F':' '{print $n}' sed 's/(([^:]*):){n}.*/2/'
awk -F':' '{print $NF}' sed 's/(.*):([^:]*)/2/'
awk -F':' '/patten/{print $n}' sed '/patten/!d' | sed 's/(([^:]*):){n}.*/2/'
awk 'NR>=x && NR=<y' sed '/x,y/!d'
awk 'NR%2' sed -n '/./{p};N'
awk 'NR%2==0' sed '1~2d'
awk 'NR%3==0' sed -n '0~3p'
awk 'NR%n==0' sed -n '0~np'
expr length str sed ':a;s/./& /;P;D;/.{2,} /ba'|sed '$d'|sed -n '$='
expr index str patten sed ':a;s/./& /;P;D;/.{2,} /ba'|sed -n '/patten/='|sed '1!d'
expr 2 + 3 sed 's/+//;s/2/**/;s/3/***/;'|sed ':a;s/./& /;P;D;/.{2,} /ba'|sed '$d'|sed -n '$='
echo something sed = /something 2>&1 |sed 's/: No.*//;s/^.*read ///;s/ //'
echo ${x::n} sed 's/(^.{n})(.*)/1/'
echo ${#x} sed ':t;s/./& /;P;D;/.{1,} /bt'|sed '$d'|sed -n '$='
whoami cd;pwd | sed 's,.*/,,'
>file (empyt file) sed 'w file' file
man somthing sed '/.B/{:a;N;s/ / /;/^.B/!bt;:t;N;s/ / /;/^.SH/ba};/.SH/{s/.SH/ /;s/"//g;s/^ //g};/.TP/{s/.TP//;n;s/^/ /;};s/\fB\-\-/--/;s/\fR//g;s/\fB\//g;s/\fI//g;s/.B//g;s/.TH/ /;s/.PP/ /;/^.\/d;s/.br//;;
s/^W/ &/'|sed '/([A-Z]){3,}/{s/^ //;n;s/^/ /}' ( only generated by help2man 写了个查看man文档的,不理想)
还有些命令,由于很难用Sed实现,还要请大家来帮忙:
:em02::em02:
1 paste file1 file2 and paste -d file1 file2
2 join file1 file2
3 fmt
4 pr file
5 clear
6 yes
7 man
8 comm
9 diff file1 file2
10 ptx file
11 fold
http://www.chinaunix.net/old_jh/24/653857.html
http://www.chinaunix.net/old_jh/24/307045.html