2012年2月4日星期六

awk & sed命令总结


awk

file          整个文件
record     文件中一行转为一个record,awk每次处理一个record
field        record由field组成,如$1,$2,$0表示真个record

例子:
file s.c
1 2
3 4
awk '{d=($1 + $2); print d}' s.c
输出:
3
7

awk匹配模式从流中提取自己需要的内容:
awk -F "\"" '{printf "msgid ""\""$2"\"""\n""msgstr \"\"\n\n"}' pc_err.c

在线资料
http://sed.sourceforge.net/sed1line_zh-CN.html



sed(stream editor)
commands = pattern + action.
$          matches the end of line
^          matches the beginning of the line


sed -e '1,10d'
删除stdin输入中的前10行,并将其余输出stdout
-e          把 '1,10d'当作sed语句来执行
1,10       pattern
d            action

sed -n -e '/1/p' z.c
输出z.c中匹配1的行
-n     不输出test.txt原文的内容

sed -e '3,$d'
sed -n -e '1,2p'

sed -e '2q'
$表示文本的最后一样,d表示delete
q表示quit
输出流的前两行

sed -e '1,2d' -e '4,5d' z.c
同时指定多个-e

grep 'foo' log | grep -v 'debug' == sed -n -e '/debug/d' -e '/foo/p' log

sed -e 's/PC_NO_MATCH) goto ret;/PC_NO_MATCH) {ret = PC_ERR_RECORD_REPEAT; goto ret;}/' -i *.c

sed -e 's/str1/str2/' -i  *.c
把当前目录下所有c文件中的str1替换为str2

在线资料
http://www.tsnc.edu.cn/default/tsnc_wgrj/doc/abs-3.9.1_cn/html/sedawk.html











没有评论:

发表评论