简体   繁体   中英

UNIX Sed Command to Replace ` with '

My file test.csv contain Record `Ashish` . I want to globally replace it with single quote 'Ashish' .

I tried this command:

sed 's/`/'/g' test.csv 

but it is not working. Please advise.

tr是替换字符的正确工具:

echo '`' | tr \` \'

Try using double quotes " on the outside and escaping the backtic.

sed "s/\`/'/g" Test.in > Test.out

Also, if you want to change the original file, you have to use the -i flag.

sed -i "s/\`/'/g" Test.in 

你需要转义'`字符:

sed s/\'/\`/g test.csv > out.csv

The reason that fails is because it is not possible to place a single quote in a single quoted string: https://www.gnu.org/software/bash/manual/bashref.html#Single-Quotes

The workaround is this:

  • sed 's/`/'"'"'/g'
    • concatenate a single-quoted string ( 's/`/' ) with a double-quoted string ( "'" ) and another single-quoted string ( '/g' ), or
  • sed 's/`/'\\''/g'
    • concatenate a single-quoted string with a literal single quote and another single-quoted string, or
  • other quoting strategies given in other answers

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