Search code examples
bashsedautohotkeydvorak

What is the variable that stores the whole line matching the pattern in sed?


I have a file whose lines are Italian words that end in [aiou]'

gia'
giacche'
giovedi'
giu'

I want to replace these lines with the autohotkey commands that would allow me to write these words with an apostrophe and automatically translate them in their correct versions, that is i want to get

::gia'::già
::giacche'::giacché
::giovedi'::giovedì
::giu'::giù

I know I can accomplish this with sed, but I couldn't get it to work. I know there is a variable to store the whole line, but I can't find it. If it were @ the following might work

sed -e "s/.a'/::@::@\b\bà/" temp
sed -e "s/.i'/::@::@\b\bì/" temp
sed -e "s/.o'/::@::@\b\bò/" temp
sed -e "s/.u'/::@::@\b\bù/" temp

Please note that while the lines are in alphabetical order they DO NOT all start in g, nor there is a pattern in the last two characters of the word.

So do you know what is the variable I'm looking for? Do you think there is a better solution to my problem than the one I'm trying?


Solution

  • The variable you're looking for is &. Perhaps something like:

    sed -e "s/\(.*\)a'/::&::\1à/" temp
    

    You can also string all of your commands together for all five vowels:

    sed -e "s/\(.*\)a'/::&::\1à/" -e "s/\(.*\)e'/::&::\1é/" -e "s/\(.*\)i'/::&::\1ì/" -e "s/\(.*\)o'/::&::\1ò/" -e "s/\(.*\)u'/::&::\1ù/" temp
    

    If you have GNU sed:

    sed -re "s/(.+)a'/::&::\1à/" -e "s/(.+)e'/::&::\1é/" -e "s/(.+)i'/::&::\1ì/" -e "s/(.+)o'/::&::\1ò/" -e "s/(.+)u'/::&::\1ù/" temp