用bash和awk解析限制

时间:2014-09-08 22:42:20

标签: regex bash perl awk sed

我有像

那样的束缚
G6N-D5C-?: (116.663, 177.052, 29.149) K87CD/E85CB/E94CB/H32CB/Q21CB
L12N-T11C-?: (128.977, 175.109, 174.412) K158C/H60C/A152C/N127C/Y159C(notH60C)
K14N-E13C-?: (117.377, 176.474, 29.823) E187CB/V78CB
A75N-Q74C-?: (123.129, 177.253, 23.513) V131CG1/V135CG1/V78CG1

我需要用输出转换它们:

assign (resid 5 and name C ) (resid 87 and name CD or resid 85 and name CB or resid 94 and name CB or resid 32 and name CB or resid 21 and name CB ) 3.5 2.5 8.5 ! G6N-D5C-?: (116.663, 177.052, 29.149) K87CD/E85CB/E94CB/H32CB/Q21CB
assign (resid 11 and name C ) (resid 158 and name C or resid 60 and name C or resid 152 and name C or resid 127 and name C or resid 159 and name C ) 3.5 2.5 8.5 ! L12N-T11C-?: (128.977, 175.109, 174.412) K158C/H60C/A152C/N127C/Y159C(notH60C)
assign (resid 13 and name C ) (resid 187 and name CB or resid 78 and name CB ) 3.5 2.5 8.5 ! K14N-E13C-?: (117.377, 176.474, 29.823) E187CB/V78CB
assign (resid 74 and name C ) (resid 131 and name CG1 or resid 135 and name CG2 or resid 78 and name CG1 ) 3.5 2.5 8.5 ! A75N-Q74C-?: (123.129, 177.253, 23.513) V131CG1/V135CG1/V78CG1

我尝试过awk,但我无法弄清楚如何分解数组。请帮我转换一下,这是手工做的残酷。

2 个答案:

答案 0 :(得分:1)

您希望将-上的第一个单词拆开并检查第二个元素 然后拆分/上的最后一个单词并检查每个元素。

假设GNU awk,请仔细阅读http://www.gnu.org/software/gawk/manual/html_node/String-Functions.html#String-Functions

中的split()match()

感觉慷慨:

gawk '
  function extract(str, fmt,      m) {
    if (match(str, /^.([0-9]+)(.+)/, m)) printf fmt, m[1], m[2]
  }
  {
    split($1, a, /-/)
    extract(a[2], "assign (resid %d and name %s ) (")
    n = split($NF, a, /\//)
    sep = ""
    for (i=1; i<=n; i++) {
      extract(a[i], sep "resid %d and name %s ")
      sep = "or "
    }
    print ") 3.5 2.5 8.5 !", $0
  }
'

答案 1 :(得分:1)

以下是使用perl的一种方式:

#!/usr/bin/perl 

use strict;
use warnings;
use autodie;

open my $fh, '<', 'restraints.file';

while (<$fh>) {
    my @values = map { /.(\d+)(\w+)/; $1, $2 } split '/', (split)[-1];
    my ( $resid, $name ) = /^[^-]+-.(\d+)(\w+)-/;
    print "assign (resid $resid and name $name ) (";
    print join ( " or ", 
        map  { "resid $values[$_] and name $values[$_ + 1]" } 
        grep { not $_ % 2 } 0 .. $#values 
    );
    print " ) 3.5 2.5 8.5 ! $_";
}

输出:

assign (resid 5 and name C ) (resid 87 and name CD or resid 85 and name CB or resid 94 and name CB or resid 32 and name CB or resid 21 and name CB ) 3.5 2.5 8.5 ! G6N-D5C-?: (116.663, 177.052, 29.149) K87CD/E85CB/E94CB/H32CB/Q21CB
assign (resid 11 and name C ) (resid 158 and name C or resid 60 and name C or resid 152 and name C or resid 127 and name C or resid 159 and name C ) 3.5 2.5 8.5 ! L12N-T11C-?: (128.977, 175.109, 174.412) K158C/H60C/A152C/N127C/Y159C(notH60C)
assign (resid 13 and name C ) (resid 187 and name CB or resid 78 and name CB ) 3.5 2.5 8.5 ! K14N-E13C-?: (117.377, 176.474, 29.823) E187CB/V78CB
assign (resid 74 and name C ) (resid 131 and name CG or resid 135 and name CG or resid 78 and name CG ) 3.5 2.5 8.5 ! A75N-Q74C-?: (123.129, 177.253, 23.513) V131CG1/V135CG1/V78CG1