tr -c '[:alnum:]' '[\n*]' < 4300-0.txt | sort | uniq -c | sort -nr | head
以下命令会检索唯一单词以及计数。我想检索标点符号以及唯一字数。
实现这个的方法是什么?
最佳答案
您可以使用 tee
拆分您的输入并分别提取标点符号和数字。
echo "Helo, world!" |
{
tee >(tr -c '[:alnum:]' '\n' >&3) |
tr -c '[:punct:]' '\n'
} 3>&1 |
sed '/^$/d' |
sort | uniq -c | sort -nr | head
应该输出:
1 world
1 Helo
1 !
1 ,
一个简短的 sed
脚本似乎也可以工作:
echo "Helo, world!
OK!" |
sed '
s/\([[:alnum:]]\+\)\([^[:alnum:]]\)/\1\n\2/g
s/\([[:punct:]]\+\)\([^[:punct:]]\)/\1\n\2/g
s/[^[:punct:][:alnum:]]/\n/g
' |
sed '/^$/d' |
sort | uniq -c | sort -nr | head
应该输出:
2 !
1 world
1 OK
1 Helo
1 ,
关于用于检索唯一单词并与标点符号一起计数的 Linux 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60039870/