Linux 从文件中删除控制字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14680100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing Control Characters from a File
提问by Neon Flash
I want to delete all the control characters from my file using linux bash commands.
我想使用 linux bash 命令从我的文件中删除所有控制字符。
There are some control characters like EOF (0x1A) especially which are causing the problem when I load my file in another software. I want to delete this.
有一些控制字符,如 EOF (0x1A),尤其是当我将文件加载到另一个软件时会导致问题。我想删除这个。
Here is what I have tried so far:
这是我迄今为止尝试过的:
this will list all the control characters:
这将列出所有控制字符:
cat -v -e -t file.txt | head -n 10
^A+^X$
^A1^X$
^D ^_$
^E-^D$
^E-^S$
^E1^V$
^F%^_$
^F-^D$
^F.^_$
^F/^_$
^F4EZ$
^G%$
This will list all the control characters using grep:
这将使用 grep 列出所有控制字符:
$ cat file.txt | head -n 10 | grep '[[:cntrl:]]'
+
1
-
-
1
%
-
.
/
matches the above output of cat command.
匹配 cat 命令的上述输出。
Now, I ran the following command to show all lines not containing control characters but it is still showing the same output as above (lines with control characters)
现在,我运行以下命令来显示所有不包含控制字符的行,但它仍然显示与上面相同的输出(带有控制字符的行)
$ cat file.txt | head -n 10 | grep '[^[:cntrl:]]'
+
1
-
-
1
%
-
.
/
here is the output in hex format:
这是十六进制格式的输出:
$ cat file.txt | head -n 10 | grep '[[:cntrl:]]' | od -t x2
0000000 2b01 0a18 3101 0a18 2004 0a1f 2d05 0a04
0000020 2d05 0a13 3105 0a16 2506 0a1f 2d06 0a04
0000040 2e06 0a1f 2f06 0a1f
0000050
as you can see, the hex values, 0x01, 0x18 are control characters.
如您所见,十六进制值 0x01、0x18 是控制字符。
I tried using the tr command to delete the control characters but got an error:
我尝试使用 tr 命令删除控制字符但出现错误:
$ cat file.txt | tr -d "\r\n" "[:cntrl:]" >> test.txt
tr: extra operand `[:cntrl:]'
Only one string may be given when deleting without squeezing repeats.
Try `tr --help' for more information.
If I delete all control characters, I will end up deleting the newline and carriage return as well which is used as the newline characters on windows. How do I delete all the control characters keeping only the ones required like "\r\n"?
如果我删除所有控制字符,我最终会删除换行符和回车符,它们在 Windows 上用作换行符。如何删除所有控制字符,只保留“\r\n”等所需的字符?
Thanks.
谢谢。
回答by Kyle Barbour
Instead of using the predefined [:cntrl:]
set, which as you observed includes \n
and \r
, just list (in octal) the control characters you want to get rid of:
而不是使用预定义的[:cntrl:]
集合,正如您所观察到的那样,它包括\n
and \r
,只需列出(八进制)您想要摆脱的控制字符:
$ tr -d 'grep -o "[[:print:][:space:]]*" in.txt > out.txt
0-1346-7' < file.txt > newfile.txt
回答by kenorb
Try grep
, like:
尝试grep
,例如:
tr -d "[:cntrl:]"
which will print only alphanumeric characters including punctuation characters and space characters such as tab, newline, vertical tab, form feed, carriage return, and space.
它将仅打印字母数字字符,包括标点符号和空格字符,例如制表符、换行符、垂直制表符、换页、回车和空格。
To be less restrictive, and remove only control characters([:cntrl:]
), delete them by:
为了减少限制并仅删除控制字符( [:cntrl:]
),请通过以下方式删除它们:
cat file.txt | tr '\r\n' '56' | tr -d "[:cntrl:]" | tr "56" "\r\n"
If you want to keep \n
(which is part of [:cntrl:]
), then replace it temporarily to something else, e.g.
如果您想保留\n
(这是 的一部分[:cntrl:]
),则将其暂时替换为其他内容,例如
$ cat scriptfile.raw | col -b > scriptfile.clean
回答by Stephen Boston
Based on this answeron unix.stackexchange, this should do the trick:
基于unix.stackexchange上的这个答案,这应该可以解决问题:
##代码##回答by UKMonkey
A little late to the party: cat -v <file>
which I think is the easiest to remember of the lot!
聚会有点晚:cat -v <file>
我认为这是最容易记住的!