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

1.4k views Asked by At

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?

2

There are 2 answers

4
Steve On BEST ANSWER

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
0
potong On

This might work for you (GNU sed):

sed -r '1{x;s/^/aàeéiìoòuù/;x};/[aeiou]'\''$/!b;G;s/((.*)(.).)\n.*\3(.).*/::\1::\2\4/' file

Explanation:

  • 1{x;s/^/aàeéiìoòuù/;x} store a lookup table in the hold space (HS) at the beginning of the file
  • /[aeiou]'\''$/!b bail out unless the desired word ending
  • G append a newline and the contents of the HS to the pattern space (PS)
  • s/((.*)(.).)\n.*\3(.).*/::\1::\2\4/ use the last but one character in the original word as a lookup in the appended lookup table and generate the required output