从bash上的二进制文件中删除钻石问号 [英] Remove diamond question marks from binary file on bash
问题描述
我正在将串行端口的输入动态写入文件,如下所示:须藤cu -s 19200 -l/dev/ttyUSB0> serialContent.json
I am dynamically writing to a file the input of a serial port, like so: sudo cu -s 19200 -l /dev/ttyUSB0 > serialContent.json
但是当我打开它时,它向我展示了很多菱形问号:
But when I open it, it shows me a lot of diamond question marks:
*#*1 ## * 1 * 0 * 702442501#9 ## * 1 * 0 * 702442501#9 ##
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������*#*1##*1*0*702442501#9##*1*0*702442501#9##
我只想得到以下部分:*#* 1 ## * 1 * 0 * 702442501#9 ## * 1 * 0 * 702442501#9 ##
What I want to get is only this portion: *#*1##*1*0*702442501#9##*1*0*702442501#9##
用vim打开文件时,我得到很多^ @字符.
When I open the file with vim I get a lot of ^@ characters.
我尝试使用sed's | [^ @] || g'serialContent.json> serialContent2.json和sed's | [ ] || g'serialContent.json> serialContent2.json替换字符,不使用运气.
I tried to replace the characters using sed 's|[^@]||g' serialContent.json > serialContent2.json and sed 's|[�]||g' serialContent.json > serialContent2.json with no luck.
这是我通过此命令得到的:
This is what I get with this command:
$ file -bi serialContent.json
application/octet-stream; charset=binary
如何删除这些标记?谢谢!
What can I do to remove those marks? Thanks!
推荐答案
这是替换字符当您拥有不可打印的数据时显示.
This is the replacement character shown when you have non-printable data.
要删除所有不可打印的字符,可以通过 tr -cd'[:print:]'
:
To remove all non-printable characters, you can pipe it through tr -cd '[:print:]'
:
sudo cu -s 19200 -l /dev/ttyUSB0 | tr -cd '[:print:]' > serialContent.json
什么是可打印的取决于您的语言环境.您可能需要先 export LC_ALL = C
,以确保跨计算机的结果一致.
What's considered printable depends on your locale. You may want to export LC_ALL=C
first to ensure consistent results across machines.
这篇关于从bash上的二进制文件中删除钻石问号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!