Skip to main content
2 of 5
added 138 characters in body
cuonglm
  • 158.2k
  • 41
  • 342
  • 420

find and replaced in diferents columns using awk

I try to make a change in a file that correspond in finding a certain value and substituted other value in other different column using awk comand:

INPUT

unit ntype qqatom
  1  'Zr1'  0.0d0
vibration
0
improper
0
unit ntype qqatom
  2  'H1'  0.0d0
vibration
0
improper
0
unit ntype qqatom
  3  'C25'  0.0d0
vibration
0
improper
0
unit ntype qqatom
  4  'O1'  0.0d0

OUTPUT

unit ntype qqatom
  1  'Zr1'  2.222d0
vibration
0
improper
0
unit ntype qqatom
  2  'H1'  3.333d0
vibration
0
improper
0
unit ntype qqatom
  3  'C25'  7.456d0
vibration
0
improper
0
unit ntype qqatom
  4  'O1'  9.99d0

I've try this command:

awk < UiO-66Zr-EH.mof '$2 ~ /Zr1/ {$3 ="2.008.d0"}1''$2 ~ /O25/ {$3 ="-1.179d0"}1''$2 ~ /O1/ {$3 ="-0.582d0"}1''$2 ~ /C25/ {$3 ="-0.121d0"}1''$2 ~ /C13 / {$3 ="-0.002d0"}1''$2 ~ /"C1 "/ {$3 ="0.625d0"}1''$2 ~ /O29/ {$3 ="-0.741d0"}1''$2 ~ /H1/ {$3 ="0.127d0"}1''$2 ~ /H25/ {$3 ="0.495d0"}1' > output

But didn't functional very well :s can i do that with awk and keep the same fomation also?

Joao Macau
  • 225
  • 1
  • 4
  • 11