Home > Mobile >  How do I keep lines starting with a list of numbers (linux)?
How do I keep lines starting with a list of numbers (linux)?

Time:03-12

I have a file that looks like this:

0   123   word
1   324   word
2   234   word
3   53    word
4   666   word
5   23    word
6   4     word
...

I have been trying to keep only the lines starting with some numbers. I have the numbers in a file that looks like this:

2
4
5
...

So the final file should look like this:

2   234   word
4   666   word
5   23    word
...

I think I can get lines starting with a specific character using grep (grep "2" input > output), but doing it one by one is not viable and I do not know how to do it for a list of numbers in a file. Could someone help me with that?

CodePudding user response:

With GNU awk:

awk 'NR == FNR {a[$1]; next} $1 in a {print}' file1 file2

where file1 contains the numbers to keep, one per line, and file2 is the file to filter.

  • Related