我是sed和awk的初学者,但我可以完成基本任务。我的工作有问题,包括将文件中的某些行从一个地方重新排列到另一个地方。
在下面的示例中,我想捕获包含模式的行" O" (使用空格),并在包含模式的行之前移动它#34; N"这样:
ATOM 1771 C ALA A 61 48.797
ATOM 1772 O ALA A 61 49.998
ATOM 1773 CB ALA A 61 48.018
ATOM 1774 HB1 ALA A 61 48.964
ATOM 1775 HB2 ALA A 61 47.476
ATOM 1776 HB3 ALA A 61 47.420
ATOM 1777 N LYS A 62 47.854
ATOM 1778 H LYS A 62 46.979
ATOM 1779 CA LYS A 62 48.163
ATOM 1780 HA LYS A 62 49.236
ATOM 1781 C LYS A 62 47.633
ATOM 1782 O LYS A 62 47.789
ATOM 1783 CB LYS A 62 47.541
ATOM 1784 HB2 LYS A 62 47.693
ATOM 1785 HB3 LYS A 62 46.473
ATOM 1798 HZ3 LYS A 62 45.853
ATOM 1799 N GLY A 63 47.001
ATOM 1800 H GLY A 63 46.906
变为:
ATOM 1771 C ALA A 61 48.797
ATOM 1773 CB ALA A 61 48.018
ATOM 1774 HB1 ALA A 61 48.964
ATOM 1775 HB2 ALA A 61 47.476
ATOM 1776 HB3 ALA A 61 47.420
ATOM 1772 O ALA A 61 49.998
ATOM 1777 N LYS A 62 47.854
ATOM 1778 H LYS A 62 46.979
ATOM 1779 CA LYS A 62 48.163
ATOM 1780 HA LYS A 62 49.236
ATOM 1781 C LYS A 62 47.633
ATOM 1783 CB LYS A 62 47.541
ATOM 1784 HB2 LYS A 62 47.693
ATOM 1785 HB3 LYS A 62 46.473
ATOM 1798 HZ3 LYS A 62 45.853
ATOM 1782 O LYS A 62 47.789
ATOM 1799 N GLY A 63 47.001
ATOM 1800 H GLY A 63 46.906
有什么建议吗?非常感谢你的时间!
答案 0 :(得分:2)
这应该有效:
awk '$3 == "O" { T = $0; next; } $3 == "N" && T { print T; T = 0; } 1' your_file.txt
确切输出:
ATOM 1771 C ALA A 61 48.797
ATOM 1772 O ALA A 61 49.998
ATOM 1773 CB ALA A 61 48.018
ATOM 1774 HB1 ALA A 61 48.964
ATOM 1775 HB2 ALA A 61 47.476
ATOM 1776 HB3 ALA A 61 47.420
ATOM 1777 N LYS A 62 47.854
ATOM 1778 H LYS A 62 46.979
ATOM 1779 CA LYS A 62 48.163
ATOM 1780 HA LYS A 62 49.236
ATOM 1781 C LYS A 62 47.633
ATOM 1782 O LYS A 62 47.789
ATOM 1783 CB LYS A 62 47.541
ATOM 1784 HB2 LYS A 62 47.693
ATOM 1785 HB3 LYS A 62 46.473
ATOM 1798 HZ3 LYS A 62 45.853
ATOM 1799 N GLY A 63 47.001
ATOM 1800 H GLY A 63 46.906
答案 1 :(得分:2)
使用perl one-liner
perl -ane '$F[2] eq "O" ? $o = $_ : print $F[2] eq "N" ? ($o, $_) : $_' file
切换:
-a
:拆分空间线并将其加载到数组@F
-n
:为输入文件中的每一行创建一个while(<>){..}
循环。 -e
:告诉perl
在命令行上执行代码。 答案 2 :(得分:1)
这也有效,
$ awk -v RS="\0" -v FS='\n' '{for (i=1;i<=NF; i++) {if ($i~/\s*ATOM\s*[0-9]+\s*O.*/) {var=$i;} else if($i~/\s*ATOM\s*[0-9]+\s*N.*/) {print var"\n"$i} else print $i;}}' file
ATOM 1771 C ALA A 61 48.797
ATOM 1773 CB ALA A 61 48.018
ATOM 1774 HB1 ALA A 61 48.964
ATOM 1775 HB2 ALA A 61 47.476
ATOM 1776 HB3 ALA A 61 47.420
ATOM 1772 O ALA A 61 49.998
ATOM 1777 N LYS A 62 47.854
ATOM 1778 H LYS A 62 46.979
ATOM 1779 CA LYS A 62 48.163
ATOM 1780 HA LYS A 62 49.236
ATOM 1781 C LYS A 62 47.633
ATOM 1783 CB LYS A 62 47.541
ATOM 1784 HB2 LYS A 62 47.693
ATOM 1785 HB3 LYS A 62 46.473
ATOM 1798 HZ3 LYS A 62 45.853
ATOM 1782 O LYS A 62 47.789
ATOM 1799 N GLY A 63 47.001
ATOM 1800 H GLY A 63 46.906
答案 3 :(得分:1)
另一个awk
awk '$3=="O" {f=$0;next} $3=="N" {$0=f RS$0}1'
ATOM 1771 C ALA A 61 48.797
ATOM 1773 CB ALA A 61 48.018
ATOM 1774 HB1 ALA A 61 48.964
ATOM 1775 HB2 ALA A 61 47.476
ATOM 1776 HB3 ALA A 61 47.420
ATOM 1772 O ALA A 61 49.998
ATOM 1777 N LYS A 62 47.854
ATOM 1778 H LYS A 62 46.979
ATOM 1779 CA LYS A 62 48.163
ATOM 1780 HA LYS A 62 49.236
ATOM 1781 C LYS A 62 47.633
ATOM 1783 CB LYS A 62 47.541
ATOM 1784 HB2 LYS A 62 47.693
ATOM 1785 HB3 LYS A 62 46.473
ATOM 1798 HZ3 LYS A 62 45.853
ATOM 1782 O LYS A 62 47.789
ATOM 1799 N GLY A 63 47.001
ATOM 1800 H GLY A 63 46.906
答案 4 :(得分:1)
这可能适合你(GNU sed):
sed '/\sO\s/{h;d};/\sN\s/{H;x}' file
将O
行保存在保留空间中,然后将其删除,将N
行附加到该行并同时打印出来。