使用 sed 剥离十六进制字节 - 不匹配

发布于 2024-09-13 13:24:26 字数 1139 浏览 12 评论 0原文

我有一个包含两个非 ascii 字节(0xFF 和 0xFE)的文本文件:

??58832520.3,ABC
348384,DEF

该文件的十六进制是:

FF FE 35 38 38 33 32 35 32 30 2E 33 2C 41 42 43 0A 33 34 38 33 38 34 2C 44 45 46

巧合的是 FF 和 FE 恰好是前导字节(它们存在于整个文件中,尽管似乎总是在开头)一条线)。

我试图用 sed 删除这些字节,但我所做的一切似乎都与它们不匹配。

$ sed 's/[^a-zA-Z0-9\,]//g' test.csv 
??588325203,ABC
348384,DEF

$ sed 's/[a-zA-Z0-9\,]//g' test.csv 
??.

主要问题:如何剥离这些字节?
额外问题:上面的两个正则表达式是直接否定,因此其中一个在逻辑上必须过滤掉这些字节,对吧?为什么这两个正则表达式都匹配 0xFF 和 0xFE 字节?

更新:删除一系列十六进制字节的直接方法(由下面的两个答案建议)似乎从每行中删除第一个“合法”字节并留下我想要获取的字节删除:

$sed 's/[\x80-\xff]//' test.csv
??8832520.3,ABC
48384,DEF

FF FE 38 38 33 32 35 32 30 2E 33 2C 41 42 43 0A 34 38 33 38 34 2C 44 45 46 0A

注意每行开头缺少“5”和“3”,并且新的 0A 添加到文件末尾。

更大的更新:这个问题似乎是系统特定的。该问题是在 OSX 上观察到的,但这些建议(包括上面我原来的 sed 语句)在 NetBSD 上的效果正如我所期望的那样。

解决方案:通过 Perl,同样的任务似乎很容易:

$ perl -pe 's/^\xFF\xFE//' test.csv
58832520.3,ABC
348384,DEF

但是,我将保留这个问题,因为这只是一个解决方法,并且没有解释 sed 的问题是什么。

I have a text file with two non-ascii bytes (0xFF and 0xFE):

??58832520.3,ABC
348384,DEF

The hex for this file is:

FF FE 35 38 38 33 32 35 32 30 2E 33 2C 41 42 43 0A 33 34 38 33 38 34 2C 44 45 46

It's coincidental that FF and FE happen to be the leading bytes (they exist throughout my file, although seemingly always at the beginning of a line).

I am trying to strip these bytes out with sed, but nothing I do seems to match them.

$ sed 's/[^a-zA-Z0-9\,]//g' test.csv 
??588325203,ABC
348384,DEF

$ sed 's/[a-zA-Z0-9\,]//g' test.csv 
??.

Main question: How do I strip these bytes?
Bonus question: The two regex's above are direct negations, so one of them logically has to filter out these bytes, right? Why do both of these regex's match the 0xFF and 0xFE bytes?

Update: the direct approach of stripping out a range of hex byte (suggested by two answers below) seems to strip out the first "legit" byte from each line and leave the bytes I'm trying to get rid of:

$sed 's/[\x80-\xff]//' test.csv
??8832520.3,ABC
48384,DEF

FF FE 38 38 33 32 35 32 30 2E 33 2C 41 42 43 0A 34 38 33 38 34 2C 44 45 46 0A

Notice the missing "5" and "3" from the beginning of each line, and the new 0A added to the end of the file.

Bigger Update: This problem seems to be system-specific. The problem was observed on OSX, but the suggestions (including my original sed statement above) work as I expect them to on NetBSD.

A solution: This same task seems easy enough via Perl:

$ perl -pe 's/^\xFF\xFE//' test.csv
58832520.3,ABC
348384,DEF

However, I'll leave this question open since this is only a workaround, and doesn't explain what the problem was with sed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

西瑶 2024-09-20 13:24:27
sed 's/[^ -~]//g'

或者正如其他答案所暗示的那样,

sed 's/[\x80-\xff]//g'

请参阅 sed 信息页面的 第 3.9 节。这一章的标题是“逃脱”。

编辑 对于 OSX,本机语言设置是 en_US.UTF-8

尝试

LANG='' sed 's/[^ -~]//g' myfile

这在 osx 机器上工作,我不完全确定为什么它在 UTF-8 中不起作用

sed 's/[^ -~]//g'

or as the other answer implies

sed 's/[\x80-\xff]//g'

See section 3.9 of the sed info pages. The chapter entitled escapes.

Edit for OSX, the native lang setting is en_US.UTF-8

try

LANG='' sed 's/[^ -~]//g' myfile

This works on an osx machine here, I'm not entirely sure why it does not work when in UTF-8

猫九 2024-09-20 13:24:27

这将删除以特定字节 FF FE 开头的所有行。

sed -e 's/\xff\xfe//g' hexquestion.txt

否定的正则表达式不起作用的原因是 [] 指定了字符类。 sed 假设一个特定的字符集,可能是 ascii。文件中的这些字符不是 7 位 ascii 字符,因为它们都以 F 开头。 sed 不知道如何处理这些字符。上面的解决方案不使用字符类,因此它应该在平台和字符集之间更具可移植性。

This will strip out all lines that begin with the specific bytes FF FE

sed -e 's/\xff\xfe//g' hexquestion.txt

The reason that your negated regexes aren't working is that the [] specifies a character class. sed is assuming a particular character set, probably ascii. These characters in your file aren't 7 bit ascii characters, as they both begin with F. sed doesn't know how to deal with these. The solution above doesn't use character classes, so it should be more portable between platforms and character sets.

骑趴 2024-09-20 13:24:27

