D
D
dr sbtn2017-04-27 13:27:50
linux
dr sbtn, 2017-04-27 13:27:50

How to count the number of words in a line with awk?

+ it is necessary to display its number before each line, that's ok, we can learn
awk '$0 = NR" "$0'
for lines divisible by 10, to display only the first word of the line,
for multiples of 5 - only the second.
I can't figure it out myself, nor can I google it. Tell me please

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Osher, 2017-04-27
@dawasaturday

➜  ~ cat test.txt
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries,
but also the leap into electronic typesetting,
remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries,
but also the leap into electronic typesetting,
remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries,
but also the leap into electronic typesetting,
remaining essentials

➜  ~ cat test.awk
{
    if ( NR % 10 == 0 )
  print NR " " $1 " (" NF ")";
    else if ( NR % 5 == 0 )
  print NR " " $2 " (" NF ")";
    else
  print NR " " $0 " (" NF ")";
}

➜  ~ awk -f test.awk test.txt
1 Lorem Ipsum is simply dummy text of the printing and typesetting industry. (12)
2 Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, (13)
3 when an unknown printer took a galley of type and scrambled it to make a type specimen book. (18)
4 It has survived not only five centuries, (7)
5 also (7)
6 remaining essentially unchanged. (3)
7 It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, (16)
8 and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. (26)
9 Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, (13)
10 when (18)
11 It has survived not only five centuries, (7)
12 but also the leap into electronic typesetting, (7)
13 remaining essentially unchanged. (3)
14 It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, (16)
15 more (26)
16 Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, (13)
17 when an unknown printer took a galley of type and scrambled it to make a type specimen book. (18)
18 It has survived not only five centuries, (7)
19 but also the leap into electronic typesetting, (7)
20 remaining (2)

V
Victor Taran, 2017-04-27
@shambler81

https://ru.wikipedia.org/wiki/Wc
awk '$0 = NR" "$0' | wc -w

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question