Warm tip: This article is reproduced from stackoverflow.com, please click
awk grep shell

Print line with match and line above that matches another pattern

发布于 2020-04-09 23:01:05

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)

Questioner
Susheel Busi
Viewed
43
RavinderSingh13 2020-01-30 23:15

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