user2025805
user2025805

Reputation: 21

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

Thanks in advance :)

Upvotes: 0

Views: 564

Answers (2)

Kenosis
Kenosis

Reputation: 6204

Perhaps the following will be helpful:

use strict;
use warnings;

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

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

Usage: 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
--

Upvotes: 1

Thor
Thor

Reputation: 47099

You can do it like this:

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

Upvotes: 1

Related Questions