加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 如何通过bash / shellscript计算文件组中特定单词的出

发布时间:2020-12-14 00:01:46 所属栏目:Linux 来源:网络整理
导读:我有两个文本文件’simple’和’simple1′,其中包含以下数据 simple.txt-- hello hi hi hello this is it simple1.txt-- hello hi how are you[]$tr ' ' 'n' simple.txt | grep -i -c 'bhw*'4[]$tr ' ' 'n' simple1.txt | grep -i -c 'bhw*'3 此命令显
我有两个文本文件’simple’和’simple1′,其中包含以下数据

simple.txt--

    hello
    hi hi hello
    this
    is it

    simple1.txt--
    hello hi
    how are you



[]$tr ' ' 'n' < simple.txt | grep  -i -c 'bhw*'
4
[]$tr ' ' 'n' < simple1.txt | grep  -i -c 'bhw*'
3

此命令显示每个文件以“h”开头的单词数,但我想显示总计数为7,即两个文件的总数.我可以在单个命令/ shell脚本中执行此操作吗?

P.S.:我不得不写两个命令,因为tr没有两个文件名.

解决方法

此替代方案不需要管道:

$awk -v RS='[[:space:]]+' '/^h/{i++} END{print i+0}' simple.txt simple1.txt
7

这个怎么运作

> -v RS ='[[:space:]]’

这告诉awk将每个单词视为记录.
> / ^ h / {i}

对于以h开头的任何记录(单词),我们将变量i递增1.
> END {print i 0}

在我们读完所有文件后,我们打印出i的值.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读