sed修改文件的内容
前面我们提了sed如何删除空白行,
但是删除只是stdin的内容,然后输出到stdout,并没有影响到文件,如果我们想直接使用sed修改文件的内容,怎么做呢?使用 sed的 -i参数即可。
下面是演示的例子
1 [xxx@localhost work]$ cat test.txt | sed '/^$/d'
2 here you got first line
3 second line here
4 aove space line
5 [xxx@localhost work]$ cat test.txt
6 here you got first line
7 second line here
8 aove space line
9 [xxx@localhost work]$ sed '/^$/d' test.txt
10 here you got first line
11 second line here
12 aove space line
13 [xxx@localhost work]$ cat test.txt
14 here you got first line
15 second line here
16 aove space line
17 [xxx@localhost work]$ sed '/^$/d' -i test.txt
18 [xxx@localhost work]$ cat test.txt
19 here you got first line
20 second line here
21 aove space line
22 [xxx@localhost work]$
不过我们也通过另外一种方法,将stdout重定向到一个文件,这样就不需要 -i 这个参数了。
[leo@localhost work]$ cat temp.txt | sed 's/leo/xxx/g' > temp1.txt
版权所有,禁止转载. 如需转载,请先征得博主的同意,并且表明文章出处,否则按侵权处理.