Linux常用命令

来源:互联网 发布:淘宝网淋浴塑料帘布 编辑:程序博客网 时间:2024/05/24 06:09

1. grep

Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户。grep的工作方式是这样的,它在一个或多个文件中搜索字符串模板。grep可用于shell脚本,因为grep通过返回一个状态值来说明搜索的状态,如果模板搜索成功,则返回0,如果搜索不成功,则返回1,如果搜索的文件不存在,则返回2。

命令格式:grep [option] patternFile命令参数:-n    #在显示符合样式的那一行之前,标示出该行的行数编号。-c    #计算符合样式的行数-f    #从文件中获得匹配模式,让grep查找符合规则条件的文件内容,格式为每行一个规则样式。-v    #显示不包含匹配文本的所有行。-E    #扩展正则表达式,特殊字符不加\斜杠-e    #指定字符串做为查找文件内容的样式。特殊字符要加\斜杠-i    #不区分大小写-w    #匹配整个单词1. 从文件中读取关键词进行搜索 且显示行号输出test.txt文件中含有从test2.txt文件中读取出的关键词的内容行,并显示每一行的行号(在原文件test中的行数)[root@localhost test]# cat test.txt hnlinuxpeida.cnblogs.comubuntuubuntu linuxredhatRedhatlinuxmint[root@localhost test]# cat test2.txt linuxRedhat[root@localhost test]# cat test.txt | grep -nf test2.txt1:hnlinux4:ubuntu linux6:Redhat7:linuxmint[root@localhost test]#2. 从文件中查找关键词[root@localhost test]# grep -n 'linux' test.txt 1:hnlinux4:ubuntu linux7:linuxmint[root@localhost test]#3. 从多个文件中查找关键词[root@localhost test]# grep -n 'linux' test.txt test2.txt test.txt:1:hnlinuxtest.txt:4:ubuntu linuxtest.txt:7:linuxminttest2.txt:1:linux4. 找出以u开头的行内容[root@localhost test]# cat test.txt |grep ^uubuntuubuntu linux[root@localhost test]#5. 显示包含ed或者at字符的内容行[root@localhost test]# cat test.txt | grep -E "ed|at"redhatRedhat[root@localhost test]#grep -e '^\(root\|zhang\)' 等价于grep -E '^(root|zhang)'  6. 显示当前目录下面以.txt 结尾的文件中的所有包含每个字符串至少有7个连续小写字符的字符串的行[root@localhost test]# grep '[a-z]\{7\}' *.txttest.txt:hnlinuxtest.txt:peida.cnblogs.comtest.txt:linuxmint[root@localhost test]#

2. TODO

0 0