Linux Shell经典面试题

查空行
awk '/^$/{print NR}' demo1.txt

求某一列的和
awk '{sum+=$2} END {print "求和:"sum}' demo2.txt

数字排序
sort -n -t ' ' -k 2 demo3.txt
sort -t ' ' -k 2nr demo3.txt

搜索指定目录下文件内容
grep -r '666' /root/ | cut -d ':' -f 1 | sort -u

筛选出单词长度大于3的单词
echo "I may not be able to change the past, but I can learn from it." | awk -F "[ ,.]" '{for(i=1;i<=NF;i++){if(length($i)>3){print $i}}}'

单词频次统计及排序
awk -F "[ ,.]" '{for(i=1;i<=NF;i++){s[$i]++}} END {for(key in s){print key, s[key]}}' demo5.txt | sort -k 2nr | head

 

posted @ 2021-05-26 16:09  jingsupo  阅读(353)  评论(0编辑  收藏  举报

欢迎光临