使用不同文件

时间:2018-02-07 04:18:13

标签: awk formatting

我有一个非常讨厌的问题:我有2个文件正在使用,我需要替换原始文件的列,同时保持其格式不变。格式如下:

  

HETATM 1 CA LIG 1 38.925 -1.038 -22.754 1.00 0.00 Ca
  HETATM 2 CA LIG 1 38.509 -0.159 -22.703 1.00 0.00 Ca
  HETATM 3 CA LIG 1 37.873 -0.673 -22.309 1.00 0.00 Ca
  HETATM 4 CA LIG 1 37.261 -1.434 -22.569 1.00 0.00 Ca
  HETATM 5 CA LIG 1 37.800 -2.277 -22.363 1.00 0.00 Ca
  HETATM 6 CA LIG 1 37.764 -1.612 -21.664 1.00 0.00 Ca
  HETATM 7 CA LIG 1 36.833 -1.557 -21.515 1.00 0.00 Ca
  HETATM 8 CA LIG 1 35.941 -1.643 -21.936 1.00 0.00 Ca
  HETATM 9 CA LIG 1 35.959 -2.319 -22.634 1.00 0.00 Ca
  HETATM 10 CA LIG 1 36.670 -2.501 -21.971 1.00 0.00 Ca

这是应该的样子,但是,在任何操作下,这种格式都会丢失。

This is what the format should look like actually, instead of just a block of text...

所以,现在我有一个.xyz格式的不同文件,我需要用.xyz文件中的$ 6列替换上述文件中的第9列(1.00)。

  

10
CA 38.6675 -1.72133 -22.1513 6.90816 3.5 CA 38.925 -1.03771 -22.7538 6.87802 4 CA 38.5086 -0.158653 -22.7027 6.12023 3.5 CA 37.8732 -0.67308 -22.309 5.93085 3.75 CA 37.2605 -1.43401 -22.569 6.07069 3.25 CA 37.8002 -2.27692 -22.3632 6.39441 2.25 CA 37.7642 -1.61234 -21.6638 5.44254 3 CA 36.8325 -1.55711 -21.5145 5.45608 3.75 CA 35.9408 -1.64281 -21.936 5.20951 4 CA 35.9593 -2.31915 -22.6336 5.91997 4.5

我使用以下命令: awk 'NR==FNR{c11[NR]=$6; next} {$9=c11[FNR]}1' data.xyz data.pdb > out.pdb

我确实从技术上得到了我需要的东西,但是文件没有被识别为.pdb,并且间距丢失了:

this is the output after AWK...

这非常令人困惑,我不知道如何继续。非常感谢我们提前提供的帮助!

2 个答案:

答案 0 :(得分:0)

答案专门适合您的文件

我认为printf更适合打印你的线条:

阅读 awk的printf手册

我已使用awk' 1格式更改了awk' printf

 awk 'NR==FNR{c11[NR]=$6; next} {$9=c11[FNR]} {printf "%s %2s %s %s %s %s %s %s %4s %s %s\n", $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11}' file2 file1

答案 1 :(得分:0)

您的问题非常模糊(输入/输出格式不正确,输出不匹配输入,没有预期输出,没有声明您是否要保留空格或填充新值以保持对齐,没有指示是否空格是空格或制表符或两者等等)但也许这就是你想要的,使用GNU awk for gensub():

$ cat file1
a      b         c     d

$ cat file2
X Y

$ awk 'NR==FNR{a[NR]=$2;next} {$0=gensub(/((\S+\s+){1})\S+/,"\\1"a[FNR],1)} 1' file2 file1
a      Y         c     d

$ awk 'NR==FNR{a[NR]=$2;next} {$0=gensub(/((\S+\s+){2})\S+/,"\\1"a[FNR],1)} 1' file2 file1
a      b         Y     d

$ awk 'NR==FNR{a[NR]=$2;next} {$0=gensub(/((\S+\s+){3})\S+/,"\\1"a[FNR],1)} 1' file2 file1
a      b         c     Y