I'm new to linux shell and am trying to do this, preferably in one line, with the following condition: It can't output anything to the terminal.
/var/folder/program.exe -L parameters | grep text_to_filter && echo SomeText >'/tmp/Log.txt'
The problem is the .exe spits out XML data to terminal. I can't figure out how to grep, use the exit status, but not have the screen cluttered with the output of each match. If I use /dev/null 2>&1
, it pipes it quite but then I can't grep the data. Any idea's?
To suppress the default grep output and print only the names of files containing the matched pattern, use the -l ( or --files-with-matches ) option.
We can make the grep to display only the matched string by using the -o option.
The quiet option ( -q ), causes grep to run silently and not generate any output.
The easiest of the two commands is to use grep's -w option. This will find only lines that contain your target word as a complete word. Run the command "grep -w hub" against your target file and you will only see lines that contain the word "hub" as a complete word.
grep -n prints the line number of the pattern that is matched. It is very useful command to locate characters in large files. 8. Print only the matched pattern with grep command grep -o command prints only the matched pattern instead of the whole line. Normally, grep command prints the whole line that contain pattern till the line breaks. 9.
Use -B and a number of lines to display before a match: grep -B 2 phoenix sample - this command prints two lines before the match. Use -C and a number of lines to display before and after the match: grep -C 2 phoenix sample - this command prints two lines before and after the match. To Display Line Numbers with grep Matches
To use negative matching in grep, you should execute the command with the -v or --invert-match flags. This will print only the lines that don’t match the pattern given.
Print only the matched pattern with grep command grep -o command prints only the matched pattern instead of the whole line. Normally, grep command prints the whole line that contain pattern till the line breaks. 9. Search all files in directory using grep command
Use grep -q
(quiet)
/var/folder/program.exe -L parameters |
grep -q "text_to_filter" && echo 'SomeText' > '/tmp/Log.txt'
As per man grep
:
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With