Question

I'm trying to adapt an article to include it in a LaTeX Document. For that I'm using sed to substitute characters. However I find myself with a problem with some symbols like quotation marks. So for example with this paragraph:

Los problemas de Europa no son los mismos en todos los países. "Alemania no está creciendo rápidamente, pero consiguió evitar la recaída en la recesión", dice Guillen. "En Irlanda, por ejemplo, la economía cayó un 20%. En Francia, la situación no es desesperada, pero el país tampoco es ninguna Alemania". Mientras, Italia y España han vuelto a caer en la recesión, y Reino Unido acaba de anunciar que está nuevamente en recesión.

The " symbol (double quote in a single character) should be changed to a `` if it's found at the beginning of a word but to a '' (they are 2 characters: \x27\x27) if it's at the end. So the resulting paragraph should be (the % sysmbol was also changed):

Los problemas de Europa no son los mismos en todos los países. ``Alemania no está creciendo rápidamente, pero consiguió evitar la recaída en la recesión'', dice Guillen. ``En Irlanda, por ejemplo, la economía cayó un 20\%. En Francia, la situación no es desesperada, pero el país tampoco es ninguna Alemania''. Mientras, Italia y España han vuelto a caer en la recesión, y Reino Unido acaba de anunciar que está nuevamente en recesión.

I imagine that a regexp combining a space symbol and word to match at beginning should work, and a similar one for the end, but I don't know how to do it.

Was it helpful?

Solution

You could check that the " is either at the beginning of a line or preceded by a whitespace:

sed -r 's/(^| )"/\1``/g' filename

If your version of sed doesn't support extended regular expressions, you could say:

sed 's/\(^\| \)"/\1``/g'` filename

For escaping % and possibly other characters like &, $, you could make use of a character class to escape all those in one go:

sed -r 's/([$%])/\\\1/g' filename

The two could be combined too:

sed -r 's/(^| )"/\1``/g; s/([$%])/\\\1/g' filename

EDIT: From your clarification, it seems that you need to say:

sed -r 's/(^| )"/\1``/g;s/"/'"''"'\1/g' filename

OTHER TIPS

This awk should change the " to `` if its in a beginning of a word.

awk '{for (i=1;i<=NF;i++) if ($i~/^"/) sub(/"/,"``",$i)}1' file
Los problemas de Europa no son los mismos en todos los países. ``Alemania no está creciendo rápidamente, pero consiguió evitar la recaída en la recesión", dice Guillen. ``En Irlanda, por ejemplo, la economía cayó un 20%. En Francia, la situación no es desesperada, pero el país tampoco es ninguna Alemania". Mientras, Italia y España han vuelto a caer en la recesión, y Reino Unido acaba de anunciar que está nuevamente en recesión.

It test one by one by one word and see if its starting with ", if yes change it.

For posix

sed "s/^\"/``/;s/ \"/ ``/g;s/\"$/''/;s/\" /''/g" YourFile > TempFile
mv TempFile YourFile

for gnu sed version (not testing machine here to validate)

sed -r "s/\( |^\)\"/\1``/g;s/"\( |$)/''\1/g" YourFile
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top