TransWikia.com

Filter rows of a file by list of a second file

Unix & Linux Asked by sh yang on January 18, 2021

file1.tabular contains information of ‘elements’ over 3 columns.

element1 235 oval red 
element2 334 oval red 
element3 356 rectangular blue

file2.txt contains a single column

element1
element3

I want to filter file1.tabular and save them as file3.tabular, which will contain only the elements in file2.txt, but with all 3 column information in file1.tabular, such as

element1 235 oval red
element3 356 rectangular blue

Would there be an command to solve this on Ubuntu?
I would appreciate an expert help.
Thanks

One Answer

You may be looking for

grep -f file2.txt file1.tabular > file3.tabular

The -f option instructs grep to read the patterns to look for from file2.txt. It applies the search to file1.tabular and redirects output to file3.tabular.

Note that I assume there are not really "line continuation" escapes ( the at the end of all lines) in your files.

Correct answer by AdminBee on January 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP