2

I want to replace the inner single quote (ignoring the outer single quote) with an inverted question mark (¿). I am using below awk command, but it's not working. Semicolon is the separator character.

awk -F "'" -v OFS="'" '{for  (i=2; i<=NF; i+=2) gsub("'","\302\277", $i } 1' filename

And the data is:

'gasg'dhsh';'dhdjs'
3
  • 2
    Please show the input and expected output. I'm nut sure I know what is meant by "turning around a question mark". Also, since you use ' as a delimiter, the data, once you parse it, will not contain any '.
    – Kusalananda
    Jun 29, 2018 at 7:32
  • ... and the ' in the argument to gsub() will end the text of the awk program, since the program is single quoted.
    – Kusalananda
    Jun 29, 2018 at 7:40
  • 1
    Turn around question mark means mirror image of questions mark
    – Rahul Jain
    Jun 29, 2018 at 7:50

2 Answers 2

1

To replace all single quotes that are flanked by words with ¿ in a file (assuming bash is used, so that we may use $'...'):

q=$'\302\277'
sed "s/\>'\</$q/g" file

Alternatively

q=$( printf '\302\277' )
sed "s/\>'\</$q/g" file

or

sed "s/\>'\</¿/g" file

With the given input, this would produce

'gasg¿dhsh';'dhdjs'

The pattern \>'\< would match any ' that has a word character immediately before and after. The ; character and start/end of line are not word characters, while both g and d are.


Your code has three main issues:

  1. A single quoted string in the shell may never contain a single quote. The embedded single quote in the call to gsub() will end the single quoted string that is the awk code, producing a syntax error.

  2. Setting the input field separator to a single quote using -F "'" will cause awk to split the input on all single quotes. This means that the awk program will never see any single quotes, and your gsub() will therefore never replace anything.

  3. The string \302\277 is not an inverted question mark, unless interpreted as an escape sequence.

Correcting all of these issues would make the program work, but it would replace all occurrences of single quotes:

$ awk -F ';' -v OFS=';' '{ for (i=1; i<=NF; ++i) gsub("'"'"'", sprintf("\302\277"), $i); print }' file
¿gasg¿dhsh¿;¿dhdjs¿
2
  • Why do you need $'\302\277' in bash? Isn't a simple ¿ better? It does not assume utf-8.
    – user232326
    Jun 29, 2018 at 11:54
  • @Isaac I'll rephrase my previous comment. It's definitely possible to do that. I have added that as an alternative. I had issues pasting in a ¿ in the terminal I was working at while initially writing the answer.
    – Kusalananda
    Jun 29, 2018 at 12:09
0

Assuming that by "Inner single quote" you imply that there are "outer single quotes" and also assuming that those outer qoutes are at the start and end of each field (delimited by ";" ín this case) you can use:

$ awk -F ';' '{OFS=FS;for(j=1;j<=NF;j++){gsub(q,i,$j);sub("^"i,q,$j);sub(i"$",q,$j)}}{print}' q='\047' i='¿'
'gasg¿dhsh';'dhdjs'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .