Extracting data from text file after 2 conditions have been met

北慕城南 提交于 2019-12-25 07:51:53

问题


I'm working on a bash script at the moment which extracts data from a text file called carslist.txt, which each car (and its corresponding characteristics) being on separate lines. I've been able to extract and save data from the text file after it's met a single condition (below for example) but I can't figure out how to do it for two conditions.

Single condition example:

grep 'Vauxhall' $CARFILE > output/Vauxhall_Cars.txt

output:

Vauxhall:Vectra:1999:White:2
Vauxhall:Corsa:1999:White:5
Vauxhall:Cavalier:1995:White:2
Vauxhall:Nova:1994:Black:8

From the examples above, how would I extract data if I wanted the conditions Vauxhall and White to be met before extracting them?

the grep example above asks for Vauxhall to be met before pulling and saving the data, but I have no idea how to do it for 2. I've tried pipelining the command as Vauxhall | White but after that I was out of ideas.

Thanks in advance.


回答1:


I would recommend to use awk, like this:

awk -F: '$1=="Vauxhall" && $4=="White"' input.file

As I'm using : as the field separator, I simply need to check the values of field 1 and 4.



来源:https://stackoverflow.com/questions/32050761/extracting-data-from-text-file-after-2-conditions-have-been-met

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!