如何在 Unix shell 脚本中使用带有 grep 的 echo? [英] How use echo with grep in a Unix shell script?

查看:29
本文介绍了如何在 Unix shell 脚本中使用带有 grep 的 echo?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 shell 脚本中使用带有 grep 的 echo.可以用吗?

I need to use echo with grep in a shell script. Can I use it?

我试过了,但不正确:

echo Linux: grep "Linux" ~/workspace/ep-exercicios/m1/e2/intro-linux.html | wc -w

我需要显示消息:

Linux: (number of Linux word on the document).

示例:

Linux: 945

推荐答案

grep -o |wc -l 来自其他答案的逻辑应该适用于当今的大多数系统.

grep -o | wc -l logic from other answer should work on most systems today.

这是另一种使用 awk 的机制.

Here is another mechanism using awk.

awk 'END{print RS " : " NR-1}' RS=Linux ~/workspace/ep-exercicios/m1/e2/intro-linux.html

逻辑:将文件分割成记录,记录分隔符=Linux".最后打印记录号.

Logic: split the file in records, with record separator = "Linux". In the end, print the record number.

例如对于包含这些内容的文件:

e.g. for file containing these contents:

Linux 是一种类 Unix 且主要符合 POSIX 的计算机操作自由开源软件模式下组装的系统(OS)开发和分配.Linux 的定义组件是Linux 内核,一种操作系统内核,于 10 月 5 日首次发布,1991 年由莱纳斯·托瓦兹 (Linus Torvalds) 撰写.自由软件基金会使用名称GNU/Linux 来描述操作系统,这导致了一些争议

The Linux is a Unix-like and mostly POSIX-compliant computer operating system (OS) assembled under the model of free and open-source software development and distribution. The defining component of Linux is the Linux kernel, an operating system kernel first released on October 5, 1991 by Linus Torvalds. The Free Software Foundation uses the name GNU/Linux to describe the operating system, which has led to some controversy

记录将是:

  1. 是一种在自由和开源软件开发和分发模式下组装的类 Unix 且主要符合 POSIX 的计算机操作系统 (OS).
  2. 的定义组件
  3. kernel,一种操作系统内核,由 Linus Torvalds 于 1991 年 10 月 5 日首次发布.自由软件基金会使用名称 GNU/
  4. 描述操作系统,这引起了一些争议

Linux 的出现次数为 4 == 最后记录数 - 1.

Occurrence count of Linux is 4 == last record number - 1.

这篇关于如何在 Unix shell 脚本中使用带有 grep 的 echo?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