2条件得到满足后,提取文本文件数据 [英] Extracting data from text file after 2 conditions have been met
问题描述
我工作的一个bash脚本在从 carslist.txt
称为一个文本文件,其中每节车厢(及其相应的特征)正对中提取数据的时刻单独的行。我已经能够提取并从文本文件保存数据它遇到了一个单一的条件(以下举例)之后,但我无法弄清楚如何做到这一点的两个条件。
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.
单条件例如:
的grep'Vauxhall的'$ CARFILE>输出/ Vauxhall_Cars.txt
输出:
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?
grep的例子上述要求沃克斯豪尔
拉动和保存数据之前必须满足,但我不知道该怎么做了。我已经试过流水线命令为沃克斯豪尔|白
但在那之后我的想法。
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.
先谢谢了。
推荐答案
我会建议使用 AWK
,就像这样:
I would recommend to use awk
, like this:
awk -F: '$1=="Vauxhall" && $4=="White"' input.file
由于我使用:
作为字段分隔符,我只需要检查场1和4的值
As I'm using :
as the field separator, I simply need to check the values of field 1 and 4.
这篇关于2条件得到满足后,提取文本文件数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!