Highlight text similar to grep, but don't filter out text
Use ack. Checkout its --passthru
option here: ack. It has the added benefit of allowing full perl regular expressions.
$ ack --passthru 'pattern1' file_name
$ command_here | ack --passthru 'pattern1'
You can also do it using grep like this:
$ grep --color -E '^|pattern1|pattern2' file_name
$ command_here | grep --color -E '^|pattern1|pattern2'
This will match all lines and highlight the patterns. The ^
matches every start of line, but won't get printed/highlighted since it's not a character.
(Note that most of the setups will use --color by default. You may not need that flag).
Colorized grep -- viewing the entire file with highlighted matches
Here are some ways to do it:
grep --color 'pattern\|$' file
grep --color -E 'pattern|$' file
egrep --color 'pattern|$' file
The |
symbol is the OR operator. Either escape it using \
or tell grep that the search text has to be interpreted as regular expressions by adding -E or using the egrep
command instead of grep
.
The search text "pattern|$" is actually a trick, it will match lines that have pattern
OR lines that have an end. Because all lines have an end, all lines are matched, but the end of a line isn't actually any characters, so it won't be colored.
To also pass the colored parts through pipes, e.g. towards less
, provide the always
parameter to --color
:
grep --color=always 'pattern\|$' file | less -r
grep --color=always -E 'pattern|$' file | less -r
egrep --color=always 'pattern|$' file | less -r
Negative matching using grep (match lines that do not contain foo)
grep -v
is your friend:
grep --help | grep invert
-v, --invert-match select non-matching lines
Also check out the related -L
(the complement of -l
).
-L, --files-without-match only print FILE names containing no match
grep highlights on screen but doesn't redirect to output
For reasons not known to me, the following command works directly.
Just FYI.
$fgrep -f pattern_list.txt data.txt
Not "grep" but "fgrep".
Thanks for your interest and time.
How can I exclude one word with grep?
You can do it using -v
(for --invert-match
) option of grep as:
grep -v "unwanted_word" file | grep XXXXXXXX
grep -v "unwanted_word" file
will filter the lines that have the unwanted_word
and grep XXXXXXXX
will list only lines with pattern XXXXXXXX
.
EDIT:
From your comment it looks like you want to list all lines without the unwanted_word
. In that case all you need is:
grep -v 'unwanted_word' file
Grep without filtering
If all you want to do is set the exit code if a pattern is found, then this should do the trick:
awk -v rc=1 '/pattern/ { rc=0 } 1; END {exit rc}'
The -v rc=1
creates a variable inside the Awk program called rc
(short for "return code") and initializes it to the value 1. The stanza /pattern/ { rc=0 }
causes that variable to be set to 0 whenever a line is encountered that matches the regular expression pattern
. The 1;
is an always-true condition with no action attached, meaning the default action will be taken on every line; that default action is printing the line out, so this filter will copy its input to its output unchanged. Finally, the END {exit rc}
runs when there is no more input left to process, and ensures that awk
terminates with the value of the rc
variable as its process exit status: 0 if a match was found, 1 otherwise.
The shell interprets exit code 0 as true and nonzero as false, so this command is suitable for use as the condition of a shell if
or while
statement, possibly at the end of a pipeline.
Grep highlight a pattern other than the searched one
Just grep
again for the pattern you want to highlight:
cat log | grep 'status":"1' | grep '"email":"[^"]*"'
How can I grep recursively, but only in files with certain extensions?
Just use the --include
parameter, like this:
grep -inr --include \*.h --include \*.cpp CP_Image ~/path[12345] | mailx -s GREP email@domain.example
That should do what you want.
To take the explanation from HoldOffHunger's answer below:
grep
: command-r
: recursively-i
: ignore-case-n
: each output line is preceded by its relative line number in the file--include \*.cpp
: all *.cpp: C++ files (escape with \ just in case you have a directory with asterisks in the filenames)./
: Start at current directory.
Related Topics
How to Write Multiple Line String Using Bash with Variables
Linux Kernel "Historical" Git Repository with Full History
How to Copy Text from My Xterm Without a Mouse
How to Fix Symbol Lookup Error: Undefined Symbol Errors in a Cluster Environment
Linux: How to Force a Specific Network Interface to Be Used
Lsb_Release: Command Not Found in Latest Ubuntu Docker Container
How to Dump Part of Binary File
What Is Active Memory and Inactive Memory
Force Unmount of Nfs-Mounted Directory
How to Recompile Just a Single Kernel Module
How to Clean Caches Used by the Linux Kernel
Is It Better to Use Git Grep Than Plain Grep If We Want to Search in Versioned Source Code
How to Register Fuse Filesystem Type with Mount(8) and Fstab