序言:本篇是Linux基本操作篇章的内容!
笔者使用的环境是基于腾讯云服务器:CentOS7.664bit。
学习集:
注:find指令常与grep指令在笔试中被提到linux模糊查询文件个数,需让你回答优缺!
目录索引:
1.基本句型、功能及使用方法1.1基本句型
基本句型:grep[option]搜救字符串文件
1.2功能及使用方法
功能:在文件中搜索字符串,将找到的行复印下来
使用方法(两种):
2.基本用法示例:过滤查找内容
测试用例生成
/* 拷贝数据集文件用于测试,注:若无该文件集可使用如下指令生成:
count=0; while [ $count -le 100 ]; do echo "hello ${count}"; let count++; done > file.txt
*/
[Mortal@VM-12-16-centos test_findsome]$ cd ~
[Mortal@VM-12-16-centos ~]$ ls
StudyingOrder_Linux test1 test2 test3 test_cp test_findsome test_mkdir test_mv test_txtfile
[Mortal@VM-12-16-centos ~]$ ls test_txtfile
file.txt filetxt.txt main.c
[Mortal@VM-12-16-centos ~]$ cp test_txtfile/file.txt test_findsome/grep_test.txt
[Mortal@VM-12-16-centos ~]$ cd test_findsome/
过滤查找内容:即(模糊)查找包含指定字符串的内容!
/* 方式一:grep 指定字符串 指定文件 */
[Mortal@VM-12-16-centos test_findsome]$ grep "0" grep_test.txt
hello 0
hello 10
hello 20
hello 30
hello 40
hello 50
hello 60
hello 70
hello 80
hello 90
hello 100
/* 方式二:cat 指定文件 | grep 指定字符串【该方式结合管道使用!】 */
[Mortal@VM-12-16-centos test_findsome]$ cat grep_test.txt | grep "0"
hello 0
hello 10
hello 20
hello 30
hello 40
hello 50
hello 60
hello 70
hello 80
hello 90
hello 100
3.「-v」:反向过滤掉指定内容输出
-v:是一个可选项linux模糊查询文件个数LINUX社区,作用:反向选择,借以显示出没有‘搜寻字符串’内容的那一行
/* 方式一:grep 指定字符串 指定文件 */
[Mortal@VM-12-16-centos test_findsome]$ grep -v "0" grep_test.txt
hello 1
hello 2
hello 3
hello 4
hello 5
hello 6
hello 7
.../* 数据过多,省略不复制出来 */
/* 方式二:cat 指定文件 | grep 指定字符串【该方式结合管道使用!】 */
[Mortal@VM-12-16-centos test_findsome]$ cat grep_test.txt | grep -v "0"
hello 1
hello 2
hello 3
hello 4
hello 5
hello 6
hello 7
.../* 数据过多,省略不复制出来 */
4.其他简单可选参数4.1「-i」:不分辨大小写过滤查找4.2「-j」:顺带输出行号5.补充说明:关联正则表达式
此时linux系统入门学习,只是简单先介绍以上内容!后续会持续更新本文!
说明:grep指令如今也支持键值/正则表达式等!
比如:catgrep_test.txt|grep-v“hello9[0-5]”
[Mortal@VM-12-16-centos test_findsome]$ cat grep_test.txt | grep 'hello 9[0-5]'
hello 90
hello 91
hello 92
hello 93
hello 94
hello 95