# grep sed awk
# sed
# 命令
先 执行
brew install gnu-sed
gsed "2a 88888888888" testfile
在 testfile的第2行后写入hellogsed "2a 88888888888\n999999999" testfile
在 testfile的第2行后写入两行,分别是hello 行和 world行gsed "2i hello" testfile
在 testfile的第2行前写入hellogsed "2d" testfile
删除第2行gsed "2,4d" testfile
删除第 [2,4]行gsed "5,+4d" testfile
删除第 [5,9]行gsed "5,$d" testfile
删除第 5行之后的所有gsed "/Runoob/, /Wiki/ d"
删除匹配到Runoob行开始到 ,匹配到Wiki结束gsed "2c hello" testfile
把第二行内容替换为hellogsed "s#hello# word#" testfile
将内容hello替换为wordgsed -n 2p 1.txt
输出文件到第二行
1.txt
aaa
bbb
ccc
x=a
gsed -ie "s#$x#eee#g" 1.txt 把所有a替换为eee
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
gsed 正则分组
echo i am caozepeng teacher. | gsed "s/^.*am \([a-z].*\) tea.*$/\1/g"
1
1name copy 2.txt
2name copy 3.txt
3name copy 4.txt
4name copy 5.txt
5name copy 6.txt
ls *copy*.txt |gsed -r "s#(.*) copy [1-9].*#mv & \1.txt#g"
mv 1name copy 2.txt 1name.txt
mv 2name copy 3.txt 2name.txt
mv 3name copy 4.txt 3name.txt
mv 4name copy 5.txt 4name.txt
mv 5name copy 6.txt 5name.txt
mv 7name copy 2.txt 7name.txt
ls *copy*.txt |gsed -r "s#(.*) copy [1-9].*#mv \"&\" \1.txt#g"|bash
mv "1name copy 2.txt" 1name.txt
mv "2name copy 3.txt" 2name.txt
mv "3name copy 4.txt" 3name.txt
mv "4name copy 5.txt" 4name.txt
mv "5name copy 6.txt" 5name.txt
mv "7name copy 2.txt" 7name.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 备份
1.txt 文件内容是
mv "1name copy 2.txt" 1name.txt
mv "2name copy 3.txt" 2name.txt
1
2
2
执行 sed -i.brk "s#mv#rename#" 1.txt
后
1.txt变为以下
rename "1name copy 2.txt" 1name.txt
rename "2name copy 3.txt" 2name.txt
1
2
2
多出一个备份文件1.txt.brk
mv "1name copy 2.txt" 1name.txt
mv "2name copy 3.txt" 2name.txt
1
2
2
# 复制文件
# awk
ls | awk 'BEGIN{ FS=" ";OFS="," }{ print $1,$2 }'
ls | awk 'BEGIN{ FS=" ";OFS="","; }{ print """$1,$2 }'