简体   繁体   English

sed或awk - 如果找不到表达式,则从文件中删除行

[英]sed or awk - remove lines from file if expression not found

I have a file that contains multiple entries. 我有一个包含多个条目的文件。 the entries are seperated by -- I want to remove all lines between this pattern if a certain phrase is not found within. 条目分开 - 如果在其中找不到某个短语,我想删除此模式之间的所有行。 Example: 例:

--
Company
Street 
Zip Code
Country
--

If Country is not found between -- and -- remove the complete block 如果在 - 和 - 之间找不到Country,则删除完整块

Thanks in advance :) 提前致谢 :)

你可以这样做:

awk '!/Country/' RS='--\n' ORS='--\n'

Perhaps the following will be helpful: 也许以下内容会有所帮助:

use strict;
use warnings;

local $/ = '--';
print $/;

while (<>) {
    print if /\bCountry\b/;
}

Usage: perl script.pl dataFile [>outputFile] 用法: perl script.pl dataFile [>outputFile]

Data: 数据:

--
Company
Street 
Zip Code
Elephant
--
Company
Street 
Zip Code
Country
--
Company
Street 
Zip Code
Goat
--
Company
Street 
Zip Code
Country
--

Output: 输出:

--
Company
Street 
Zip Code
Country
--
Company
Street 
Zip Code
Country
--

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM