8

I have file 1 which have those lines:

ATM 1434.972183
BMPR2 10762.78192
BMPR2 10762.78192
BMPR2 1469.14535
BMPR2 1469.14535
BMPR2 1738.479639
BMS1 4907.841667
BMS1 4907.841667
BMS1 880.4532628
BMS1 880.4532628
BMS1P17 1249.75
BMS1P17 1249.75
BMS1P17 1606.821429
BMS1P17 1606.821429
BMS1P17 1666.333333
BMS1P17 1666.333333
BMS1P17 2108.460317
BMS1P17 2108

And file 2 have a list of words:

ATM
BMS1

So, the output will be like this:

ATM 1434.972183
BMS1 4907.841667
BMS1 4907.841667
BMS1 880.4532628
BMS1 880.4532628

I know it's really a duplicate question, but I tried all types of grep and sed and awk, maybe it will works with you guys with this tiny example but I have a very huge file > 1M lines and all previous way doesn't help

it return part of the lines that containing those words although there are other words in file 2 that matches the lines from file 1

3
  • If the well-know duplicates don't work, then perhaps there is something non-standard about your files (such as DOS-style line endings or other hidden embedded characters)? Commented Jul 25, 2018 at 19:15
  • It's a big file, I just check quickly and it's ok, is there any command that could help me to check something like that ?
    – LamaMo
    Commented Jul 25, 2018 at 19:22
  • file yourfile may report something like ASCII text, with CRLF line terminators or you can inspect a portion of the file with cat -A yourfile Commented Jul 25, 2018 at 19:25

3 Answers 3

7
grep -Fw -f words myfile

This would extract the lines in myfile that contains the words in the file words anywhere.

The strings in words are treated as fixed strings (not regular expressions) due to the -F option, and the -w option ensures that we only get lines that contains the exact same word (no matches of substrings in words are allowed). A word is a consecutive sequence of characters from the set of alphanumerical characters and the underscore character.

The words in the file words most be listed on separate lines.

4
  • 1
    Unfortunately, I tried this before and it just return part of not all the lines that should be extracted :(
    – LamaMo
    Commented Jul 25, 2018 at 19:09
  • @SaraWasl Could you give an example (in the text of the question) of a line that is not returned when you do this, together with the string that that line should be matching? If you're getting any error messages, then include these in the question as well.
    – Kusalananda
    Commented Jul 25, 2018 at 19:12
  • 1
    In small example like in the question it's work, but here the actual big file it's return part. So, for example: it return me just the lines with ATM but not BMS1
    – LamaMo
    Commented Jul 25, 2018 at 19:17
  • @SaraWasl It's difficult to debug something like that when one doesn't have access to the data. The grep utility should have given you some form of error message if it failed, or the data may be corrupt or not quite what you think it is.
    – Kusalananda
    Commented Jul 25, 2018 at 20:03
2

Another of the classic options is with Awk:

$ awk 'NR==FNR {a[$1]++; next} $1 in a' words myfile
ATM 1434.972183
BMS1 4907.841667
BMS1 4907.841667
BMS1 880.4532628
BMS1 880.4532628

This one will be insensitive to any trailing whitespace in the words file entries.

0

Try join command:

join file1 file2

ATM 1434.972183
BMS1 4907.841667
BMS1 4907.841667
BMS1 880.4532628
BMS1 880.4532628

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .