简体   繁体   中英

How to replace single quote to double quote by awk when under Windows?

I tried gsub() but it didn't work. Here is the command:

awk -v a="'"  b=" \" "  "gsub(a,b,$0){print $0}" D:\re.json

or anybody could tell me what is the right escape character in Windows command line?

As suggested, put the script into a file. Referring to Windows, we would understand the context to be Windows batch-files (rather than say, mingw which has its own problems with command-line scripting). For Windows batch files

  • double-quotes are eaten before the program sees them
  • there is no well-documented way to escape double-quotes
  • single-quotes are passed as-is to the program (and are not, as in Bourne shell, used for quoting).

Here are a few links where those issues are discussed:

So the rules are entirely different from what awk needs for this construction. The last link by the way gives some clues that you might use to devise a workaround (at the expense of readability). It is also discussed in Escaping Double Quotes in Batch Script .

As a separate file, the script would also be more readable, eg, call that foo.awk :

gsub("'"," \" ",$0){print $0}

and use it as

awk -f foo.awk D:\re.json

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