文件开头的 FFFE 字节称为“字节顺序标记 (BOM)”。它可以出现在 Unicode 文本流的开头以指示文本的字节顺序。 FF FE 表示 Little Endian 中的 UTF-16

下面是常见问题解答的摘录:

问:我应该如何处理 BOM?

答:以下是一些需要遵循的准则:

  1. 特定协议(例如 Microsoft 针对 .txt 文件的约定)可能需要在某些 Unicode 数据流(例如文件)上使用 BOM。当您需要遵守此类协议时,请使用 BOM。
  2. 某些协议允许在未标记文本的情况下使用可选的 BOM。在那些情况下,
    • 如果已知文本数据流是纯文本,但编码未知,则可以使用 BOM 作为签名。如果没有 BOM,编码可以是任何内容。
    • 如果已知文本数据流是纯 Unicode 文本(但不知道哪种字节序),则可以使用 BOM 作为签名。如果没有 BOM,文本应被解释为 big-endian。
  3. 一些面向字节的协议期望在文件开头使用 ASCII 字符。如果这些协议使用 UTF-8,则应避免使用 BOM 作为编码形式签名。
  4. 如果数据流的精确类型已知(例如 Unicode big-endian 或 Unicode Little-endian),则不应使用 BOM。特别是,每当数据流声明为 UTF-16BE、UTF-16LE、UTF-32BE 或 UTF-32LE 时,都不得使用 BOM。

参考文献

另请参阅

相关问题

The FF and FE bytes at the beginning of your file is what is called a "byte order mark (BOM)". It can appear at the start of Unicode text streams to indicate the endianness of the text. FF FE indicates UTF-16 in Little Endian

Here's an excerpt from the FAQ:

Q: How I should deal with BOMs?

A: Here are some guidelines to follow:

  1. A particular protocol (e.g. Microsoft conventions for .txt files) may require use of the BOM on certain Unicode data streams, such as files. When you need to conform to such a protocol, use a BOM.
  2. Some protocols allow optional BOMs in the case of untagged text. In those cases,
    • Where a text data stream is known to be plain text, but of unknown encoding, BOM can be used as a signature. If there is no BOM, the encoding could be anything.
    • Where a text data stream is known to be plain Unicode text (but not which endian), then BOM can be used as a signature. If there is no BOM, the text should be interpreted as big-endian.
  3. Some byte oriented protocols expect ASCII characters at the beginning of a file. If UTF-8 is used with these protocols, use of the BOM as encoding form signature should be avoided.
  4. Where the precise type of the data stream is known (e.g. Unicode big-endian or Unicode little-endian), the BOM should not be used. In particular, whenever a data stream is declared to be UTF-16BE, UTF-16LE, UTF-32BE or UTF-32LE a BOM must not be used.

References

See also

Related questions

野鹿林 2024-09-20 13:24:27

要表明这不是 Unicode BOM 的问题,而是八位字符与七位字符的问题并与区域设置相关,请尝试以下操作:

显示所有字节:

$ printf '123 abc\xff\xfe\x7f\x80' | hexdump -C
00000000  31 32 33 20 61 62 63 ff  fe 7f 80                 |123 abc....|

sed 删除用户区域设置中非字母数字的字符。请注意,空格和 0x7f 已被删除:

$ printf '123 abc\xff\xfe\x7f\x80'|sed 's/[^[:alnum:]]//g' | hexdump -C
00000000  31 32 33 61 62 63 ff fe  80                       |123abc...|

让 sed 删除 C 语言环境中非字母数字的字符。请注意,仅保留“123abc”:

$ printf '123 abc\xff\xfe\x7f\x80'|LANG=C sed 's/[^[:alnum:]]//g' | hexdump -C
00000000  31 32 33 61 62 63                                 |123abc|

To show that this isn't an issue of the Unicode BOM, but an issue of eight-bit versus seven-bit characters and tied to the locale, try this:

Show all the bytes:

$ printf '123 abc\xff\xfe\x7f\x80' | hexdump -C
00000000  31 32 33 20 61 62 63 ff  fe 7f 80                 |123 abc....|

Have sed remove characters that aren't alpha-numeric in the user's locale. Notice that the space and 0x7f are removed:

$ printf '123 abc\xff\xfe\x7f\x80'|sed 's/[^[:alnum:]]//g' | hexdump -C
00000000  31 32 33 61 62 63 ff fe  80                       |123abc...|

Have sed remove characters that aren't alpha-numeric in the C locale. Notice that only "123abc" remains:

$ printf '123 abc\xff\xfe\x7f\x80'|LANG=C sed 's/[^[:alnum:]]//g' | hexdump -C
00000000  31 32 33 61 62 63                                 |123abc|
小ぇ时光︴ 2024-09-20 13:24:27

在 OS X 上,字节顺序标记可能被读取为单个字。根据字节顺序,尝试使用 sed 's/^\xfffe//g'sed 's/^\xfeff//g'

On OS X, the Byte Order Mark is probably being read as a single word. Try either sed 's/^\xfffe//g' or sed 's/^\xfeff//g' depending on endianess.

莫言歌 2024-09-20 13:24:27

您可以使用 \xff \xfE 获取十六进制代码,然后将其替换为空。

You can get the hex codes with \xff \xfE and replace it by nothing.

葬心 2024-09-20 13:24:27

作为替代方案,您可以使用 ed(1):

printf '%s\n' H 
g/[\xff\xfe]/s///g' ',p' | ed -s test.csv

printf '%s\n' H 
g/[\xff\xfe]/s///g' wq | ed -s test.csv  # in-place edit

As an alternative you may used ed(1):

printf '%s\n' H 
g/[\xff\xfe]/s///g' ',p' | ed -s test.csv

printf '%s\n' H 
g/[\xff\xfe]/s///g' wq | ed -s test.csv  # in-place edit
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文