我想通过在每行的开头添加行号来修改文件。我发现下面的命令做到这一点:为什么此行编号命令会破坏字符编码?
cat file | perl -pe '$_ = "$. $_"' > file_with_line_numbers
这似乎是工作,但是,当我在vim打开文件时,它的全^ @ ^和M字符的。进一步的调查显示编码已经改变。
> file -bi file
text/plain; charset=utf-16le
> file -bi file_with_line_numbers
application/octet-stream; charset=binary
我在这里错过了什么?
你可以使用nl。这是它的目的。 'nl file> new_file_with_line_numbers' – squiguy
@squiguy,Nope,'nl'将会以完全相同的方式失败。 – ikegami