Linux-awk实践案例
1. 统计 words.txt 频率最高的单词
words.txt格式如下;
sh
the day is sunny the the
the sunny is is- 输出要求:
- 统计所有单词的频率,不允许有重复
- 先单词,后频率,单词和数字之间空一格
- 解答如下
sh
# for every line, execute below content
{
for (i = 1; i < NF; i++) {
words_records[$i] += 1
}
}
# execute all lines
END {
for (word in words_records) {
print word, words_records[word]
}
}words_records类似全局变量,每执行一行,就记录一次$可以理解成一个运算符,取出对应列当中的内容
2. 按照类型切分 netstat.txt
sh
#!/usr/bin/awk
NR != 1 {
if ($6 ~ /TIME|ESTABLISHED/) {
print > "./5.split/1.txt"
} else if ($6 ~ /LISTEN/){
print > "./5.split/2.txt"
} else {
print > "./5.split/3.txt"
}
}- 写法基本上和
C差不错
3. 打印 ls -al 的结果
在
mac系统下,执行ls -al结果如下:
利用
awk打印最后一列,结果如下:
shell
ls -al | awk '{print $NF}' | tail -n +4