shell
# cat words.txt
the day is sunny the the
the sunny is is
统计一个文本文件words.txt中每个单词出现的频率
# cat words.txt | tr ' ' '\n' | grep -v '^$' | awk '{ ++S[$1] } END { for ( a in S) print a,S[a]}' | sort -r -n -k2
the 4
is 3
sunny 2
day 1
# awk '{ for (i=1;
i<=NF;
i++) ++S[$i]} END { for (a in S) print a,S[a]}' words.txt | sort -r -n -k2
the 4
is 3
sunny 2
day 1
# cat words.txt | xargs -n1 | sort | uniq -c | awk '{print $2,$1}' | sort -r -n -k2
the 4
is 3
sunny 2
day 1
打印文件的第10行
file.txt
# cat a.bash
rowCount=$(awk 'END {print NR}' file.txt)if [ $rowCount -lt 10 ];
then
echo "less than 10 lines"
else
awk '{ if (NR==10) print $0}' file.txt
fi
给定一个文件,转置它的内容
# cat file.txt
name age
alice 21
ryan 30
# cat file.txt | awk '{ for (i=1;
i<=NF;
i++) { if (NR==1) { row[i]=$i} else {row[i]=row[i]" "$i}}} END { for (a in row) {print row[a]}}'
name alice ryan
age 21 30
# cat file.txt
name age
alice 21
ryan 30
# cat a.bash
colCount=$(awk 'END {print NF}' file.txt)for ((i=1;
i<=$colCount;
i++));
do
cat file.txt | cut -d ' ' -f $i | xargs
done
987-123-4567
123 456 7890
(123) 456-7890
你的脚本应当输出下列有效的电话号码:
987-123-4567
(123) 456-7890
# cat file.txt
987-123-4567
123 456 7890
(123) 456-7890
# cat file.txt | grep -E '([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}'
987-123-4567
(123) 456-7890
netstat -antup | awk '/^tcp/ {++S[$NF]} END {for (a in S) print a,S[a]}'
6.查看 打开文件句柄数最多的进程
lsof -n | awk '{print $2}' | sort | uniq -c | sort -nr -k1 | head -n1
一个文本有很多这样的字符串,每一行都删除c和j之间的字符
# echo abcdefghijklmn | sed 's/\([^cj]*\)c[^cj]*j\([^cj]\)/\1\2/g'
abklmn
lsof | awk 'NR>1 {++S[$2]} END { for(a in S) {print a,"\t",S[a]}}'|sort -n -k 1121
23
33
43
53
63
73
83
93
103
113
123
133lsof | awk 'NR>1 {++S[$2]} END { for(a in S) {print a,"\t",S[a]}}'|sort -n -k 2|tail -n 1215785
查看各个状态的TCP Socket的个数
netstat -ant | awk 'NR>2 {++S[$NF]} END {for(a in S) {printf "%-12s %-5s\n",a,S[a]}}'TIME_WAIT24
ESTABLISHED25
LISTEN16
【shell】10.linux中想对一个文件中内容相同的行去重(即多行内容一致就只保留一行)应该输入什么命令执行。
# cat hello.txt
hello world
awk
coding ants
hello world
awk
hello world
awk
coding ants
coding ants
# awk '!a[$0]++' hello.txt
hello world
awk
coding ants
推荐阅读
- Shell-Bash变量与运算符
- Linux下面如何查看tomcat已经使用多少线程
- SSH|SSH 免密
- 探索免费开源服务器tomcat的魅力
- 2020|2020 CATTI英语一级笔译
- Linux|109 个实用 shell 脚本
- Tomcat8带来的坑
- locate搜索
- maven使用tomcat7插件编译jsp出错
- Xshell5|Xshell5 远程连接本地虚拟机Ubuntu16