[英]Using grep in shell file
I have a question about getting data in a file using a shell script. 我有一个关于使用Shell脚本在文件中获取数据的问题。 I want to search in some lines with more than one condition.
我想在某些行中搜索多个条件。
For example, if I have this file: 例如,如果我有此文件:
TX VIDEO ID 34 B 33 SIZE 672 SRC -1 DST 11 T 0.1 1
TX CBR ID 35 B 35 SIZE 10 SRC -1 DST 11 T 0.1 1
RX VOIP ID 0 B 0 SIZE 32 SRC -1 DST 3 D 0.001 0
RX VOIP ID 8 B 8 SIZE 32 SRC -1 DST 5 D 0.001 1
RX VOIP ID 20 B 20 SIZE 32 SRC -1 DST 8 D 0.001 1
RX VIDEO ID 9 B 9 SIZE 1490 SRC -1 DST 5 D 0.002 1
RX VIDEO ID 21 B 21 SIZE 1490 SRC -1 DST 8 D 0.002 1
TX INF_BUF ID 37 B 10 SIZE 776 SRC 1 DST 5 T 0.102 1
TX INF_BUF ID 39 B 22 SIZE 776 SRC 2 DST 8 T 0.102 1
In this file I have each of the "RX VIDEO"
and "TX VIDEO"
lines. 在此文件中,我分别具有
"RX VIDEO"
和"TX VIDEO"
行。 The first condition I want to take data from is all the "RX VIDEO"
within the "DST 3 && DST 5"
. 我要从中获取数据的第一个条件是
"DST 3 && DST 5"
所有"RX VIDEO"
"DST 3 && DST 5"
。
For more than one condition you can pipe greps output to another grep. 对于多个条件,您可以通过管道将输出输出到另一个grep。
If you want to search a file for lines with AAA and BBB you can do: 如果要在文件中搜索具有AAA和BBB的行,则可以执行以下操作:
grep 'AAA' file | grep 'BBB'
Or you can search it with regular expressions with only one grep: 或者,您也可以只用一个grep使用正则表达式进行搜索:
grep -e 'AAA.*BBB' -e 'BBB.*AAA' file
If an expressions after any of the -e
options are matched, you will get a result. 如果任何
-e
选项之后的表达式都匹配,则将得到结果。 That means that it acts like or . 这意味着它的行为类似于或 。
Both examples will find lines like this ones: 这两个示例都将找到类似以下内容的行:
AAABBB
AAA BBB
BBB AAA
BBBAAA
AAA-BBB
AAA---BBB
AAAA BBB
xAAAAAAA BBB AAAAA
and so on. 等等。
For your example it could be: 对于您的示例,它可能是:
grep 'RX VIDEO' file | grep -e 'DST 3' -e 'DST 5'
This will search for RX VIDEO
together with DST 3
or DST 5
. 这将与
DST 3
或DST 5
一起搜索RX VIDEO
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.