Reputation: 4750
As the title says I'm wondering if there is an easier way of getting the number of words between two lines in a text file, using text processing tools available on *nix.
For example given a text file is as follows,
a bc ae
a b
ae we wke wew
countwords between, 1-2 -> 5, 2-3 -> 6.
Upvotes: 1
Views: 177
Reputation: 207758
You can use sed
and wc
like this:
sed -n '1,2p' file | wc -w
5
and
sed -n '2,3p' file | wc -w
6
Upvotes: 3
Reputation: 85845
You can do this with a simple awk
command:-
awk -v start='1' -v end='2' 'NR>=start && NR <=end{sum+=NF}END{print sum}' file
For the sample file you have provided:-
$ cat file
a bc ae
a b
ae we wke wew
$ awk -v start='1' -v end='2' 'NR>=start && NR <=end{sum+=NF}END{print sum}' file
5
$ awk -v start='2' -v end='3' 'NR>=start && NR <=end{sum+=NF}END{print sum}' file
6
$ awk -v start='1' -v end='3' 'NR>=start && NR <=end{sum+=NF}END{print sum}' file
9
The logic is simple:-
start
, end
variables for specifying the ranges in the file, they are awk
variablesNR>=start && NR <=end
provides the condition to loop from the lines you needsum+=NF
does the word count arithmetic. NF
is a special awk
variable which counts the number of words de-limited by IFS
, which in this case is white-space.END{print sum}
prints the final count.Worked fine on GNU Awk 3.1.7
Upvotes: 0