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

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

问题描述

我需要一个shell脚本使用grep使用回声。我可以用它?

我想这一点,但不正确:

 回声的Linux命令:grep的Linux〜/工作区/ EP-exercicios / M1 /​​ E2 /介绍,linux.html | WC -w

我需要显示消息:

 的Linux:(在文件许多Linux的词)。

例如:

 的Linux:945


解决方案

的grep -o |厕所-l 其他答案逻辑应该在大多数系统今天的工作。

下面是一个使用另一种机制 AWK

 的awk'END {打印RS:NR-1}RS = Linux的〜/工作区/ EP-exercicios / M1 /​​ E2 /介绍,linux.html

逻辑:劈记录文件中,记录分隔符=LINUX。在结束时,打印记录数


例如作为包含这些内容的文件:


  

在Linux是一种类Unix和POSIX兼容的大多是电脑操作系统
  系统(OS)的自由和开源软件模式下组装
  开发和分发。 Linux的的定义组件是
  Linux内核,操作系统内核10月5日第一次发布,
  1991年被Linus Torvalds公司。自由软件基金会使用名称
  的GNU / Linux描述操作系统,这导致了一些
  争议


记录将是:



  1. 是自由和开放源码软件开发和分销的模式下,组建了一个类Unix和POSIX兼容的大多是计算机操作系统(OS)。的定义组件


  2. 内核,操作系统内核由Linus Torvalds在1991年10月5日第一次发布。自由软件基金会使用的名称GNU /

  3. 来形容操作系统,这导致了一些争议

的Linux的发生次数是4 ==最后一个记录号 - 1

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

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

解决方案

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

Here is another mechanism using 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.


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

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

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

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

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