1:使用Linux命令查询file1中空行所在的行号。
[root@localhost ~]# awk '/^$/{print NR}' sed.txt 5
2:有文件chengji.txt内容如下'' 张三 40 李四 50 王五 60 使用Linux命令计算第二列的和并输出。 [root@localhost ~]# awk '{sum+=$2} END{print sum}' chengji.txt 150
3:Shell脚本里如何检查一个文件是否存在?如果不存在该如何处理?
#!/bin/bash
if [ -f file.txt ]
then
echo "文件存在!"
else echo "文件不存在!"
fi
4:用shell写一个脚本,对文本中无序的一列数字排序
[root@localhost ~]# cat test.txt
9 8 7 6 5 4 3 2 10 1
[root@localhost ~]# sort -n test.txt
1 2 3 4 5 6 7 8 9 10
5:请用shell脚本写出查找当前文件夹(/home)下所有的文本文件内容中包含有字符”shen”的文件 名称
[root@localhost ~]#grep -r "shen" /home | cut -d ":" -f1
6: 一个文本文件info.txt的内容如下: aa,201 zz,502 bb,1 ee,42 每行都是按照逗号分隔,其中第二列都是数字,请对该文件按照第二列数字从大到小排列。
[root@localhost ~]# sort -t"," -k 2 -nr info.txt
zz,502
aa,201
ee,42
bb,1
:7:处理以下文件内容,将域名取出并进行计数排序,如处理: 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 ~]# cut -d'/' -f3 test.txt | sort | uniq -c | sort -nr
[root@localhost ~]# sed -r 's#^h.*//(.*)/.*#\1#' test.txt |sort | uniq -c | sort -nr
[root@localhost ~]# awk -F'/' '{print $3}' test.txt | sort | uniq -c | sort -nr
[root@localhost ~]# awk -F'/' '{++IP[$3]};END {for (key in IP) print IP[key],key}' test.txt | sort -n
8、写一个脚本查找最后创建时间是3天前,后缀是\*.log的文件并删除。**
[root@localhost ~]# find / -name “*.log” -ctime +3 -exec rm -f {} \
9、写一个脚本将某目录下大于100k的文件移动至/tmp下。
[root@localhost ~]# for i in `find /test -type f -size +100k`;do cd /test && mv $i /tmp;done
10、写一个脚本进行nginx日志统计,得到访问ip最多的前10个(nginx日志路 径:/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
11、写一个脚本把指定文件里的/usr/local替换为别的目录。
[root@localhost ~]# sed 's:/user/local:/tmp:g' filename