uniq终端命令不起作用



我正在学习如何使用终端命令uniq。我使用的是mac,bash-shell(unix)。

这是我的文本文件"terminal.txt":

this is a line  
this is a line  
this is a line
this is also a line  
this is also a line 
this is not a line

我在这里找到了一个使用uniq的例子http://www.computerhope.com/unix/uuniq.htm所以这就是我想要使用命令的方式。我没有把我手动写的文本复制到文本文件中。但是,当我键入时:

uniq terminal.txt

我收到这个错误消息:

uniq: terminal.txt: Illegal byte sequence

经过一些谷歌搜索,我发现我应该在uniq:前面写LC_ALL=C

LC_ALL=C uniq terminal.txt

但当我输入这个时,我得到的输出是:

??t

这不是正确的输出。我不知道自己做错了什么,在谷歌上也找不到答案。

我有谁知道我做错了什么?

更新:

这是od -c terminal.txt:的结果

0000000  377 376   t     h     i     s           i     s  
0000020          a           l     i     n     e        
0000040   n     t     h     i     s           i     s  
0000060          a           l     i     n     e        
0000100   n     t     h     i     s           i     s  
0000120          a           l     i     n     e    n  
0000140   n     t     h     i     s           i     s  
0000160          a     l     s     o           a        
0000200    l     i     n     e          n     t     h  
0000220    i     s           i     s           a     l  
0000240    s     o           a           l     i     n  
0000260    e          n    n     t     h     i     s  
0000300          i     s           n     o     t        
0000320    a           l     i     n     e    n          
0000336

这是file terminal.txt:的结果

terminal.txt: Little-endian UTF-16 Unicode text

cat terminal.txt:

??this is a line
this is a line 
this is a line

this is also a line 
this is also a line 

this is not a line

您的文件是用UTF-16编码的,uniq无法处理。

要将文件转换为UTF-8,请执行以下操作:

iconv -f utf-16 -t utf-8 terminal.txt > terminal2.txt

然后uniq命令应该可以处理新创建的文件。

为了解决这个问题,您应该了解编辑器设置并更改它们,以便将文件存储为UTF-8而不是UTF-16。

您可能还想熟悉可以在命令行上运行的简单编辑器,如vi/vim、emacs或nano,尽管这些编辑器一开始需要一些时间来适应。这些编辑器通常会创建简单的文本文件,而不会出现用于创建文件的编辑器所带来的问题。

最新更新