-2

I need to replace the $6 from file A with the $147 from file B. Can anyone help me with the syntax?

I tried with

cut -d, f6 fileA | paste -d, f157 fileB

but it doesnt work. I tried also

awk '$6 fileA = $157 fileB'

but the syntaxis is wrong.

  • 1
    Welcome to the site. Please edit your post to show what you already tried. Also, do you want the replacement to be done line-wise, i.e. replace field 6 of line 1 of file A with field 6 of line 1 on file B anf field 6 on line 2 with field 147 on line 2 etc., or is it a constant value from file B? – AdminBee Sep 17 at 13:12
2

Work backwards. Catalogue the replacements, and then insert them as you parse the 'main' file:

$ awk 'NR == FNR { replacements[NR]=$147 } NR != FNR { $6 = replacements[FNR]; print }' fileB fileA

To save the output to a new file, use a shell redirect as usual:

$ awk 'NR == FNR { replacements[NR]=$147 } NR != FNR { $6 = replacements[FNR]; print }' fileB fileA > newFileC
| improve this answer | |
  • It worked great...but it prints the result. How can I save the edited file? I tried adding at the end of the file > fileC.txt but it didnt work... – Jose Sep 18 at 10:28

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.