Chubaka
Chubaka

Reputation: 3135

removal hexcode in linux file

We receive a file with a lot of <96> today:

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

We would like to correct it into:

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

I did this in vi, but it says pattern not found:

:%s/<96>/^/g

I tried this

od -c file.txt

and return:

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

which does suggest <96> is <96>. Don't know why I cannot replace <96> in vi. Also I am curious what <96> means here? Any guru could help? Thank you!

Upvotes: 1

Views: 505

Answers (2)

Stun Brick
Stun Brick

Reputation: 1244

(mostly drawn from How to search and replace an unprintable character, read it for more advice)

If you want to find and replace an unprintable character, move your cursor onto it and press
ga
This will show the hex value of the character on the bottom of the screen.

to find and replace that hex character, run
:%s/\%x[Hex]/[Replacement]/g
where [Hex] is the 2 digit hex code you found and [replacement] is whatever you want instead.

If the hex code is 4 digits, run this instead :%s/\%u[Hex]/[Replacement]/g

Upvotes: 1

that other guy
that other guy

Reputation: 123460

You can replace hex character 96 in Vim with:

:%s/\%x96/^/g

Here's :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.

In a script, you can replace it by its octal equivalent with tr:

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

Upvotes: 4

Related Questions