Skip to the content.

Useful commands when process stdout

-E option in grep, zgrep, sed

Although -e could work in most cases when regex in search phase, -E could help to save some escape backslash. In complex regex, bunch of backslashes will bring troubles.

Some samples:

>>> echo "hello" | grep -o -e 'h|l'
(Nothing produced)
>>> echo "hello" | grep -o -e 'h\|l'
>>> h
>>> l
>>> l
(Backslash is necessary)
>>> echo "hello" | grep -o -E 'h\|l'
>>> h
>>> l
>>> l

>>> echo "hello" | sed -e 's/h|l/x/g'
>>> hello
>>> echo "hello" | sed -E 's/h|l/x/g'
>>> xexxo

-o in grep

Super commonly used option, which tells grep only to return the part matching the pattern

sort -u

Simply sort result lines by default order but with redundant lines removed.

In processing some access logs, it could be used after grep and sed to show the list of a certain patterns.

uniq -c

Remove duplicated lines and attach line count. Need to be pipelined after sort because the unique operation only applies to sequential lines.

Written on December 19, 2016