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 ,如需转载请自行联系原作者


相关文章
|
9月前
|
开发工具 Perl
使用sed去掉代码中的行号
使用sed去掉代码中的行号
使用sed去掉代码中的行号
奇怪的“可以复制 不能粘贴”问题
奇怪的“可以复制 不能粘贴”问题
|
7月前
|
缓存 Shell 开发工具
[oeasy]python024_vim读取文件_从头复制到尾_撤销_重做_reg_寄存器
[oeasy]python024_vim读取文件_从头复制到尾_撤销_重做_reg_寄存器
66 5
|
9月前
|
Windows
FastCopy文件快速复制
FastCopy文件快速复制工具。Windows平台上最快的文件复制、删除软件!功能强劲,性能优越!它是源于日本的高效文件复制加速软件,支持拖拽操作,三种不同HDD模式;支持通配符,任务管理/命令行
62 0
|
机器学习/深度学习 存储 监控
sed命令(超详细)
是一种流编辑器,它是文本处理中非常好的工具,能够完美的配合正则表达式使用,功能不同凡响。 处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命 令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复, 直到文件末尾。
238 0
shell遍历文件夹及去掉文件后缀名
shell遍历文件夹及去掉文件后缀名
XCOPY复制空目录的选项
XCOPY复制空目录的选项
118 0

热门文章

最新文章