0

I have followed your post and used :%s/\\u\(\x\{4\}\)/\=nr2char('0x'.submatch(1),1)/g to translate unicode escape sequences.

However I have to open the file and need to press escape and paste your command. I wanted to write a bash script which will open a file and execute this command automatically. I tried doing this as shown in below but its not working..

script.sh:

# usage bash script.sh file1.csv
#!/bin/bash
cat $1 :%s/\\u\(\x\{4\}\)/\=nr2char('0x'.submatch(1),1)/g

Can you please help how to write a bash script to open a file and do this translation.

Thanks

1

Your code doesn't work because you don't actually invoke Vim anywhere!

Try the following:

#!/bin/bash
vim -c '%s/\\u\(\x\{4\}\)/\=nr2char("0x".submatch(1),1)/g' -c "wq" "$1"

This will open the file, run the command, save, and quit.

See :help -c for more details on how the Vim command works.

Note that I changed the quote characters used in your :substitute command from single quotes to double quotes. This doesn't make any difference to this particular Vim string, and it makes it easier to include the command in the shell script, because you can then use a literal single quoted string and don't have to escape things.

If you actually needed to use single quotes in your Vim command, then you would need to go to further lengths.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.