Linux基本命令列搜尋操作之&&grep命令
阿新 • • 發佈:2019-01-05
Linux基本命令列搜尋操作之&&grep命令
1.搜尋包含指定內容的檔案
- 例如:搜尋
/root
下所有檔案中是否包含"hello spark"
的檔案,使用如下命令:
[[email protected] ~]# grep "hello spark" /root/*.*
/root/word.txt:hello spark
- 例如:搜尋指定路徑下的指定檔案是否包含某一個字串,使用如下命令:
[[email protected] ~]# grep "spark" /root/word.txt
hello spark
- 注:實驗環境的檔案如下:
[[email protected] ~]# ll
total 1
-rw-r--r--. 1 root root 58 Apr 5 02:21 word.txt
2.顯示檔案中包含某字串的行及內容
grep -n [string] [filepath]
[[email protected] ~]# grep -n hadoo /root/word.txt
4:hello hadoop
7:spark is better than hadoop in streaming calculating
8:But when the data size is at PB level the hadoop is more effective
3.常用選項
-i, --ignore-case
Ignore case distinctions in both the PATTERN and the input files. (-i is specified by POSIX.)
-L, --files-without-match
Suppress normal output; instead print the name of each input file from which no output would normally have been printed. The scanning will stop on the first match.
-l, --files-with-matches
Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match. (-l is specified by
POSIX.)
-n, --line-number
Prefix each line of output with the 1-based line number within its input file. (-n is specified by POSIX.)
-r, --recursive
Read all files under each directory, recursively, following symbolic links only if they are on the command line. This is equivalent to the -d recurse option.