sed搜索某行在行末追加_示范sed指定某行插入 追加和全局替换
有時候會有這樣的需求,在指定的行后面或者是前面追加一行,這個時候可以使用sed來完成,具體用法如下
a\ 在指定的行后面追加一行
b\ 在指定的行前面追加一行
使用指定的行號追加內容,在使用行號的過程中,需要注意的問題有以下
N;后面只能使用偶數,且不可以為0
a表示在指定的行后面追加一行
i表示在當前行插入一行,如果指定行為4,其實最終的結果插入行的位置是第三行。
sed -i 'N;2a\newline' 1.txt
sed -i 'N;2i\newline' 1.txt
[root@lanmp shell]# cat << eof > 1.txt
> a
> b
> c
> d
> eof
[root@lanmp shell]# sed -i 'N;2a2222' 1.txt
[root@lanmp shell]# cat 1.txt
a
b
2222
c
d
[root@lanmp shell]# sed -i 'N;2i2222' 1.txt
[root@lanmp shell]# cat 1.txt
2222
a
b
2222
c
d
[root@RS2 shell]# cat 1.txt
1111
3333
[root@RS2 shell]# sed -i '/^1111$/a\2222' 1.txt ; cat 1.txt
1111
2222
3333
[root@RS2 shell]# sed -i '/^1111$/i\0000' 1.txt ; cat 1.txt
0000
1111
2222
3333
下面是把所有匹配的字符都替換為指定的字符
[root@SLAVE ~]# cat << eof > 1.txt
> 1111
> 222333333
> 44444444445
> eof
[root@SLAVE ~]# sed -i 2{s/2/3/g} 1.txt
[root@SLAVE ~]# cat 1.txt
1111
333333333
44444444445
[root@SLAVE ~]# sed -i 3{s/4/5/g} 1.txt
[root@SLAVE ~]# cat !$
cat 1.txt
1111
333333333
55555555555
總結
以上是生活随笔為你收集整理的sed搜索某行在行末追加_示范sed指定某行插入 追加和全局替换的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: netcore docker_让.Net
- 下一篇: 安卓apk签名提取工具_Android测
