bash 如何在 Unix shell 脚本中使用带有 grep 的 echo?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38024647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:49:35  来源:igfitidea点击:

How use echo with grep in a Unix shell script?

bashshellunixgrepecho

提问by Eric Saboia

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

我需要在 shell 脚本中将 echo 与 grep 一起使用。我可以使用它吗?

I tried this, but is incorrect:

我试过这个,但不正确:

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

I need show the message:

我需要显示消息:

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

Example:

例子:

Linux: 945

采纳答案by anishsane

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

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

Here is another mechanism using awk.

这是另一种使用awk.

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

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

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



e.g.for file containing these contents:

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

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

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

records will be:

记录将是:

  1. The
  2. 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
  3. is the
  4. kernel, an operating system kernel first released on October 5, 1991 by Linus Torvalds. The Free Software Foundation uses the name GNU/
  5. to describe the operating system, which has led to some controversy
  1. 是在自由和开源软件开发和分发模式下组装的类 Unix 且主要符合 POSIX 的计算机操作系统 (OS)。的定义组件
  2. 是个
  3. 内核,一种操作系统内核,由 Linus Torvalds 于 1991 年 10 月 5 日首次发布。自由软件基金会使用名称 GNU/
  4. 来描述操作系统,这引起了一些争议

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

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

回答by Мона_Сах

Use grepwith -ooption:

grep-o选项一起使用:

printf "%s: %s\n" "Linux : " "$(grep -o "Linux" ~/workspace/ep-exercicios/m1/e2/intro-linux.html | wc -w)"

should do it

应该做

grepmanpage says :

grep联机帮助页说:

-o, --only-matching
Print only the matched (non-empty) parts of a matching line,with each such part on a separate output line.

-o, --only-matching
只打印匹配行的匹配(非空)部分,每个这样的部分在一个单独的输出行上。

回答by sailesh ramanam

Simply you can achieve it by doing slight modification below.

只需在下面稍作修改即可实现。

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

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