awk 多行匹配,打印输出以换行符分隔

问题描述 投票:0回答:1

我正在按照示例如何在bash脚本中使用awk处理多行记录

我的输入是:

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

我想选择哥伦布的所有餐厅:

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

我的输出是这样的:

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

我希望每个输出记录都用换行符分隔。我尝试过使用 FS 和 ORS,但我一直得到相同的结果。我真的很感激任何帮助。

谢谢,

awk multiline
1个回答
1
投票

您可以将

ORS
设置为两个换行符以获得所需的输出:

awk -v name="Columbus" -v ORS="\n\n" -v RS="" '$0 ~ "City: " name' file.txt
© www.soinside.com 2019 - 2024. All rights reserved.