作为练习,我正在尝试输出字典中每个可能长度存在多少个单词.这是我的代码:
$ awk '{print length}' dico.txt | sort -nr | uniq -c
Run Code Online (Sandbox Code Playgroud)
这是输出:
...
1799 5
427 4
81 3
1 2
Run Code Online (Sandbox Code Playgroud)
我的问题是awk长度为我文件中的每个单词再多一个字母.正确的输出应该是:
1799 4
427 3
81 2
1 1
Run Code Online (Sandbox Code Playgroud)
我检查了我的文件,它后面没有任何空格:
ABAISSA
ABAISSABLE
ABAISSABLES
ABAISSAI
...
Run Code Online (Sandbox Code Playgroud)
所以我觉得awk将换行计算为一个角色,尽管事实并非如此.有什么解决方案吗?或者我做错了什么?
我猜想一想.不是你awk
期待的"U*X"风格换行符(LF),但是你的dico.txt有Windows风格(CR + LF).这很容易给你所有长度的+1.
我接受了你的四个字:
$ cat dico.txt
ABAISSA
ABAISSABLE
ABAISSABLES
ABAISSAI
Run Code Online (Sandbox Code Playgroud)
跑了你的路线:
$ awk '{print length}' dico.txt | sort -nr | uniq -c
1 11
1 10
1 8
1 7
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.现在一样,但是带有windows换行符的dico.txt:
$ cat dico.txt | todos > dico_win.txt
$ awk '{print length}' dico_win.txt | sort -nr | uniq -c
1 12
1 11
1 9
1 8
Run Code Online (Sandbox Code Playgroud)