Search code examples
awkmultiline

Awk multiline match, print output separated by newline


I am following the example How can I process multi-line records with awk in a bash script

My input is:

Restaurant: Chik-Fil-A 
City: Columbus
State: GA
Address: 123 Biscayne Blvd
Phone: 911

Restaurant: 5 guys
City: Columbus
State: GA
Address: 123 Peachtree Rd
Phone: 911

Restaurant: KFC
City: NYC
State: NY
Address: 123 Madison Square
Phone: 911

Restaurant: Bahama Breeze
City: Orlando
State: FL
Address: 123 Madison Square
Phone: 911

I want to select all restaurants in Columbus:

awk -v name="Columbus" -v RS="" '$0 ~ "City: " name' file.txt

My output is this:

Restaurant: Chik-Fil-A 
City: Columbus
State: GA
Address: 123 Biscayne Blvd
Phone: 911
Restaurant: 5 guys
City: Columbus
State: GA
Address: 123 Peachtree Rd
Phone: 911

What I would like is this:

Restaurant: Chik-Fil-A 
City: Columbus
State: GA
Address: 123 Biscayne Blvd
Phone: 911

Restaurant: 5 guys
City: Columbus
State: GA
Address: 123 Peachtree Rd
Phone: 911

I would like each output record to be separated by a newline. I've tried playing with FS and ORS but I keep on getting the same result. I would really appreciate any help.

Thanks,


Solution

  • You can set ORS to two newlines to obtain the desired output:

    awk -v name="Columbus" -v ORS="\n\n" -v RS="" '$0 ~ "City: " name' file.txt