参考了
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/\n/\t/;s/^/ &/' or sed '=' | sed '$!N;s/\n/ /' cat -E sed 's/$/\$/' cat -t sed 's/\t/^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/\t/ /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/\n/:/' 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/\n/\t/;ba;' file1 file2 | sed 's/\t\t/\n/' paste -sdstr sed ':a;N;s/\n/str/;ba' rev sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//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 "\n" " " sed ':a;N;s/\n/ /;ba' tr "A-Z" "a-z" sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' tr "a-z" "A-Z" sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'
tr -d "\012" sed ':a;N;s/\n//;ba' or sed ':a;N;s/\(^.\)*\n\(.*\)/\2\1/;ba' tr -s 'x' sed 's/\(x\)\(x\{1,\}\)/\1/' tr -s ' ' sed 's/ \+/ /g' uniq -u sed '$b;N;/^\(.*\)\n\1$/ ! {P;D};:c;$d;s/.*\n//;N;/^\(.*\)\n\1$/{bc};D' uniq sed 'N;/^\(.*\)\n\1$/!P;D' wc -l sed -n '$=' wc -c sed ':a;s/./&\n/;P;D;/.\{2,\}\n/ba' t|sed -n '$=' wc -w sed 's/ /\n/g' | sed -n '$=' xargs sed ':a;N;s/\n/ /;ba' or sed -e ':a' -e '$!N;s/\n/ /;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/./&\n/;P;D;/.\{2,\}\n/ba'|sed '$d'|sed -n '$=' expr index str patten sed ':a;s/./&\n/;P;D;/.\{2,\}\n/ba'|sed -n '/patten/='|sed '1!d' expr 2 + 3 sed 's/+//;s/2/**/;s/3/***/;'|sed ':a;s/./&\n/;P;D;/.\{2,\}\n/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/./&\n/;P;D;/.\{1,\}\n/bt'|sed '$d'|sed -n '$=' whoami cd;pwd | sed 's,.*/,,' >file (empyt file) sed 'w file' file man somthing sed '/\.B/{:a;N;s/\n/ /;/^\.B/!bt;:t;N;s/\n/ /;/^\.SH/ba};/\.SH/{s/\.SH/\n/;s/"//g;s/^ //g};/\.TP/{s/\.TP//;n;s/^/\t/;};s/\\fB\\-\\-/--/;s/\\fR//g;s/\\fB\\//g;s/\\fI//g;s/\.B//g;s/\.TH/\t\t/;s/\.PP/\n\t/;/^\.\\/d;s/\.br//;; s/^\W/\t&/'|sed '/\([A-Z]\)\{3,\}/{s/^ //;n;s/^/\t/}' ( 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.htmlhttp://www.chinaunix.net/old_jh/24/307045.html
时间: 2024-10-09 18:30:52