sed -i 'ni\x' test.file 表示向test.file文件里的第n行的前面添加x内容
sed -i 'na\x' test.file 表示向test.file文件里的第n行的后面添加x内容
sed -i '/m/i\x' test.file 表示向test.file文件里匹配m字符串的行的前面添加x内容
sed -i '/m/a\x' test.file 表示向test.file文件里匹配m字符串的行的后面添加x内容
-i 表示in front,前面
-a 表示after,后面
比如向a.txt文件的首行添加123456789
# sed -i '1i\123456789' a.txt
比如向a.txt文件的第3行添加hhhhh
# sed -i '3a\hhhhh' a.txt
比如向a.txt文件匹配abcd字符串的行的前面添加66666
# sed -i '/abcd/i\66666' a.txt
比如向a.txt文件匹配1234字符串的行的后面添加hahaha
# sed -i '/1234/a\hahaha' a.txt
比如向/etc/puppet/puppet.conf文件中的第2行的前面添加" server=puppet01.test.cn"内容
然后再向第3行添加" runinterval = 600"内容
# /bin/sed -i '2i\ server=puppet01.test.cn' /etc/puppet/puppet.conf
# /bin/sed -i '3i\ runinterval = 600' /etc/puppet/puppet.conf
------------------------------------------------------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
取最后一个字符: awk '{print substr($0,length())}' filename
[root@localhost ~] # cat a
3G 32G 123G 2348G 123131G 123123123123123G [root@localhost ~] # awk '{print substr($0,length())}' a
G G G G G G [root@localhost ~] # awk -F"G" '{print $1}' a
3 32 123 2348 123131 123123123123123 |