Linux下grep、sed、awk、tcpdump、strace用法实例

本文详细介绍了Linux下grep、sed、awk、tcpdump和strace的使用方法与实例。grep用于搜索文本,sed进行文件处理,awk进行文本分析,tcpdump用于网络数据包捕获,strace跟踪进程的系统调用。通过多个实用案例,展示了这些命令的强大功能。
摘要由CSDN通过智能技术生成

1. grep
grep 是全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。
用法:

       grep [OPTIONS] PATTERN [FILE...]
       grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]

首先使用ls命令和输出重定向生成两个用来测试grep命令的文本

root@CGS-21-14:/home/lhp# ls /usr/ > ls_usr.txt
root@CGS-21-14:/home/lhp# ls /etc/ > ls_etc.txt

1). 在ls_usr.txt中搜索含有“bin”的字符串

root@CGS-21-14:/home/lhp# grep bin ls_usr.txt
bin
sbin

2). 从多文件中搜索含有”bin”的字符串

root@CGS-21-14:/home/lhp# grep bin *
ls_etc.txt:bindresvport.blacklist
ls_usr.txt:bin
ls_usr.txt:sbin
root@CGS-21-14:/home/lhp# grep bin ls*
ls_etc.txt:bindresvport.blacklist
ls_usr.txt:bin
ls_usr.txt:sbin

3). 从ls_usr.txt中搜索含“bin”和”BIN”的字符串。

root@CGS-21-14:/home/lhp# echo /BINs >> ls_usr.txt
root@CGS-21-14:/home/lhp# echo /BINABLE >> ls_usr.txt
root@CGS-21-14:/home/lhp# echo /Bining >> ls_usr.txt
root@CGS-21-14:/home/lhp# grep -i bin ls_usr.txt
bin
sbin
/BINs
/BINABLE
/Bining

4). 在ls_usr.txt中搜索以this开头case结尾的字符串

root@CGS-21-14:/home/lhp# echo "this is test case" >> ls_usr.txt
root@CGS-21-14:/home/lhp# grep -i "this.*case" ls_usr.txt
this is test case

5). 在ls_usr.txt中搜索整词is,不像1)中那种含部分,并加上不区分大小写

root@CGS-21-14:/home/lhp# grep -iw "is" ls_usr.txt
this is test case

6). 显示ls_etc.txt中含emacs匹配行前、后、前后 4行

root@CGS-21-14:/home/lhp# grep -A 4 -w emacs ls_etc.txt
emacs
environment
firefox
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值