shell实战案例

题目

1.使用Linux命令查询file1中空行所在的行号。

[root@servera last_work]# vim sed.txt
  1 one
  2 
  3 two
  4 
  5 three
  6 four
  7 five
  8 
  9 seven
[root@servera last_work]# awk '/^$/{print NR}' sed.txt 
2
4
8

2.有文件chengji.txt内容如下''
张三 40
李四 50
王五 60
使用Linux命令计算第二列的和并输出。

[root@servera last_work]# awk '{sum+=$2} END{print sum}' chengji.txt 
150

3.Shell脚本里如何检查一个文件是否存在?

[root@servera last_work]# vim file_exist.sh
#!/bin/bash
if [ -e file1.txt ];then
    echo "exist"
else
    echo " not exist"
fi 

4.用shell写一个脚本,对文本中无序的一列数字排序
9 8 7 6 5 4 3 2 1 0 1

**[root@servera last_work]# sort sort.txt 
0 
1
1 
2 
3 
4 
5 
6 
7 
8 
9**

5.请用shell脚本写出查找当前文件夹(/home)下所有的文本文件内容中包含有字符”shen”的文件名称

[root@servera last_work]# grep -r "shen" /home
-r: 递归查找

6.一个文本文件info.txt的内容如下:
aa,201
zz,502
bb,1
ee,42
每行都是按照逗号分隔,其中第二列都是数字,请对该文件按照第二列数字从大到小排列。

[root@servera last_work]# sort -t "," -k 2 info.txt -r -n
zz,502
aa,201
ee,42
bb,1
-t: 指定字段分隔符
-k: 取列
-r: 降序
-n: 按数字排序

7.请用shell脚本创建一个组class、一组用户,用户名为stdX,X从01-30,并归属class组

[root@servera last_work]# vim create_user.sh
#!/bin/bash
groupadd class
for i in `seq -f "%02g" 30`
do
    useradd std$i -g class &> /dev/null
done

8.处理以下文件内容,将域名取出并进行计数排序,如处理:
http://www.baidu.com/more/
http://www.baidu.com/guding/more.html
http://www.baidu.com/events/20060105/photomore.html
http://hi.baidu.com/browse/
http://www.sina.com.cn/head/www20021123am.shtml
http://www.sina.com.cn/head/www20041223am.shtml

[root@servera last_work]# awk -F "/" '{print $3}'  test.txt | sort | uniq -c | sort -nr
      3 www.baidu.com
      2 www.sina.com.cn
      1 hi.baidu.com
uniq -c: 去重并计数

9.写一个脚本查找最后创建时间是3天前,后缀是.log的文件并删除

[root@servera last_work]# find / -name "*.log" -ctime +3 -exec rm -f {} \;

ctime:change time 元数据改变时间
btime: birth time 创建时间
注意:此题说的是创建时间,应该使用btime,但是btime在find里面不支持,即用ctime
+3 : 代表三天以前
-3 : 代表三天以后
exec : 执行程序不产生新的进程
{} : 类似于循环里面的临时变量
\; : exec....\; 固定搭配

10.写一个脚本将某目录下大于100k的文件移动至/tmp下

[root@servera last_work]# for i in `find /test -type f -size +100k`;do cd /test && mv $i /tmp;done

-type f:文件类型为普通文件
-size: 大小

11.写一个脚本进行nginx日志统计,得到访问ip最多的前10个

#路径:/home/logs/nginx/default/access.log)
[root@localhost ~]# awk '{a[$1]++}END{for (j in a) print a[j],j}' /home/logs/nginx/default/access.log|sort -nr|head

head: 默认取前十行
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值