BraveZhao的个人博客分享 http://blog.sciencenet.cn/u/BraveZhao

博文

LINUX系统grep命令基础+进阶

已有 2574 次阅读 2018-8-1 11:18 |系统分类:科研笔记

 

grep  命令

功能:分析一行信息,若该行中有我们需要的信息,就将该行整行拿出来

image.png


grep 进阶选项:

   image.png

dmesg 可列出核心产生的讯息

dmesg  | grep -n –color=auto ‘qxl’

dmesg  | grep  -n -A3  -B2  –color=auto  ‘qxl’

grep  --color=auto ‘qxl’  etc/man/zzq/band


基础正规表达法练习

1.

grep  -n  --color=auto  “the”  1.txt   #将1.txt中含有the的行输出出来;

grep  -vn  --color=auto  “the”  1.txt  #反向选择-v,不输出含有the的行

grep  -in  --color=auto  “the”  1.txt  #不区分大小写-i,输出含有the的行,不区分大小写;


2. 利用中括号[ ]来搜索集合字符

grep  -n  --color=auto  ‘t[ae]st’  1.txt   #检索含有tast和test的字符

grep  -n  --color=auto  ‘oo’  1.txt #搜索含有oo字符的行;


3. [^] ,代表反向选择

grep  -n  --color=auto  ‘[^g]oo’  1.txt  #搜索oo字符前面没有g的行,[^],代表反向选择;


grep  -n  --color=auto  ‘[^a-z]oo’  1.txt    #输出字符oo前面没有小写字母的行

grep  -n  --color=auto  ‘[^A-Z]oo’  1.txt   #输出字符oo前面没有大写字母的行

grep  -n  --color=auto  ‘[^0-9]oo’  1.txt   #输出字符oo前面没有字母的行

如果字符组是连续的可以使用-来代表连续字符组,如[a-z],[A-Z],[0-9]


grep  -n  --color=auto  ‘[a-zA-Z0-9]’  1.txt #检索具有数字和字母的行;

grep  -n  --color=auto  ‘[^[ : lower: ] ]’  1.txt  #输出不含有小写字母的行

[ : lower: ]代表a-z的意思;


4. 行首与行尾字符^ $

行首字符^

注意区分^与[^],^在括号外代行首字符,在括号内代表反向选择

grep  -n  ‘^the’  1.txt  #^代表行首字符,该命令只提取行首为the的行

grep  -n  ‘^[a-z]’  1.txt  #只提取行首为小写字母的行;

grep  -n  ‘^[ [ : lower: ] ]’  1.txt  #与上一条命令等价;

grep  -n  ‘^[^a-zA-Z]’  1.txt  #只提取行首为非英文字母的行;


行尾字符$

grep  -n  ‘\ .$’  1.txt   #找出行尾结束字符为小数点的那一行;

因为小数点具有其他特殊意义,所以必须使用跳脱字符\来解释其意义

grep  -n  ‘^$’  1.txt4   #因为只有行尾与行首,所以可以找出空白行

grep  -v  ‘^$’  1.txt  |  grep -v  ‘^#”  1.txt  #代表不输出空白行和不输出开头是#的行 


任意一个字符 . 与重复字符 *

grep  -n  ‘g..d’  1.txt  #找出含有以g开头,以d结尾,g、d之间含有任意两个字符的行


image.png

grep  -n  ‘ooo*’  1.txt  #找出至少含有两个oo字符串的行;

image.png

grep  -n  ‘goo*g’  1.txt  #输出1.txt文件中含有字符串(字符串的开头和结尾都是g, 但是两个g之间至少                                                  存在一个o)的行;

grep  -n  ‘g.*g’  1.txt  #输出以g为开头和结尾,且中间可以为任意字符的行

image.png

grep  -n  ‘[0-9] [0-9]*’  1.txt

正规表达法与文件格式华处理.docx

本笔记参考《鸟哥的linux》私房菜



https://wap.sciencenet.cn/blog-3388193-1126991.html

上一篇:shell脚本处理VASP计算得到的数据,用来绘制能量-体积曲线
下一篇:LINUX学习笔记---文件权限与目录配置
收藏 IP: 124.239.251.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-27 06:29

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部