1.京东
问题1:使用Linux命令查询file1中空行所在的行号
答案:
[用户@hadoop102 datas]$ awk '/^$/{print NR}' sed.txt
5
问题2:有文件chengji.txt内容如下:
张三 40 李四 50 王五 60
使用Linux命令计算第二列的和并输出
答案:
[用户@hadoop102 datas]$ cat chengji.txt | awk -F " " '{sum+=$2} END{print sum}'
150
2.搜狐&和讯网
问题1:Shell脚本里如何检查一个文件是否存在?如果不存在该如何处理?
#!/bin/bash if [ -f file.txt ]; then echo "文件存在!" else echo "文件不存在!" fi
3.新浪
问题1:用shell写一个脚本,对文本中无序的一列数字排序
root@CentOS6-2 ~]# cat test.txt
9 8 7 6 5 4 3 2 10 1
[用户@CentOS6-2 ~]# sort -n test.txt|awk '{a+=$0;print $0}END{print "SUM="a}'
1 2 3 4 5 6 7 8 9 10 SUM=55
4.金和网络
问题1:请用shell脚本写出查找当前文件夹(/home)下所有的文本文件内容中包含有字符”shen”的文件名称
[用户@hadoop102 datas]$ grep -r "shen" /home | cut -d ":" -f 1
/home/用户/datas/sed.txt /home/用户/datas/cut.txt