文本三剑客练习题

显示/proc/meminfo文件中以不区分大小的s开头的行

grep -i ^s /proc/meminfo
 
cat /proc/meminfo|grep –i ^s

显示/etc/passwd中以nologin结尾的行;

grep nologin$ /etc/passwd
 
sed -n '/nologin$/p' passwd

显示/boot/grub/grub.conf文件中以一个或多个空白字符开头的行;

grep ^[[:space:]] /boot/grub2/grub.cfg

显示/etc/sysconfig/network-scripts/ifcfg-eth0文件中的包含了类似IP地址点分十进制数字格式的行

grep -E "([0-9]{1,3}\.){3}\.[0-9]{1,3}"/etc/sysconfig/network-scripts/ifcfg-eth0
grep"[0-9]\{1,3\}\."\{3\}\.[0-9]\{1,3\}/etc/sysconfig/network-scripts/ifcfg-eth0

在text.txt文件中匹配regex的行之后插入一空行

sed ‘/regex/G’ text.txt

替换一个文件/etc/passwd里的这root:x:0:0:root:/root:/bin/bash一行第二个root为test?

sed ‘/root/s/:root:/:test:/’ /etc/passwd

⽇志⽂件a.log,内容是时间顺序递增,从0点到23点的所有⽇志记录,每条时间的⽇志为⼀⾏

2016/06/12 00:00:00 - - 200 190 http://www.a.com/o1html xxxxxxx
2016/06/12 00:00:01 - - 200 390 http://www.b.com/o1html xxxxxxx
2016/06/12 00:00:02 - - 200 490 http://www.v.com/o.html xxxxxxx
2016/06/12 00:00:03 - - 200 890 http://www.a.com/o.html xxxxxxx

2016/06/12 23:59:56 - - 200 320 http://www.3.com/o.jpg xxxxxxx
2016/06/12 23:59:57 - - 200 131 http://www.9.com/o.html xxxxxxx
2016/06/12 23:59:58 - - 200 489 http://www.r.com/o.net xxxxxxx
2016/06/12 23:59:59 - - 200 772 http://www.w.com/o.php xxxxxxx
打印出05:30:35到22:45:55之间的所有日志?

sed -nr ‘/05:30:35/,/22:45:55/p’ file.log

   123abc456
   456def123
   567abc789
   789def567

要求输出:
   456ABC123
   123DEF456
   789ABC567
   567DEF789

sed -r -i 's#(...)(.)(.)(.)(...)#\5\u\2\u\3\u\4\1#g' 22.txt

假设qq. tel文件内容:
12334:13510014336
12345:12334555666
12334:12343453453
12099:13598989899
12334:12345454545
12099:12343454544
分类如下:
[12334]
13510014336
12343453453
...........
[12099]
13598989899
12343454544
 

[root@localhost ~]# cat qq.tel | sort -r | awk -F: '{if (tmp!=$1) {tmp=$1;print "["tmp"]"} ;print $2}'
[12345]
12334555666
[12334]
13510014336
12345454545
12343453453
[12099]
13598989899
12343454544

处理一下文件内容,将域名取出并进行计数排数,如处理: 
http: //www . baidu. com/ index. html
http: / / ww .baidu. com/1.html
http:/ / www . baidu. com/2. html
http: / /post . baidu. com/ index . html
http: / /mp3. baidu. com/ index. html
http:/ / www . baidu. com/3. html
http: / /post.baidu. com/2. html


注意域名字符串中的空格

cat yumi.tex|awk -F / '{print $3}'|sort|sed 's/[[:space:]]//g'|uniq -c

结果如下      

     1 mp3.baidu.com
      2 post.baidu.com
      1 ww.baidu.com
      3 www.baidu.com
 

测试文件内容如下


a+b.sh,2,3,4,5
awk,2,3,4,5
case.sh,2,3,4,5
for.sh,2,3,4,5
grep.all,2,3,4,5
if.sh,2,3,4,5
param.sh,2,3,4,5


sed,2,3,4,5
test,2,3,4,5
while.sh,2,3,4,5


a+b.sh,2,3,4,5
awk,2,3,4,5
case.sh,2,3,4,5
for.sh,2,3,4,5
grep.all,2,3,4,5
if.sh,2,3,4,5
param.sh,2,3,4,5
sed,2,3,4,5
test,2,3,4,5
while.sh,2,3,4,5
 

#统计空白行

方法一
cat input.tex|awk 'BEGIN{x=0};/^$/{x++};END{print x}'
方法二
cat input.tex|grep -c '^$'
方法三
cat input.tex|grep '^$'|wc -l

使用awk输出/etc/passwd文件中uid和gid不相等的行内容

cat /etc/passwd|awk -F : '{if ($3 != $4) {print $0}}'

使用awk输出/etc/passwd文件中uid小于1000的行

[root@gaozhu tmp]# awk -F: '{if ($3<1000) print $0}' passwd

使用awk输出/etc/passwd文件中除过数字的列,同时输出的分隔符为*

[root@gaozhu tmp]# awk -F: -v OFS=* '{print $1,$2,$5,$6,$7}' passwd

删除一个文件中的所有数学

sed -r 's/[0-9]+//g' 9.txt 

显示奇数行 ****

方法一
cat num.tex|sed -n 'p;n'
方法二
cat num.tex|awk '{if(NR%2){print}}'
方法三
cat num.tex|awk 'NR%2'

删除passwd文件中以bin开头的行到nobody开头的行

sed -r '/^bin/,/^nobody/d' /etc/passwd

从指定行开始,每隔两行显示一次

显示用户id为奇数的行

awk -F: '{if($3%2){print $0}}' /etc/passwd

显示系统普通用户,并打印系统用户名和id

awk -F: '{if($3>=1000){print $1,$3}}' /etc/passwd
awk -F: '$3>1000{print $1,$3}' /etc/passwd

输出 b 文件中在 a 文件不同的行


grep -v -f a b

去除 http.conf 文件空行或开头#号的行


grep -E '^#|^$' /etc/httpd.d/conf/httpd.conf

词锚定

root字符之前不能有字符


grep -E "\<root" passwd	

root字符之后不能有字符

grep -E "root\>" passwd  

root前后都不能有字符

grep -iE "\<root\>" passwd   

在系统中列出所有能够使用的用户名称

[root@westos_linux gz]# grep -E "bash$|sh$|tcsh$|csh$|tmux$" /etc/passwd | cut -d : -f 1
root
daddy
westos
manalo

把/opt/messi目录下面所有后缀为sh的改为shell  并删除文件内容的第二行

#!/bin/bash
# please replace ".sh" to ".shell" of current file  and delete second line of every directory

for i in `ls /opt/messi`
do
houzui=`echo $i|awk -F "." '{print $2}'`
new=`echo $i|awk -F "." '{print $1}'`
newname="${new}.shell"
  if [ $houzui = "sh" ]
  then
  mv /opt/messi/$i /opt/messi/$newname
  sed -i '2d' /opt/messi/$newname
  fi


done

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值