sed命令实现多行复制

简介:

以nginx.conf为例。文件原内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@RS1 conf]# cat nginx.conf
worker_processes  1;
events {
     worker_connections  1024;
}
http {
     include       mime.types;
     default_type  application/octet-stream;
     sendfile        on;
     keepalive_timeout  65;
     server {
         listen       80;
         server_name  localhost;
         location / {
             root   html;
             index  index.html index.htm;
         }
         error_page   500 502 503 504  /50x.html;
         location = /50x.html {
             root   html;
         }
     }
}

实现复制多个server容器

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
sed -rn  'p;10,21H;21{g;p};21{g;s/^\n//;p}'  nginx.conf
sed -rni  'p;10,21H;21{g;p};21{g;s/^\n//;p}'  nginx.conf
[root@RS1 conf]# cat nginx.conf
worker_processes  1;
events {
     worker_connections  1024;
}
http {
     include       mime.types;
     default_type  application/octet-stream;
     sendfile        on;
     keepalive_timeout  65;
     server {
         listen       80;
         server_name  localhost;
         location / {
             root   html;
             index  index.html index.htm;
         }
         error_page   500 502 503 504  /50x.html;
         location = /50x.html {
             root   html;
         }
     }
     
     server {
         listen       80;
         server_name  localhost;
         location / {
             root   html;
             index  index.html index.htm;
         }
         error_page   500 502 503 504  /50x.html;
         location = /50x.html {
             root   html;
         }
     }
     server {
         listen       80;
         server_name  localhost;
         location / {
             root   html;
             index  index.html index.htm;
         }
         error_page   500 502 503 504  /50x.html;
         location = /50x.html {
             root   html;
         }
     }
}

参考:

多行文本复制,如何用sed或awk或别的工具实现

有文本

1
2
3
4
5
aaa
bbb
ccc
ddd
eee

匹配bbb-ddd后复制,变为:

1
2
3
4
5
6
7
8
aaa
bbb
ccc
ddd
bbb
ccc
ddd
eee

使用命令为:

1
sed -rn  'p;/bbb/,/ddd/H;/ddd/{g;s/^\n//;p}'  file.txt

说明:

sed内部有两个空间,一个模式空间,一个保留空间。


通常sed将文本内容逐行读入模式空间进行处理,保留空间仅用于暂时保留内部数据用于与模式空间的数据交换。可以这么理解:模式空间用于与外部的数据交换,保留空间用于sed内部的数据交换,最终还是要通过模式空间输出。


/bbb/,/ddd/H;  逐行处理时将bbb~ddd区段的文本从sed的模式空间附加到保留空间内,每行内容之间以\n分割,因此,最终保留空间内容为:\nbbb\nccc\nddd

/ddd/{g;s/^\n//;p}  处理到ddd这行后,通过g命令获取保留空间内容到模式空间,通过s替换命令去除开头的\n,p命令打印。

 


sed除了可以将输出重定向到新文件外,加 -i 选项还可以直接改写原文件。


其他回答(此处暂时不理解)

1
awk  '{print $0;}n ~/1/{a=a"\n"$0;}/^bbb/{a=$0;n=1;}/^ddd/{print a;n=0;}'  file_name



本文转自 f_066 51CTO博客,原文链接: http://blog.51cto.com/ganmu/1913599 ,如需转载请自行联系原作者


相关文章
|
4月前
|
开发工具 Perl
使用sed去掉代码中的行号
使用sed去掉代码中的行号
使用sed去掉代码中的行号
|
4月前
|
容器
查看文件内容命令
查看文件内容命令
46 0
|
4月前
|
存储 程序员 开发工具
Vim:在系统剪贴板中复制和粘贴文本
【5月更文挑战第3天】
303 8
|
4月前
|
弹性计算 运维 Shell
向文件中追加内容
【4月更文挑战第29天】
40 2
|
4月前
|
开发工具
如何在 Vim 中剪切、复制和粘贴
如何在 Vim 中剪切、复制和粘贴