7

I am working on an application about windows rdp. Now I get 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 origin rdp file is garbled.

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

I find that the file's format is Little-endian UTF-16 Unicode.

enter image description here

Can I still use the sed command to replace the text in the files correctly? Or other method to process this problem?

liuan
  • 299
  • 1
  • 3
  • 9

2 Answers2

15

If the file is UTF-16 encoded text (as RDP is), and that is not your current encoding (it's not likely to be on Linux) then you can pre- and post-process the file with iconv. For example:

iconv -f utf-16 -t us-ascii <rdpzhitong.rdp |
 sed 's/original/modified/' |
 iconv -f us-ascii -t utf-16 >rdpzhitong.rdp.modified
Joe
  • 29,416
  • 12
  • 68
  • 88
  • I had some problems using iconv with a UTF16 file. First one was that our file was UTF16 little endian, and as far as I remember, iconv's 'utf-16' is big endian. Then, our file was a Windows file, using CRLF, and the sed I was using (from bash from git from Windows) strips that. Third, the format generated by iconv is not exactly the same (our file had FEFF in hex at the beginning, iconv didn't seem to write those). Our file was used for compiling a project, so it messed up a few things. So it's best to check with a hex editor that the file is only changed where you intended. – fencekicker Mar 14 '22 at 14:34
1

if you can cat the file, then you may use sed. no harm in trying before you ask the question.

if the check-free-ip.to.rdpzhitong.rdp file has any text, you may want to 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 know it's working.

Marty McGowan
  • 356
  • 2
  • 10