[英]List just the names of files that contain a string in Linux
我想列出使用Linux命令包含字符串的文件的名称。
使用egrep时,会显示字符串的所有实例,这可能是倍数。
例如:
egrep disco music_file*.txt
可能表现出来;
music_file1.txt: blah blah blah disco stuff
music_file1.txt: disco blah blah blah
music_file1.txt: I like to listen to italodisco
music_file2.txt: I like to party to disco
music_file3.txt: Does your dog like disco?
而我想要的只是:
music_file1.txt
music_file2.txt
music_file3.txt
问题: 在Linux中搜索字符串时,如何只显示每个文件名的一个实例?
将-l
添加到egrep表达式:
egrep -l disco music_file*.txt
从man grep
:
-l, - 带有匹配的文件
抑制正常输出; 而是打印通常打印输出的每个输入文件的名称。 扫描将在第一场比赛中停止。 (-l由POSIX指定。)
grep -l应该适合你。
grep -l pattern files*.txt
从手册页:
-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.)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.