1

I have the following kind of text:

$OU = 'or';
$DINHEIRO_CAIXA = 'Cash balance';
$PAGINA = 'Page';
$DE = 'from';
$ENVIAR = 'Send';
$PRINCIPAL = 'Main';

I've already got all the text within the single quotation marks using

cat arquivo | grep -oP "'[^']+'" | tr -d "'"

This extracted text was then translated to Portuguese, but now I don't know how to replace the original text between single quotation marks with the translated text, I was looking for something like

$OU = 'ou';
$DINHEIRO_CAIXA = 'Dinheiro em caixa';
$PAGINA = 'Pagina';
$DE = 'de';
$ENVIAR = 'enviar';
$PRINCIPAL = 'Principal';

I've tried using grep -F to replace the file itself, but it failed miserably because the pattern didn't match since I don't have the quotation marks on the new file, is there a way to do this?

1 Answer 1

1

Using awk:

awk -F\' '
    { getline translate_text <"trans_file";
      print $1, translate_text, $3;
    }' OFS=\' original_file

original_file is your original file; trans_file is your translated of original_file as in below format:

ou
Dinheiro em caixa
Pagina
de
enviar
Principal
3
  • This adds a new line before the closing quotation mark when generating a new file, is there a way to stop this from happening? Oct 2, 2020 at 13:11
  • 1
    I think you would need to do dos2unix trans_file for that (if you generated that file in Windows and now you are working on Linux) Oct 2, 2020 at 13:13
  • 1
    That worked out just fine, thanks! Oct 2, 2020 at 13:14

You must log in to answer this question.

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