Generally speaking, the more specialized a tool is, the faster it is. So in most cases, you can expect cut
and grep
to be faster than sed
, and sed
to be faster than awk
. If you're compairing longer pipelines of simpler tools with a single invocation of a more complex tool, there's no rule of thumb. This only matters with large inputs (say, millions of lines); for short inputs, you won't see any difference.
The advantage of more complex tools is of course that they can do more things.
Your commands use cat needlessly. Use redirection instead (especially if you're worried about speed, though you probably shouldn't be worried about speed until you've run benchmarks¹).
<fileName awk '/WORD/ { print $2 }'
<fileName grep WORD | cut -f 2 -d ' '
These commands are almost equivalent. The differences are:
- awk and grep have different regexp syntaxes. Awk and
grep -E
have almost identical regexp syntaxes (extended regular expressions).
cut -d ' '
treats each individual space character as a delimiter. Awk's default delimiter is any whitespace sequence, which can be multiple spaces, a tab, etc. You cannot use arbitrary whitespace sequences as separators with cut
. To use individual spaces as separators in awk, set the field separator to a regexp that matches a single space, other than a regexp consisting of single space (which is a special case meaning “any whitespace sequence”, i.e. the default): awk -F '[ ]' '/WORD/ {print $2}'
.
¹ The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet. — Michael A. Jackson
echo filename
orcat filename
? – Avinash Raj May 28 '14 at 06:48