如何获得最常出现的 unix 实用程序
how to get the most common occur with unix utilites
以下代码块保存在文件 INP.txt:
wtf.txt|/Users/jaro/documents/inc/face/|
lol.txt|/Users/jaro/documents/inc/linked/|
lol.txt|/Users/jaro/documents/inc/twitter/|
lol.txt|/Users/jaro/documents/inc/face/|
wtf.txt|/Users/jaro/documents/inc/face/|
omg.txt|/Users/jaro/documents/inc/twitter/|
omg.txt|/Users/jaro/documents/inc/linked/|
wtf.txt|/Users/jaro/documents/inc/linked/|
lol.txt|/Users/jaro/documents/inc/twitter/|
wtf.txt|/Users/jaro/documents/inc/linked/|
lol.txt|/Users/jaro/documents/inc/face/|
omg.txt|/Users/jaro/documents/inc/twitter/|
omg.txt|/Users/jaro/documents/inc/face/|
wtf.txt|/Users/jaro/documents/inc/face/|
wtf.txt|/Users/jaro/documents/inc/twitter/|
omg.txt|/Users/jaro/documents/inc/linked/|
omg.txt|/Users/jaro/documents/inc/linked/|
例如,我有这个输入文件(保存在变量中),我的任务是在给定路径中找到最常见的文件。
例如在路径 /Users/jaro/documents/inc/linked/
中查找最常见的文件
预期输出:
omg.txt
我花了几个小时寻找最佳解决方案 - 但没有成功。
p.s。抱歉我的英语不好,我希望你能理解我的问题
编辑:
多个文件出现次数相同 -> 获取出现次数相同的文件
我发现了另一个问题,我的脚本 无法创建任何临时文件(例如 sed -i
可以)...awk 可以吗?
$ awk -F'|' '=="/Users/jaro/documents/inc/linked/" {print }' input.txt |
sort | uniq -c | sort -rn
3 omg.txt
2 wtf.txt
1 lol.txt
这可能是您要找的:
$ awk -F'|' -v tgt="/Users/jaro/documents/inc/linked/" '
==tgt { max=(++cnt[] > max ? cnt[] : max) }
END { for (file in cnt) if (cnt[file]==max) { print file; exit } }
' file
omg.txt
但如果您没有向我们展示预期的输出,这只是一个猜测。
另一种技术:工具管道:
popular() {
grep -F "||" "" | cut -d'|' -f1 | sort | uniq -c | sort -n | sed -n '$s/ \+[0-9]\+ //p'
}
popular /Users/jaro/documents/inc/linked/ INP.txt
打破那一行
popular() {
grep -F "||" "" | # find the lines you're searching for
cut -d'|' -f1 | # extract only the first field
sort | uniq -c | # aggregate them
sort -n | # sort numerically
sed -n '$s/ \+[0-9]\+ //p' # and remove the count
}
这是避免任何显式循环的 AWK 版本:
awk -F '|' '
== "''" {
if (++count[] > max) {
max = count[];
fname =
}
}
END { print fname }
' ""
以下代码块保存在文件 INP.txt:
wtf.txt|/Users/jaro/documents/inc/face/|
lol.txt|/Users/jaro/documents/inc/linked/|
lol.txt|/Users/jaro/documents/inc/twitter/|
lol.txt|/Users/jaro/documents/inc/face/|
wtf.txt|/Users/jaro/documents/inc/face/|
omg.txt|/Users/jaro/documents/inc/twitter/|
omg.txt|/Users/jaro/documents/inc/linked/|
wtf.txt|/Users/jaro/documents/inc/linked/|
lol.txt|/Users/jaro/documents/inc/twitter/|
wtf.txt|/Users/jaro/documents/inc/linked/|
lol.txt|/Users/jaro/documents/inc/face/|
omg.txt|/Users/jaro/documents/inc/twitter/|
omg.txt|/Users/jaro/documents/inc/face/|
wtf.txt|/Users/jaro/documents/inc/face/|
wtf.txt|/Users/jaro/documents/inc/twitter/|
omg.txt|/Users/jaro/documents/inc/linked/|
omg.txt|/Users/jaro/documents/inc/linked/|
例如,我有这个输入文件(保存在变量中),我的任务是在给定路径中找到最常见的文件。
例如在路径 /Users/jaro/documents/inc/linked/
中查找最常见的文件预期输出:
omg.txt
我花了几个小时寻找最佳解决方案 - 但没有成功。
p.s。抱歉我的英语不好,我希望你能理解我的问题
编辑: 多个文件出现次数相同 -> 获取出现次数相同的文件
我发现了另一个问题,我的脚本 无法创建任何临时文件(例如 sed -i
可以)...awk 可以吗?
$ awk -F'|' '=="/Users/jaro/documents/inc/linked/" {print }' input.txt |
sort | uniq -c | sort -rn
3 omg.txt
2 wtf.txt
1 lol.txt
这可能是您要找的:
$ awk -F'|' -v tgt="/Users/jaro/documents/inc/linked/" '
==tgt { max=(++cnt[] > max ? cnt[] : max) }
END { for (file in cnt) if (cnt[file]==max) { print file; exit } }
' file
omg.txt
但如果您没有向我们展示预期的输出,这只是一个猜测。
另一种技术:工具管道:
popular() {
grep -F "||" "" | cut -d'|' -f1 | sort | uniq -c | sort -n | sed -n '$s/ \+[0-9]\+ //p'
}
popular /Users/jaro/documents/inc/linked/ INP.txt
打破那一行
popular() {
grep -F "||" "" | # find the lines you're searching for
cut -d'|' -f1 | # extract only the first field
sort | uniq -c | # aggregate them
sort -n | # sort numerically
sed -n '$s/ \+[0-9]\+ //p' # and remove the count
}
这是避免任何显式循环的 AWK 版本:
awk -F '|' '
== "''" {
if (++count[] > max) {
max = count[];
fname =
}
}
END { print fname }
' ""