How can I use linux sed command to process Little-endian UTF-16 file

I am working on an application about windows rdp. Now I am having a problem when I try to use the sed command to replace the string of IP address directly in the rdp file. But after executing this command, the original rdp file is distorted.

sed -i "s/address:s:.*/address:s:$(cat check-free-ip.to.rdpzhitong.rdp)/" rdpzhitong.rdp 

I find the file format to be Unicode UTF-16 of lower order.

enter image description here

Can I use the sed command to correctly replace text in files? Or another method to handle this problem?

+4
source share
2 answers

If the file is UTF-16 encoded text (like RDP ), and this is not your current encoding (this is unlikely to be on Linux), then you can before and after processing the file with iconv . For instance:

 iconv -f utf-16 -t us-ascii <rdpzhitong.rdp | sed 's/original/modified/' | iconv -f us-ascii -t utf-16 >rdpzhitong.rdp.modified 
+12
source

if you can cat save the file then you can use sed . there is no harm in trying before asking a question.

if the check-free-ip.to.rdpzhitong.rdp file has any text, you can do this:

 address=$(sed 1q check-free-ip.to.rdpzhitong.rdp) sed -i "s/address:s:.*/address:s:$address/" rdpzhitong.rdp 

also, a little advice. try without the -i switch until you find out that it works.

+1
source

Source: https://habr.com/ru/post/1492316/


All Articles