输入示例:
Question1: What kind of cake do you like?
Anything with sprinkles
输出示例:
Question1: What kind of cake do you like?
3 Anything with sprinkles
唯一的标记是“Questionx:”和“?”。答案总是在问题之后。这对我来说很糟糕,(找到/?$/),但我无法完全让多行方面与字数统计($ NF)很好地配合。其他语言也可用,如 perl/python/php 等,但我试图避免编写我认为应该是简单的单行代码的程序
答案1
等待我上面评论的答案:
awk '/^Question[0-9]+:.*\?$/ {print; next} {print NF, $0}'
或者
awk '{printf "%s%s\n", (/^Question[0-9]+:.*\?$/ ? "" : NF " "), $0}'
答案2
这将单独给出每个问题中的“单词”数量:
awk '/^Question/ {if (NR > 0) print words; words=0} $0 !~ /^Question/ {words+=NF}' /path/to/input
要按照您的描述打印问题和每行的字数:
awk '/^Question/ {print} $0 !~ /^Question/ {print NF, $0}' /path/to/input
使每行的数字前缀累积:
awk '/^Question/ {words=0; print} $0 !~ /^Question/ {words+=NF; print words, $0}' /path/to/input