linux - Linking two arguments - Shell Script -
i'm writing shell script, , here example input file.
1 n 0.000000 0.000000 0.000000 0 2 5 6 7 2 c 0.000000 0.000000 1.480000 0 1 3 8 9 3 c 1.430212 0.000000 2.000555 0 2 4 13 4 o 2.255128 -0.786666 1.463647 0 3 5 h -0.693210 0.693210 -0.347159 0 1 6 h -0.252566 -0.947254 -0.347159 0 1 7 h 0.947254 0.252566 -0.347159 0 1 8 h -0.501852 0.899485 1.804762 0 2 9 c -0.697228 -1.265943 2.006027 0 2 10 11 12 10 h -0.698163 -1.267642 3.117025 0 9 11 h -0.159269 -2.172191 1.654408 0 9 12 h -1.750698 -1.295700 1.654408 0 9 13 o 1.717017 0.786666 2.942096 0 3
what want link first column second one, 1
mean n
, 2
mean c
, etc. replace values in last columns.
is possible?
thank you!
$ awk 'fnr==nr{a[$1]=$2; next;} {for (i=8;i<=nf;i++) $i=a[$i]; print;}' file file | column -t 1 n 0.000000 0.000000 0.000000 0 2 h h h 2 c 0.000000 0.000000 1.480000 0 1 c h c 3 c 1.430212 0.000000 2.000555 0 2 o o 4 o 2.255128 -0.786666 1.463647 0 3 5 h -0.693210 0.693210 -0.347159 0 1 6 h -0.252566 -0.947254 -0.347159 0 1 7 h 0.947254 0.252566 -0.347159 0 1 8 h -0.501852 0.899485 1.804762 0 2 9 c -0.697228 -1.265943 2.006027 0 2 h h h 10 h -0.698163 -1.267642 3.117025 0 9 11 h -0.159269 -2.172191 1.654408 0 9 12 h -1.750698 -1.295700 1.654408 0 9 13 o 1.717017 0.786666 2.942096 0 3
how works
the awk script reads file twice. on first time through, creates array translate numbers chemical symbols. on second time through, writes out lines new chemical symbols.
fnr==nr{a[$1]=$2; next;}
on first time through, create array
a
has number-to-chemical info. then, skip rest of commands , jumpnext
line.for (i=8;i<=nf;i++) $i=a[$i]
on second read through, use array
a
change columns 8 end numbers symbols.print
print newly modified line.
Comments
Post a Comment