首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在模式匹配之间将文件A的内容添加到B文件中

如何在模式匹配之间将文件A的内容添加到B文件中
EN

Unix & Linux用户
提问于 2020-03-20 04:33:03
回答 1查看 98关注 0票数 0

我有两份文件:

这是文件A的内容:

代码语言:javascript
复制
etc...
this is a test file having \@ and \# and \$ as well
looking for awk or sed solution to print this content in another file between the matching pattern
etc....

这是B文件的内容:

代码语言:javascript
复制
file-B begin 
this is a large file containing many patterns like 
pattern-1 
pattern-2 
pattern-2 
pattern-3 
pattern-2 
pattern-4 
file-B end 

我希望B文件的输出如下:

代码语言:javascript
复制
file-B begin 
this is a large file containing many patterns like 
pattern-1 
pattern-2 
pattern-2 
etc... 
this is a test file having \@ and \# and \$ as well 
looking for awk or sed solution to print this content in another file between the matching pattern 
etc.... 
pattern-3 
pattern-2 
pattern-4 
file-B end 

我想在文件B的模式-2和模式-3之间打印A文件的内容。

目前,我使用的是:

代码语言:javascript
复制
awk '/pattern-2/{c++;if(c==2){printf $0; print "\n"; while(getline line<"file-A"){print line};next}}1' file-B

它工作得很好,但是,我需要一些东西来搜索这两种模式,然后在它们之间放置另一个文件内容。

EN

回答 1

Unix & Linux用户

发布于 2020-03-20 05:14:13

代码语言:javascript
复制
awk -v file="file-A" '
  last=="pattern-2" && $0=="pattern-3"{
    while ((getline line < file)>0) print line
    close(file)
  }
  {last=$0}1
' file-B

或者,如果您使用regex模式而不是字符串,请使用以下内容

代码语言:javascript
复制
  last ~ /pattern-2/ && /pattern-3/{

在第二行。

票数 3
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/573871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档