简体   繁体   中英

Match words from 2 files

I have 2 txt file's (words.txt and fail.txt)

words.txt

level4:the
level4:of
level4:and
level4:to
level4:a
level4:in
level4:for
level4:is
level4:on
level4:that
level4:by
level4:this
level4:with
level4:i
level4:you

fail.txt

the
of
and
to
a
in

How can I obtain a list that will contain all the list from words.txt except those from fail.txt? EXAMPLE of what i need to obtain as a result

level4:for
level4:is
level4:on
level4:that
level4:by
level4:this
level4:with
level4:i
level4:you

Thank you.

Using awk:

awk -F: 'FNR==NR{fail[$0]++; next} !($2 in fail)' fail.txt words.txt
level4:for
level4:is
level4:on
level4:that
level4:by
level4:this
level4:with
level4:i
level4:you

Description:

-F :                         # use : as delimiter
NR == FNR {                  # While processing the fail file
  fail[$0]++                 # store the record in an array fail
  next                       # move to next record
}
{                            # while processing the words file
  !($2 in fail)              # print if 2nd field is not in fail array
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM