Reputation: 1
I'm writing awk script to determine the number of words.
awk '$1 ~/the/ {++c}END{print c}' FS=: br.txt
awk '$1 ~/not/ {++c}END{print c}' FS=: br.txt
awk '$1 ~/that/ {++c}END{print c}' FS=: br.txt
And formatting the output, so the heading will be "the not that" and the line under them must be the number of each word. I'm using this:
awk 'BEGIN { print "the not that"
{ printf "%-10s %s\n", $1, $1 }}' br.txt
The problem is I can't getting the number of words in a row under the the words. What should I change or add? Thanks for your efforts
Upvotes: 0
Views: 229
Reputation: 37288
create separate variables for counts of the words you want to count, all in one awk program, i.e.
awk -F: '$1~/the/{t++}
$1~/not/{n++}
$1~/that/{h++}
END {
printf("the\tnot\tthat\n%d\t%d\t%d\n", t,n,h)
}' br.txt
Tested with
echo "see the fox
the fox is not here
what is not that" \
| awk -F: '$1~/the/{t++}
$1~/not/{n++}
$1~/that/{h++}
END {
printf("the\tnot\that\n%d\t%d\t%d\n", t,n,h)
}'
the not hat
2 2 1
Upvotes: 1
Reputation: 41456
Here is an awk
that should do what you need.
awk '$1~/the/ {the++} $1~/not/ {not++} $1~/that/ {that++} END {print "the","not","that\n"the,not,that}' FS=: OFS="\t" br.txt
Here is how it works:
awk '
$1~/the/ {the++} # If field `1` contains `the` and `1` to variable `the`
$1~/not/ {not++} # If field `1` contains `not` and `1` to variable `not`
$1~/that/ {that++} # If field `1` contains `that` and `1` to variable `that`
END { # When all file is read, do
print "the","not","that\n"the,not,that} # Print header, and the value of variable `the,not,that`
' FS=: OFS="\t" br.txt # Input field separator = `:`. Output separator = `<tab>`. Read file
Upvotes: 2