sed和gawk
1 sed编辑器
在命令行定义编辑器命令
[root@iZbp11f8g5h7oozejqy6k6Z ~]# echo "this is a test" | sed 's/test/big test/' this is a big test
[root@iZbp11f8g5h7oozejqy6k6Z test3]# sed 's/dog/cat/' data1.txt the quick brown fox jumps over the lazy cat. the quick brown fox jumps over the lazy cat. the quick brown fox jumps over the lazy cat. the quick brown fox jumps over the lazy cat. the quick brown fox jumps over the lazy cat. [root@iZbp11f8g5h7oozejqy6k6Z test3]# cat data1.txt the quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog.
这边可以看到sed编辑器并不会修改文本文件的数据,只会将修改后的数据发送到STDOUT
要在sed命令行上处理多个命令,使用-e选项即可
[root@iZbp11f8g5h7oozejqy6k6Z test3]# sed 's/dog/cat/; s/lazy/notlazy/' data1.txt the quick brown fox jumps over the notlazy cat. the quick brown fox jumps over the notlazy cat. the quick brown fox jumps over the notlazy cat. the quick brown fox jumps over the notlazy cat. the quick brown fox jumps over the notlazy cat.
如果有大量的sed命令需要处理,可以将命令放入到文件中
[root@iZbp11f8g5h7oozejqy6k6Z test3]# sed -f script1.sed data1.txt the quick green elephant jumps over the lazy cat. the quick green elephant jumps over the lazy cat. the quick green elephant jumps over the lazy cat. the quick green elephant jumps over the lazy cat. the quick green elephant jumps over the lazy cat. [root@iZbp11f8g5h7oozejqy6k6Z test3]# cat script1.sed s/brown/green/ s/fox/elephant/ s/dog/cat/
2 gawk程序
$0 代表整个文本行
$1 代表文本中第1个数据字段
$2 代表文本中第2个数据字段
$n 代表文本中第n个数据字段
-F可以指定分隔符
[root@iZbp11f8g5h7oozejqy6k6Z test3]# cat test.txt | gawk -F: '{print $1}' root bin daemon adm lp sync shutdown
在程序脚本中使用多个命令
[root@iZbp11f8g5h7oozejqy6k6Z test3]# echo "this is a dog" | gawk '{$4="cat";print $0}' this is a cat
从文件中读取程序
[root@iZbp11f8g5h7oozejqy6k6Z test3]# cat script2.gawk {print $1 "'s home directory is " $6} [root@iZbp11f8g5h7oozejqy6k6Z test3]# gawk -F: -f script2.gawk test.txt root's home directory is /root bin's home directory is /bin daemon's home directory is /sbin adm's home directory is /var/adm lp's home directory is /var/spool/lpd sync's home directory is /sbin
[root@iZbp11f8g5h7oozejqy6k6Z test3]# cat script3.gawk BEGIN{ print "test" print "UserID \t Shell" print "------ \t ------" FS=":" } { print $1 " \t" $7 } END{ print "end..." } [root@iZbp11f8g5h7oozejqy6k6Z test3]# gawk -f script3.gawk test.txt test UserID Shell ------ ------ root /bin/bash bin /sbin/nologin daemon /sbin/nologin adm /sbin/nologin lp /sbin/nologin sync /bin/sync shutdown /sbin/shutdown halt /sbin/halt mail /sbin/nologin operator /sbin/nologin games /sbin/nologin