Question

I want to replace a' into à, e' into è, etc. into a file with a script like this:

#!/bin/sh
if [ -e "$1" ]
then
  sed  's/a'/\à/g' -i "$1";
  sed  's/e'/\è/g' -i "$1";
  sed  's/i'/\ì/g' -i "$1";
  sed  's/o'/\ò/g' -i "$1";
  sed  's/u'/\ù/g' -i "$1";
else
  echo "File not found!"
fi

But I get this error:

Syntax error: Unterminated quoted string

I don't know how to wrote '

Was it helpful?

Solution

Yes there is syntax problem, try your sed command as this:

sed -i "s/a'/à/g" "$1"
sed -i "s/e'/è/g" "$1"
sed -i "s/i'/ì/g" "$1"
sed -i "s/o'/ò/g" "$1"
sed -i "s/u'/ù/g" "$1"

Problem was that you were using nested single quote (quote inside code).

OTHER TIPS

Exit the single quotes, then put an escaped quote, then start another single quoted string:

sed  's/a'\''/\à/g' -i "$1"
awk '{ gsub(/e'\''/,"è"); print}' "$1"

Yet another solution available in bash is $'...' quoting, which does allow escaped single quotes:

sed  $'s/a\'/\à/g' -i "$1";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top