I am trying to get data from a text file using a shell script, by searching in some lines with more than one condition. For example in this file:
TX VIDEO ID 34 B 33 SIZE 672 SRC -1 DST 11
TX CBR ID 35 B 35 SIZE 10 SRC -1 DST 11
RX VOIP ID 0 B 0 SIZE 32 SRC -1 DST 3 D
RX VOIP ID 8 B 8 SIZE 32 SRC -1 DST 5 D
RX VOIP ID 20 B 20 SIZE 32 SRC -1 DST 8 D
RX VIDEO ID 9 B 9 SIZE 1490 SRC -1 DST 5 D
RX VIDEO ID 21 B 21 SIZE 1490 SRC -1 DST 8 D
TX INF_BUF ID 37 B 10 SIZE 776 SRC 1 DST 5 T
TX INF_BUF ID 39 B 22 SIZE 776 SRC 2 DST 8 T
I have both the "RX VIDEO"
and "TX VIDEO"
lines. The first condition is, take data from all the "RX VIDEO"
within the "DST 3 && DST 5"
.
For more than one condition you can pipe greps output to another grep.
If you want to search a file for lines with AAA and BBB you can do:
grep 'AAA' file | grep 'BBB'
Or you can search it with regular expressions with only one 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. 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\>'
Or:
grep 'RX VIDEO.*DST [35]\>' file
This will search for RX VIDEO
together with DST 3
or DST 5
.
Edit: I added a word ending at the end of the expressions to surpress for example DST 37 beeing found. See Keith's comment.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.