Using colored output for awk, or grep multiple pattern search in and condition

awk, bash, grep

Solution

You can use `--color=always` when piping from grep to retain color:

grep pat1 --color=always | grep pat2

Problem

For my work, I'm frequently searching for patterns in files. Generally I use the `grep --color=auto` to color the search pattern. Now when I'm searching for multiple patterns, all of which should be present in a line, I use `grep pattern1 file|grep pattern2|grep pattern3` or `awk '/pattern1/&&/pattern2'`. But this way, in grep I lose the coloring which is highly helpful for me, or in awk, I don't know any way to color only the pattern string. When it becomes too troublesome, I use `grep pattern1 file|grep pattern2|grep pattern3|grep -E "pattern1|pattern2|pattern3"`. So is there any way in grep to mention multiple patterns in and condition? ( I think regular expressions should support it, but could not find any, specially the ordering of patterns are not fixed) Or is there any way to color print the awk search patterns? Any short compact approach is welcome (for I will be using many times a day )

Original source

Related problems