Wednesday, July 20, 2016

Difference between awk and cut command?

cut:  takes a single character in -d as the field delimiter (the default being TAB), and every single occurrence of that character starts a new field. 



awk:  however, is more flexible. The separator is in the FS variable and can be an empty string (every input character makes a separate field), a single character, or a regular expression. The special case of a single space character (the default) means to split on any sequence of whitespace. Also, awk suppresses leading whitespace by default.

Examples: 

% echo "All is well" | cut -f 2 -d ' '
is

%echo "All  is  well" | cut -f 2 -d ' '


% echo " All is well" | cut -f 2 -d ' '
All


% echo "All is well" | awk '{print $2}'
is

% echo "All  is  well" |  awk '{print $2}'
is

% echo " All is well" | awk '{print $2}'
is





No comments:

Post a Comment