grep命令:匹配多个单词

作者: KiwenLau | 来源:发表于2015-06-26 08:54 被阅读1900次

使用grep命令可以通过匹配单词迅速定位对应的行,但是有时候需要同时匹配多个单词,而且会有不同的匹配要求。本文将通过匹配两个单词作为示例,介绍grep匹配多个单词的方法。

输入文本(country.txt)

Austria England
Austria Canada
China England
China Canada

1. 匹配同时含两个单词的行

cat country.txt | grep Austria | grep England

输出

Austria England

2. 匹配两个单词都不存在的行

cat country.txt | grep -v Austria | grep -v England

输出

China Canada

3. 匹配含有任意一个单词的行

cat country.txt | grep -E 'Austria|England'

输出

Austria England
Austria Canada
China England

3. 匹配含有其中一个单词但是不含另一个单词的行

cat country.txt | grep Austria | grep -v England

输出

Austria Canada

版权声明
转载时请注明作者KiwenLau以及本文地址:
http://kiwenlau.com/2015/06/26/grep-multiple-word/


相关文章

  • grep命令:匹配多个单词

    使用grep命令可以通过匹配单词迅速定位对应的行,但是有时候需要同时匹配多个单词,而且会有不同的匹配要求。本文将通...

  • 2019-11-13 grep 匹配多个关键字

    一般情况下,grep 命令只能匹配一个关键字,怎么实现匹配多个关键字呢? 下面看几种情况: 1、 grep ‘字符...

  • 常用linux命令

    grep 命令 grep ‘关键字’ 文件名 标记匹配颜色 --color=auto 选项:grep "match...

  • Shell常见命令

    Shell常见命令 grep 命令格式详解 : grep [参数] [关键字] <文件名>参数详解-c:只输出匹配...

  • shell grep 基本使用

    grep语法: -i //忽略大小写 -v //取反匹配 -w //单词匹配 -q //静默匹配,不将结果显...

  • linux 查看日志 - 命令笔记

    grep 命令 grep -C 5 foo file 显示file文件里匹配foo字串那行以及上下5行 grep ...

  • 查看日志小技巧

    grep命令的几个参数含义如下:grep ‘name’ -A 10 显示匹配内容和后面的10行grep ‘name...

  • Shell grep 命令简介

    grep 命令用于搜索文本或指定的文件中与指定的字符串或者模式匹配符相同的行。默认情况下,grep 命令只显示匹配...

  • grep匹配命令查找

    grep常用选项: 1.根据匹配内容过滤:grep "com.test.demo" a.log2.在当前目录多个文...

  • 学习小组Day1笔记-Young

    一、搜索命令 1. grep 命令 grep的作用是在文件中和匹配符合条件的字符串行 2. find find命令...

网友评论

    本文标题:grep命令:匹配多个单词

    本文链接:https://www.haomeiwen.com/subject/dmreqttx.html