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?


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:

Dinheiro em caixa
  • 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 '20 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 '20 at 13:13
  • 1
    That worked out just fine, thanks! Oct 2 '20 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.