sed 命令基础
# sed 笔记
一、替换[s]
1. -e选项 sed -e 's/dog/DOG/;s/cat/CAT/' test.txt 2. -f选项 sed -f sed.txt test.txt 3. -n选项 sed -n sed 's/dog/DOG/' test.txt 4. 数学方式行寻址 sed '2,3s/cat/CAT/' test.txt sed '3,$s/cat/CAT/' test.txt 5. 模式过滤器 sed '/is dog/s/dog/DOG/' test.txt 6. 命令组合 sed '2{s/dog/CAT/;s/pig/PIG/}' test.txt
二、全局匹配[g]
1. sed 's/dog/DOG/g' test.txt 2. sed 's/dog/DOG/3' test.txt
三、打印[p]
1. sed 's/dog/DOG/p' test.txt 2. sed -n '/line 4/{=;s/4/9p}' line.txt
四、写入文件[w]
1. sed 's/dog/DOG/w file.txt' test.txt
五、删除[d]
1. sed '2d' test.txt 2. sed '2, $d' test.txt 3. sed '/line 1/d' line.txt
六、插入[i,a]
1, sed '2i\this is insert line' line.txt 2. sed '$a\this is insert line' line.txt
七、修改[c]
1. sed '2c\this is update line' line.txt 2. sed '/line 4/c\this is update line' linet.txt
八、转换[y]
1. sed 'y/123/987/' line.txt
九、文件读[r]
1. sed '/line 3/r test.txt' line.txt 2. sed '/line 3/{r test.txt;d}' line.txt
# gawk 笔记
一、 打印[print]
1. gawk '{print "hello world"}' 2. gawk -F: '{print $1 "home is " $6}' /etc/passwd 3. gawk -F: -f test.awk /etc/passwd 4. gawk -F: '{$1=leng;print $0}' /etc/passwd
二、处理前后[BEGIN:END]
1. gawk 'BEGIN {print "this is file content"} {print $0} END{print "end file"}'