# 企业真实面试题(重点)

# 京东

问题1:使用Linux命令查询file1中空行所在的行号

[bigdata@hadoop01 datas]$ awk '/^$/ {print NR}' sed.txt  
5
1
2

问题2:有文件chengji.txt内容如下:

张三 40

李四 50

王五 60

使用Linux命令计算第二列的和并输出

[bigdata@hadoop01 datas]$ cat chengji.txt 
张三 40
李四 50
王五 60
[bigdata@hadoop01 datas]$ cat chengji.txt | awk -F " " '{sum+=$2} END{print sum}'
150
1
2
3
4
5
6

# 搜狐&和讯网

问题1:Shell脚本里如何检查一个文件是否存在?如果不存在该如何处理?

#!/bin/bash
if [ -e file.txt ];then
	echo "文件存在"
else
	echo "文件不存在"
fi
1
2
3
4
5
6

# 新浪

问题1:用shell写一个脚本,对文本中无序的一列数字排序

点击查看代码
[bigdata@hadoop01 datas]$ cat test.txt 
9
8
7
6
5
4
3
2
10
1
[bigdata@hadoop01 datas]$ 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 金和网络

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

[bigdata@hadoop01 datas]$ grep -r shen /home | cut -d ":" -f 1
/home/bigdata/datas/cut.txt
/home/bigdata/datas/sed.txt
1
2
3

# 统计词频

count.txt 内容如下:

good good study
day day day up
1
2

以词频降序排列输出:

day 3
good 2
study 1
up 1
1
2
3
4

答案:

[bigdata@hadoop01 shell]$ cat count.txt | xargs -n1 | sort | uniq -c | awk '{print $2" "$1}'
day 3
good 2
study 1
up 1
1
2
3
4
5