【问题标题】:copy contents of a file starting from regex word to end of the file将从正则表达式单词开始的文件内容复制到文件末尾
【发布时间】:2012-08-29 21:38:20
【问题描述】:

我想复制从单词(搜索的单词/行)开始的文件内容到文件末尾。

示例: 文件1:

this is a sample text for file1 in PA
this is a sample text for file1 in CA
this is a sample text for file1 in CT
this is a sample text for file1 in IL
this is a sample text for file1 in MI
end of the file.

我想将内容从具有“CT”的行复制到文件2,直到文件末尾。 输出 : 文件2

this is a sample text for file1 in CT
this is a sample text for file1 in IL
this is a sample text for file1 in MI
end of the file.

【问题讨论】:

  • 如果我想从两个文件中获取内容并将其复制到一个输出文件中怎么办?

标签: linux file bash shell copy


【解决方案1】:

你可以使用sed:

sed -n '/searchtext/,$p' file1 > file2

awk:

awk '/searchtext/ {flag=1} flag;' file1 > file2

【讨论】:

    【解决方案2】:
    awk '{if($0~/CT/)p=1;if(p)print}' your_file
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-01
      • 1970-01-01
      相关资源
      最近更新 更多