linux - 格式化awk的输出

标签 linux bash shell awk

我正在编写 awk 脚本来确定字数。

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

并对输出进行格式化,因此标题将是“the not that”,并且它们下面的行必须是每个单词的编号。我正在使用这个:

awk 'BEGIN { print "the not that"<br/> { printf "%-10s %s\n", $1, $1 }}' br.txt

问题是我无法获取单词下一行的单词数。我应该更改或添加什么? 感谢您的努力

最佳答案

这是一个 awk,它应该可以满足您的需要。

awk '$1~/the/ {the++} $1~/not/ {not++} $1~/that/ {that++} END {print "the","not","that\n"the,not,that}' FS=: OFS="\t" br.txt

这是它的工作原理:

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

关于linux - 格式化awk的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28652094/

相关文章:

java - SWT GC : Color not correctly drawn on Windows

database - 在 Linux 上索引大型数据集的最便宜方法(最好使用 sphinx)

java - 在 Linux 上运行 Java 调用 native .so 时持续存在 UnsatisfiedLinkError

bash - 像在 SAS 中一样加入 bash

linux - 调用 ausearch 的脚本在 stdin 上传输数据时表现不同

python - 无法在 Linux 的虚拟环境中使用包

bash - 在 bash 中有条件地将命令的输出定向到/dev/null

linux - 使用函数建议在 bash 脚本中静音输出?

linux - 需要在linux中将制表符分隔的文本拆分为多个变量

python - 子进程命令显示 FileNotFoundError : [Errno 2] No such file or directory