05-查找、管道和重定向

查找、管道和重定向

  • 查找: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$ 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值