如何删除所有符合特定条件的行

如何删除所有符合特定条件的行

我有多个 pdb 文件,我想只保留以 开头的行^FORMUL,如果行后面C跟着一个大于 的数字,(C3,C4,C5,C6..100 etc)那么我就不应该打印它。第二个条件是每行中的 总数C, H and N至少应为 6。

因此,总体而言,删除后面跟着C数字大于且2>和为C+O+N6的行>=

FORMUL   3  HOH   *207(H2 O)    (print it)                                     
FORMUL   2  SF4    FE4 S4       (print it)                                                
FORMUL   3  NIC    C5 H7 N O7   (don't print, there is C5)                                               
FORMUL   4  HOH   *321(H2 O)    (print it)                                                
FORMUL   3  HEM    2(C34 H32 FE N4 O4)  (don't print, there is C34)

我曾经尝试过这样做,perl但是线路彼此差异很大,所以我不确定是否可以做到。

答案1

这是我的建议,不幸的是我现在没有时间写解释:

$ cat script.bash

#!/bin/bash

INPUT_FILE="${1}"
[[ -z "${2}" ]] && OUTPUT_FILE="output-file" || OUTPUT_FILE="${2}"

LINE_COUNTER=1

while read -a line; do

        C_VALUE="$(echo ${line[@]} | grep -Po 'C\d+' | sed 's/C//')"
        O_VALUE="$(echo ${line[@]} | grep -Po 'O\d+' | sed 's/O//')"
        H_VALUE="$(echo ${line[@]} | grep -Po 'H\d+' | sed 's/H//')"
        COH_SUM=$(( C_VALUE + O_VALUE + H_VALUE  ))
        printf '\nLINE=%s: C=%s, O=%s, H=%s, SUM=%s' "$LINE_COUNTER" "$C_VALUE" "$O_VALUE" "$H_VALUE" "$COH_SUM"

        if [[ "$COH_SUM" -ge "6" ]] || [[ "$C_VALUE" -gt "2" ]]; then
                LINES_TO_BE_REMOVED="${LINES_TO_BE_REMOVED};${LINE_COUNTER}d"
        fi

        ((LINE_COUNTER++))

done < "$INPUT_FILE"

printf '\n\nLines to be removed: %s\n\nResult:\n\n' "${LINES_TO_BE_REMOVED#';'}"


sed -e "${LINES_TO_BE_REMOVED#';'}" "$INPUT_FILE" | tee "$OUTPUT_FILE"

如何使用它:

$ cat input-file
FORMUL   3  HOH   *207(H2 O)
FORMUL   2  SF4    FE4 S4
FORMUL   3  NIC    C5 H7 N O7
FORMUL   4  HOH   *321(H2 O)
FORMUL   3  HEM    2(C34 H32 FE N4 O4)

$ ./script.bash input-file output-file

LINE=1: C=, O=, H=2, SUM=2
LINE=2: C=, O=, H=, SUM=0
...
Lines to be removed: 3d;5d

Result:
FORMUL   3  HOH   *207(H2 O)
...

$ cat output-file
FORMUL   3  HOH   *207(H2 O)
FORMUL   2  SF4    FE4 S4
FORMUL   4  HOH   *321(H2 O)

以下是非常简单的脚本,仅输出要保留的行:

#!/bin/bash

while read -a line; do
        C_VALUE="$(echo "${line[@]}" | grep -Po 'C\d+')"
        if [[ ! "${C_VALUE#C}" -gt '2' ]]; then echo "${line[@]}"
        else
                O_VALUE="$(echo "${line[@]}" | grep -Po 'O\d+')"
                H_VALUE="$(echo "${line[@]}" | grep -Po 'H\d+')"
                C_VALUE="${C_VALUE#C}"; O_VALUE="${O_VALUE#O}"; H_VALUE="${H_VALUE#H}"
                if (( (( C_VALUE + O_VALUE + H_VALUE )) <= 6 )); then echo "${line[@]}"; fi
        fi
done < "${@}"

如何使用它:

./script.bash input-file > output-file

答案2

虽然它不是以 shell 脚本的形式编写的,但是如果将来有人会使用它,这里就有如何在 Perl 中执行它的答案。

#!/usr/bin/perl


use strict;
use warnings;


#open ($file, '<', '5PCZ.pdb') or die $!; 

while (<>) { # read from STDIN a line at a time
  # Split data on whitespace, but only into three columns
  my @cols = split /\s+/, $_, 3;

  next unless $cols[0] eq 'FORMUL';

  # Now extract the letter stuff into a hash for easy access.
  my %letters = m/([A-Z])(\d+)/g;

  # Give the values we're interested in, a default of 0
  $letters{$_} //= 0 for (qw[C O N]);

  next if $letters{C} > 2
    and $letters{C} + $letters{O} + $letters{N} >= 6;

  # I think we can then print the line;
  print;
}

相关内容