Issue
I have a file that i want to pars using grep command, the file consist of lines like this:
NVO,0,267,61,247357,247357,O,19:00:00.000000,06:09:08.417320,07:55:22.068670
DVD,0,267,61,247357,247357,O,19:00:00.000000,06:09:08.417320,07:55:22.068670
NVO,0,267,61,247358,247358,B,19:00:00.000000,06:09:08.417407,07:55:22.079291
DVD,0,267,61,247358,247358,B,19:00:00.000000,06:09:08.417407,07:55:22.079291
I want to get only the line that start with NVO, and have ,B, in the line. the out put should look like this:
NVO,0,267,61,247358,247358,B,19:00:00.000000,06:09:08.417407,07:55:22.079291
to get the NVO,
line I'm using grep ^NVO, file_name.txt
but I'm unable to add the second condition ,B,
I tried grep '^NVO,|,B,' file_name.txt
and also
grep ",B," | grep "^NVO," file_name.txt
with no luck
I know I can do it with two commands, the first to write to file and then to do a second grep command with the ,B,
filter
Solution
The operation you're attempting does not actually require combining two grep
calls. Therefore, you can use
grep "^NVO,.*,B," file > outfile
Details:
^
- string startNVO,
- anNVO,
string.*
- any text (any zero or more chars),B,
- a,B,
text.
See the online demo:
#!/bin/bash
s='NVO,0,267,61,247357,247357,O,19:00:00.000000,06:09:08.417320,07:55:22.068670
DVD,0,267,61,247357,247357,O,19:00:00.000000,06:09:08.417320,07:55:22.068670
NVO,0,267,61,247358,247358,B,19:00:00.000000,06:09:08.417407,07:55:22.079291
DVD,0,267,61,247358,247358,B,19:00:00.000000,06:09:08.417407,07:55:22.079291'
grep "^NVO,.*,B," <<< "$s"
Output:
NVO,0,267,61,247358,247358,B,19:00:00.000000,06:09:08.417407,07:55:22.079291
Answered By - Wiktor Stribiżew