Reputation: 123
I have two statements in bash that I would like to combine so that the output is all in one file. Would there be a way to combine these? What the second line is basically doing is adding a total line count to the bottom of the file. I would like it to line up with the formatting of the file which is why I would like to combine the two somehow. Any help would be greatly appreciated!
grep -E -o -r "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" $ARCHIVE | sed 's/.*@//' | sort -r | uniq -ic | sort -nr | sed 's/^ *//g' | awk ' { t = $1; $1 = $2; $2 = t; print; } ' | column -t > temp2
echo Total "$(wc -l < temp2)"
Upvotes: 1
Views: 1169
Reputation: 203169
Without any sample input to test against or even see, this is obviously untested and includes some guess work but it is basically the right approach (using GNU awk and assuming "$ARCHIVE" is a directory):
find "$ARCHIVE" -type f -print |
xargs awk '
BEGIN { FPAT="[[:alnum:]_%+-]+@[[:alnum:]_%+-]+[.][[:alpha:]]{2,6}" }
{
$0 = tolower($0)
for (i=1;i<=NF;i++) {
sub(/.*@/,"",$i)
cnt[$i]++
width = (length($i) < width ? width: length($i))
}
}
END {
PROCINFO["sorted_in"] = "@ind_val_desc"
for (host in cnt) {
printf "%-*s %s\n", width, host, cnt[host]
}
printf "%-*s %s\n", width, "Total", length(cnt)
}'
Upvotes: 0
Reputation: 19982
Replace
| column -t > temp2
with
| column -t | tee temp2 | wc -l
Upvotes: 0
Reputation: 113814
awk
does a good job of keeping track of the number of lines. Replace
awk ' { t = $1; $1 = $2; $2 = t; print; }'
With:
awk ' { t = $1; $1 = $2; $2 = t; print; } END{print "Total",NR}'
In awk, NR
is the number of records (lines) that have been read. The END
block is executed only after all the lines have been read. Thus, this puts the total count at the end of the file, eliminating the need for the final echo Total "$(wc -l < temp2)"
statement.
Upvotes: 2