sse*_*vic 70
perl oneliner会做: perl -i.bak -pe 's/[^[:ascii:]]//g' <your file>
-i说该文件将在原地进行编辑,备份将以扩展名保存.bak.
Iva*_*van 41
# -i (inplace)
sed -i 's/[\d128-\d255]//g' FILENAME
Run Code Online (Sandbox Code Playgroud)
jca*_*314 15
sed -i 's/[^[:print:]]//' FILENAME
Run Code Online (Sandbox Code Playgroud)
此外,这就像dos2unix
Kat*_*age 15
我尝试了所有解决方案,没有任何效果.但是,以下内容:
tr -cd '\11\12\15\40-\176'
Run Code Online (Sandbox Code Playgroud)
我在这里找到的:
https://alvinalexander.com/blog/post/linux-unix/how-remove-non-printable-ascii-characters-file-unix
我的问题需要在一系列管道程序中,而不是直接来自文件,因此根据需要进行修改.
Viv*_*vek 10
尝试tr而不是sed
tr -cd '[:print:]' < file.txt
Run Code Online (Sandbox Code Playgroud)
# -i (inplace)
LANG=C sed -i -E "s|[\d128-\d255]||g" /path/to/file(s)
Run Code Online (Sandbox Code Playgroud)
该LANG=C部分的作用是避免Invalid collation character错误。
基于伊万的回答和帕特里克的评论。
我使用的是非常少的busybox系统,在其中不支持trPOSIX字符类中的范围,因此我必须采用笨拙的老式方法。这是的解决方案sed,从文件中剥离所有不可打印的非ASCII字符:
sed -i 's/[^a-zA-Z 0-9`~!@#$%^&*()_+\[\]\\{}|;'\'':",.\/<>?]//g' FILE
Run Code Online (Sandbox Code Playgroud)