小智 10
此外grep,您还可以使用其他实用程序,例如awk或sed
这里有几个例子。假设您要is在名为GPL.
您的示例文件
user@linux:~$ cat -n GPL
1 The GNU General Public License is a free, copyleft license for
2 The licenses for most software and other practical works are designed
3 the GNU General Public License is intended to guarantee your freedom to
4 GNU General Public License for most of our software;
user@linux:~$
Run Code Online (Sandbox Code Playgroud)
1.grep
user@linux:~$ grep is GPL
The GNU General Public License is a free, copyleft license for
the GNU General Public License is intended to guarantee your freedom to
user@linux:~$
Run Code Online (Sandbox Code Playgroud)
2.awk
user@linux:~$ awk /is/ GPL
The GNU General Public License is a free, copyleft license for
the GNU General Public License is intended to guarantee your freedom to
user@linux:~$
Run Code Online (Sandbox Code Playgroud)
3. sed
user@linux:~$ sed -n '/is/p' GPL
The GNU General Public License is a free, copyleft license for
the GNU General Public License is intended to guarantee your freedom to
user@linux:~$
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助
/ tmp / myfile
first line text
wanted text
other text
Run Code Online (Sandbox Code Playgroud)
命令
$ grep -n "wanted text" /tmp/myfile | awk -F ":" '{print $1}'
2
Run Code Online (Sandbox Code Playgroud)
所述的grep命令的家族(包括egrep的,fgrep一样)是用于这个通常的解决方法.
$ grep pattern filename
Run Code Online (Sandbox Code Playgroud)
如果您正在搜索源代码,那么ack可能是更好的选择.它将自动搜索子目录并避免您通常不搜索的文件(对象,SCM目录等)