1.使用Linux命令查询file1中空行所在的行号。
[root@localhost test]# cat file_test
aaa
bbb
ccc
ddd
fff
ggg
[root@localhost test]# awk '/^$/{print NR}' file_test
3
5
7
10
2.有文件chengji.txt内容如下''
张三 40
李四 50
王五 60
使用Linux命令计算第二列的和并输出。
[root@localhost test]# awk '{print $2}' chengji.text
40
50
60
3.Shell脚本里如何检查一个文件是否存在?
[root@localhost test]# test -e file_test;echo $?
0 #0为存在
[root@localhost test]# test -e file_fasle;echo $?
1
[root@localhost test]# test -e chengji.text;echo $?
0
4.用shell写一个脚本,对文本中无序的一列数字排序
9 8 7 6 5 4 3 2 1 0 1
[root@localhost test]# cat number.text
9
8
6
7
4
7
0
1
0
10
5
13
11
[root@localhost test]# sort -n number.text
0
0
1
4
5
6
7
7
8
9
10
11
13
5.请用shell脚本写出查找当前文件夹(/home)下所有的文本文件内容中包含有字符”shen”的文件名称
[root@localhost test]# grep -r "shen" /home -l
6.一个文本文件info.txt的内容如下:
aa,201
zz,502
bb,1
ee,42
每行都是按照逗号分隔,其中第二列都是数字,请对该文件按照第二列数字从大到小排列。
[root@localhost test]# cut -d "," -f2 info.text|sort -n
1
42
201
502
7.请用shell脚本创建一个组class、一组用户,用户名为stdX,X从01-30,并归属class组
[root@localhost test]# vim class_group.sh
grep ^class /etc/group &> /dev/null || groupadd class
for i in {01..30}
do
id std$i &> /dev/null || useradd -G class std$i
done
[root@localhost test]# bash class_group.sh
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@localhost test]# sed -r 's#^h.*//([A-Za-z0-9.]*)/.*#\1#' domain_name.text|uniq -c|sort -nr
3 www.baidu.com
2 www.sina.com.cn
1 hi.baidu.com
[root@localhost test]# cut -d "/" -f3 domain_name.text|uniq -c|sort -nr
3 www.baidu.com
2 www.sina.com.cn
1 hi.baidu.com
[root@localhost test]# awk -F "/" '{print $3}' domain_name.text|uniq -c|sort -nr
3 www.baidu.com
2 www.sina.com.cn
1 hi.baidu.com
[root@localhost test]# awk -F "/" '{++IP[$3]};END{for(key in IP) print IP[key],key}' domain_name.text |sort -nr
3 www.baidu.com
2 www.sina.com.cn
1 hi.baidu.com
9.写一个脚本查找最后创建时间是3天前,后缀是.log的文件并删除
[root@localhost ~]# find / -name “*.log” -ctime +3 -exec rm -f {} \;
10.写一个脚本将某目录下大于100k的文件移动至/tmp下
[root@localhost test]# vim mv_touch.sh
for i in `find /test -type f -size +100k`
do
cd /test && mv $i /tmp
done
11.写一个脚本进行nginx日志统计,得到访问ip最多的前10个
[root@localhost test]# awk '{++a[$1]};END{for(i in a) print a[i];i}' /home/logs/nginx/defalt/access.log|sort -nr|head -10