请选择 进入手机版 | 继续访问电脑版
搜索
房产
装修
汽车
婚嫁
健康
理财
旅游
美食
跳蚤
二手房
租房
招聘
二手车
教育
茶座
我要买房
买东西
装修家居
交友
职场
生活
网购
亲子
情感
龙城车友
找美食
谈婚论嫁
美女
兴趣
八卦
宠物
手机

Linux Bash文本操作之grep篇

[复制链接]
查看: 23|回复: 0

1万

主题

2万

帖子

4万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
46407
发表于 2019-11-9 08:51 | 显示全部楼层 |阅读模式
Linux grep命令用于查找文件里合适条件的字符串。是文本检索中常用的工具之一。
<blockquote> grep  指令在文件中查找可以大要婚配指定形式字符串的行。假如没有指定文件名,大要文件名为  -  ,则从标准输入装备获得数据。默许会输出婚配行。
  1. grep will print lines matching a pattern.grep searches the named input FILEs for lines containing a match to the given PATTERN. If no files are specified, or “-” is given, grep searches standard input. By default, grep prints the matching lines.
复制代码
尝试文本获得方式见sed篇其一
利用方式以下
  1. grep [OPTIONS] PATTERN [FILE...]grep [OPTIONS] [-e PATTERN]...  [-f FILE]...  [FILE...]
复制代码
根柢用法婚配文本内容
示例1检察文本中婚配   sed   字符的行。
示例2表示假如我们想包含巨细写,利用巨细写不敏感选项,查找内容就会包含  [sS][eE][dD]  。
示例3展现了还可以举行反向查找,也就是查找并打印不合适条件的行。
示例4和5用于输出婚配内容或不婚配内容的行的数目。
  1. -G, --basic-regexp     Interpret PATTERN as a basic regular expression. This is the default.-c, --count     Suppress normal output; instead print a count of matching lines for each input file.-e PATTERN, --regexp=PATTERN     Use PATTERN as the pattern. If this option is used multiple times or is combined with the -f, search for all patterns given.                    This option can be used to protect a pattern beginning with “-”.-f FILE, --file=FILE     Obtain patterns from FILE, one per line. If this option is used multiple times or is combined with the -e, search for all patterns given.               The empty file contains zero patterns, and therefore matches nothing.-i, --ignore-case    Ignore case distinctions in both the PATTERN and the input files.-n, --line-number     Prefix each line of output with the 1-based line number within its input file.-o, --only-matching   Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.-v, --invert-match   Invert the sense of matching, to select non-matching lines.-w, --word-regexp     Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line,             or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character.             Word-constituent characters are letters, digits, and the underscore.
复制代码
  1. cv@cv:~/myfiles$ grep 'sed' test.txt        #example-1       sed - stream editor for filtering and transforming text       sed [OPTION]... {script-only-if-no-other-script} [input-file]...       Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter textcv@cv:~/myfiles$ grep -i 'sed' test.txt     #example-2       sed - stream editor for filtering and transforming text       sed [OPTION]... {script-only-if-no-other-script} [input-file]...       Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter textcv@cv:~/myfiles$ grep -v 'sed' test.txt     #example-3NAMESYNOPSISDESCRIPTION       in a pipeline which particularly distinguishes it from other types of editors.       -n, --quiet, --silent              suppress automatic printing of pattern space       -e script, --expression=script              add the script to the commands to be executed       -f script-file, --file=script-file              add the contents of script-file to the commands to be executed       --follow-symlinks              follow symlinks when processing in place       -i[SUFFIX], --in-place[=SUFFIX]cv@cv:~/myfiles$ grep -c 'sed' test.txt        #example-44cv@cv:~/myfiles$ grep -c -v 'sed' test.txt     #example-514
复制代码
示例6表示只婚配完整的单词而不包含子串的形式。
示例7只表示婚配到的地方,其他部分疏忽掉,当需要把婚配变量存入变量时比力有用。
示例8打印婚配行的同时表示婚配行的行号。
示例9表示以  sed  开首的行,测试文本中没有这样的行,是以无输出。
示例10表示以  sed  开首的婚配,如答应以扫除  used  这样开首不合适的字符串,与  \bsed  成果类似。
示例11表示以  sed  末真个婚配,包含  used  ,由于其末端的子串确切为  sed  。
示例12和13都用来表示完全婚配  sed  的字符串,与上面的  -w  指令成果类似。
示例14可以用于婚配多个差别的字符串形式,以下面的示例所示,加上第二个指令后会将原文第六行的  Sed  也表示出来,否则婚配不到,由于首字母为大写。
[code]cv@cv:~/myfiles$ grep -w 'sed' test.txt     #example-6       sed - stream editor for filtering and transforming text       sed [OPTION]... {script-only-if-no-other-script} [input-file]...       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter textcv@cv:~/myfiles$ grep -o 'sed' test.txt     #example-7sedsedsedsedsedcv@cv:~/myfiles$ grep -n 'sed' test.txt     #example-82:       sed - stream editor for filtering and transforming text4:       sed [OPTION]... {script-only-if-no-other-script} [input-file]...6:       Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an7:       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter textcv@cv:~/myfiles$ grep "^sed" test.txt         #example-9cv@cv:~/myfiles$ grep '\
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Copyright © 2006-2014 妈妈网-中国妈妈第一,是怀孕、育儿、健康等知识交流传播首选平台 版权所有 法律顾问:高律师 客服电话:0791-88289918
技术支持:迪恩网络科技公司  Powered by Discuz! X3.2
快速回复 返回顶部 返回列表