Sometimes it needs to extract directory names, file names from a ls command.
For example, ls prints:
. D 0 Fri Jul 21 03:24:22 2017
.. D 0 Fri Jul 21 03:24:22 2017
ABC Good D 0 Fri Jul 21 03:24:25 2017
ABC D 0 Fri Jul 21 03:24:40 2017
Now it needs to extract the ABC Good, ABC from the output
ls | awk '$2 == "D" {print $1} $3 == "D" {print $1 " " $2}'
returns:
.
..
ABC Good
ABC
The AWK command here delimit every line by space, and put each field into $1, $2, ...
If $2 == D, it means it is a directory, then print $1 which is the directory name
But if $3 ==D, the directory name contains space, so print $1 $2 with space in between.
To test it out.
echo "
. D 0 Fri Jul 21 03:24:22 2017
.. D 0 Fri Jul 21 03:24:22 2017
ABC Good D 0 Fri Jul 21 03:24:25 2017
ABC D 0 Fri Jul 21 03:24:40 2017
" | awk '$2 == "D" {print $1} $3 == "D" {print $1 " " $2}'