If the size in the output is not too large to fit in memory and you don't need the wc
and sort
commands to work in parallel for performance reasons, here's a relatively simple solution:
output=$(awk '$13 ~ /type/ {print $15}' filename.txt; echo a)
printf "%s" "${output%a}" | sort -u
printf "%s" "${output%a}" | wc -l
That complication with the the extra a
is in case the awk
command might print some empty lines at the end of the input, which the $()
construct would strip. You can easily choose which of sort
or wc
should appear first.
Here's a way that works with any POSIX shell (ash, bash, ksh, zsh, ...) but only on systems that have /dev/fd
(which includes reasonably recent Linux, *BSD and Solaris). Like Walter's similar construction using the simpler method available in bash, ksh93 and zsh, the output of wc
and the output of sort
may be intermixed.
{
awk '$13 ~ /type/ {print $15}' filename.txt |
tee /dev/fd3 |
wc -l
} 3>&1 1>&3 | sort -u
If you both need to deal with intermediate output that doesn't comfortably fit in memory and don't want to have the output of the two commands intermixed, I don't think there's an easy way in a POSIX shell, though it should be doable with ksh or zsh coprocesses.