查找、管道和重定向
- 查找:grep
- 管道:|
- 重定向:>> 和 >
01.查找grep
- grep 与 find命令不同,grep是查找文件内容,命令格式如下
# 查找字符串是否在文件中,如果在则打印出来
grep 字符串 文件名
例如:查找 test.py 中出现 fu 的行
grep fu test.py -n
02.管道
-
管道的作用是将查找到的数据作为输入,我是这样理解的 ==> 输入 | 输出
-
管道经常配合grep来使用,筛选出需要的内容
例如:查看当前目录下是所有的.py文件中是否存在 1.py
python@ubuntu:~/Desktop$ ls *.py | grep 1.py
03.重定向
-
将查找的内容输出到文本中,不在终端显示
-
>> 表示不删除文本原来内容,将查找结果追加到后面
-
> 删除文本原来内容,然后将查找结果打印到文本中
例子:查找 test.py 中所有出现 hello 的行,并将其打印到find.txt中
python@ubuntu:~/Desktop$ cat find.txt
cat: find.txt: 没有那个文件或目录
python@ubuntu:~/Desktop$ cat test.py
def aa_fu():
print("hello world")
print("hello world")
print("hello world")
print("hello world")
print("hello world")
python@ubuntu:~/Desktop$ grep hello test.py >> find.txt -n
python@ubuntu:~/Desktop$ cat find.txt
2: print("hello world")
3: print("hello world")
4: print("hello world")
5: print("hello world")
6: print("hello world")
python@ubuntu:~/Desktop$