Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use grep to match but without printing the matches?

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?

like image 389
Nimjox Avatar asked Nov 07 '13 16:11

Nimjox


People also ask

How can I grep without printing?

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.

Which option for grep command is used for displaying only the matched pattern?

We can make the grep to display only the matched string by using the -o option.

How can you get something with grep and do not have all output on the screen?

The quiet option ( -q ), causes grep to run silently and not generate any output.

How do you grep words only?

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.

How to use grep command to find the matched pattern?

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.

How to display line numbers with grep matches?

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

How to use negative matching in grep?

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.

How to print only the matched pattern in Linux?

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


1 Answers

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.

like image 131
anubhava Avatar answered Sep 20 '22 17:09

anubhava