简体   繁体   English

如何从多行标准输入中仅提取特定的字符串匹配行

[英]How to extract only specific string matching lines from a multiple lines standard input

I have a standard input piped from another bash function output in the sample form: 我有一个从示例形式的另一个bash函数输出中传递过来的标准输入:

fruit=apple
dog=doberman
car=bmw
flower=tulip
---
fruit=orange
dog=corgy
car=ford
flower=rose
---

...and so on repeated thousands of times... ...如此反复数千次...

From this multiple repeating piped standard output I want to extract only the values matching say fruit and car, like so: 从这个重复的管道标准输出中,我只想提取与诸如水果和汽车匹配的值,如下所示:

apple
bmw
---
orange
ford
---

It would be even better if the sed/awk magic can output those in csv table-like format, like so: 如果sed / awk魔术可以以类似于csv表的格式输出那些内容,那将更好,例如:

apple,bmw
orange,ford

I have only basic knowledge in bash string input manipulation magic, so any help would be appreciated. 我只有bash字符串输入操作魔术方面的基础知识,因此将不胜感激。

No magic required - save the values in an array when they're seen in the input and print them when you hit a --- line: 无需任何技巧-在输入中看到值时将其保存在数组中,并在按下---行时将其打印出来:

$ awk -F'=' -v OFS=',' '/---/{print v["fruit"], v["car"]} {v[$1]=$2}' file
apple,bmw
orange,ford

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

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