【问题标题】:removal hexcode in linux file删除linux文件中的十六进制代码
【发布时间】:2018-12-24 08:52:50
【问题描述】:

我们今天收到一个包含很多 的文件:

A^Y^ABC^EFG^HIJK<96>this - PM^0^0^0^456^-123^0^0^0^0^0^0^0^08/10/18

我们想把它改成:

A^Y^ABC^EFG^HIJK^this - PM^0^0^0^456^-123^0^0^0^0^0^0^0^08/10/18

我在 vi 中执行此操作,但显示未找到模式:

:%s/<96>/^/g

我试过了

od -c file.txt

然后返回:

0000000   A   ^   Y   ^   A   B  C   ^   E   F    G
0000020   ^   H   I   J   K   <   9   6   >   t   h   i    
0000040   s       -       P
0000060   M   ^   0   ^   0   ^   0   ^   4   6   6   ^   -   1   2   3
0000100   ^   0   ^   0   ^   0   ^   0   ^   0   ^   0   ^   0   ^   0
0000120   8   /   1   0   /   1   8  \n

确实表明 是 。不知道为什么我不能在 vi 中替换 。我也很好奇 在这里是什么意思?有哪位大师可以帮忙吗?谢谢!

【问题讨论】:

  • 如果你这样标记,你可能会得到更多vim 大师。
  • file.txt 是您最初尝试在 vim 中编辑的那个吗? od 输出与您的 vim 体验相矛盾,因此请确保使用相同的文件。具体来说,不要尝试复制粘贴数据。
  • 同意@thatotherguy:如果你在文件中有一个字面的四个字符&lt;96&gt;,那么你在问题中的vim替换应该可以工作。

标签: shell vim awk sed encoding


【解决方案1】:

你可以将 Vim 中的十六进制字符 96 替换为:

:%s/\%x96/^/g

这里是:help regex

                            /\%d /\%x /\%o /\%u /\%U E678

\%d123  Matches the character specified with a decimal number.  Must be
        followed by a non-digit.
\%o40   Matches the character specified with an octal number up to 0377.
        Numbers below 040 must be followed by a non-octal digit or a non-digit.
\%x2a   Matches the character specified with up to two hexadecimal characters.
\%u20AC Matches the character specified with up to four hexadecimal
        characters.
\%U1234abcd     Matches the character specified with up to eight hexadecimal
        characters.

在脚本中,您可以将其替换为与 tr 等效的八进制数:

LC_ALL=C tr '\226' '^'  <myfile.txt  >newfile.txt

【讨论】:

  • 也可以使用&lt;C-v&gt;输入这样的字符
【解决方案2】:

(主要来自How to search and replace an unprintable character,阅读它以获得更多建议)

如果要查找和替换不可打印的字符,请将光标移到该字符上并按
ga
这将在屏幕底部显示字符的十六进制值。

要查找并替换该十六进制字符,请运行
:%s/\%x[Hex]/[Replacement]/g
其中[Hex] 是您找到的两位十六进制代码,[replacement] 是您想要的任何代码。

如果十六进制代码是 4 位数字,请改为运行此代码 :%s/\%u[Hex]/[Replacement]/g

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-05
    • 2018-09-27
    • 2019-01-01
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    相关资源
    最近更新 更多