@dawasaturday

Как подсчитать с помощью утилиты awk число слов в строке?

+ надо выводить перед каждой строкой её номер, это ок, умеемнаучились
awk '$0 = NR" "$0'
для строк, кратных 10ти, выводить только первое слово строки,
для кратных 5 - только второе.
ни сама сообразить не могу, ни нагуглить хоть похожее. подскажите, пожалуйста
  • Вопрос задан
  • 562 просмотра
Решения вопроса 1
miraage
@miraage
Старый прогер
➜  ~ 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)
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 1
shambler81
@shambler81 Куратор тега Linux
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы