简体   繁体   中英

Replace some text using SED and HEX

I have a big text file with some ugly chars like

He’s lean, he’s mean, he’s got a awesome flamethrower and jetpack – what’s not to love here?

I'd like to replace

’

to

'

to have "He's lean, he's mean" etc

but standard SED command

sed -i 's/’/'/g' file.txt

does not work.

Symbols ’ has HexCode <0432><0402><2122> (looks like that)

I tried something like

sed -i 's/\x432\x402\x2122/'/g' file.txt

but nothing happenned

Thanks a lot for any ideas how to manage that.

This might work for you (GNU sed):

sed 's/\o320\o262\o320\o202\o342\o204\o242/'\''/g' file

To get the octal representation use sed as so:

sed -n l0 file

and insert an o infont of \\xxx strings ( \\oxxx ).

You can't include a single quote in single quotes. Use double-quotes:

sed "s/’/'/g" file.txt

maybe something like

sed -n '1!H;1h
$ {s/.*/’/;G
:a
   s/^\(...\)\(\n.*\)\1/\1\2'/
   t a
   s/^....//p
   }' YourFile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM