Print line with match and line above that matches another pattern

佐手、 提交于 2020-02-06 08:01:07

问题


I have a file with certain IDs.

ID.txt
aaa
bbb
ccc

I have a another file like this File.txt

Query: ABC1
aaa
abc
bbb
ccc
Query: CAB1
bbb
ccc
abc
Query: CBB1
ass
aaa
bbc

**Expected output:**
Query: ABC1
aaa
bbb
ccc
Query: CAB1
bbb
ccc
Query: CBB1
aaa

Real example:

**IDs**
    LYSC_CHICK
    LACB_BOVIN
    B5B0D4_BOVIN
    DEF1_ARAHY
    DEF2_ARAHY
    DEF3_ARAHY
    TRFL_BOVIN
    Q0PKR4_ARAHY
    Q0GM57_ARAHY
    Q647G5_ARAHY
    Q6JYQ7_HEVBR
    AMP2_FAGES

**File**
    Query: PROKKA_00022 hypothetical protein - 36 aa
    Hit: AMP1_FAGES UniProt Fag e 4 UniProt P0DKH7 http://www.u
     100.0% identity
    Hit: AMP2_FAGES UniProt Fag e 4 UniProt P0DKH8 http://www.u
     100.0% identity
    Hit: O49860_HEVBR UniProt Hev b 6 UniProt O49860 http://www
     100.0% identity
    Hit: Q6JYQ7_HEVBR UniProt Hev b 6 UniProt Q6JYQ7 http://www
     100.0% identity
    Hit: HEVE_HEVBR UniProt Hev b 6 UniProt P02877 http://www.u
    Query: PROKKA_00572 hypothetical protein - 36 aa
    Hit: AMP1_FAGES UniProt Fag e 4 UniProt P0DKH7 http://www.u
     100.0% identity
    Hit: AMP2_FAGES UniProt Fag e 4 UniProt P0DKH8 http://www.u
     100.0% identity
    Hit: O49860_HEVBR UniProt Hev b 6 UniProt O49860 http://www
     100.0% identity
    Hit: Q6JYQ7_HEVBR UniProt Hev b 6 UniProt Q6JYQ7 http://www
     100.0% identity
    Query: PROKKA_01572 hypothetical protein - 36 aa
    Hit: AMP1_FHYES UniProt Fag e 4 UniProt P0DKH7 http://www.u
     100.0% identity
    Hit: AMX5_FAGES UniProt Fag e 4 UniProt P0DKH8 http://www.u
     100.0% identity
    Hit: O87860_HLLBR UniProt Hev b 6 UniProt O49860 http://www
     100.0% identity
    Hit: JHYYQ7_HEVBR UniProt Hev b 6 UniProt Q6JYQ7 http://www
     100.0% identity

**Expected output:**

    Query: PROKKA_00022 hypothetical protein - 36 aa
    Hit: Q6JYQ7_HEVBR UniProt Hev b 6 UniProt Q6JYQ7 http://www
    Hit: AMP2_FAGES UniProt Fag e 4 UniProt P0DKH8 http://www.u
    Query: PROKKA_00572 hypothetical protein - 36 aa
    Hit: Q6JYQ7_HEVBR UniProt Hev b 6 UniProt Q6JYQ7 http://www
    Hit: AMP2_FAGES UniProt Fag e 4 UniProt P0DKH8 http://www.u

Do i need to do this in a loop? I've tried something like this, but not having much luck:

for i in `cat ID.txt`
do 
   awk '/Query/{bar=$2} /"$i"/{print bar}' File.txt > output.txt
done

(Original post updated to reflect expected real output). Thanks a lot for the help. Updated on 02-01-2020 to include additional details for the IDs and Files and Expected output files)


回答1:


Could you please try following.

awk '
FNR==NR{
  a[$1]
  next
}
/^Query/ || $0 in a
' id.txt file.txt

Output is as follows.

Query: ABC1
aaa
bbb
ccc
Query: CAB1
bbb
ccc
Query: CBB1
aaa



回答2:


For those interested @RavinderSingh13 had the correct answer, with a minor modification as stated by @EdMorton for the real example:

awk '
FNR==NR{
  a[$1]
  next
}
/^Query/ || $2 in a
' IDs File

The output on the other had had a list of "Query" lines that did not have matching IDs as required. So, I used the following to get rid of the additional lines:

 awk '
    FNR==NR{
      a[$1]
      next
    }
    /^Query/ || $2 in a
    ' IDs File | grep -B 1 'Hit: ' | grep -v '^--' > output_file


来源:https://stackoverflow.com/questions/59986630/print-line-with-match-and-line-above-that-matches-another-pattern

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!