[Solved] delete line from file1 by reading from file2

Hi All,

I have to arrange one of the text file by deleting specific lines.

 
cat file1.txt
 
3595 3595 -0.00842773 -0.0085077 0.00368851 
12815 12815 -0.00929239 0.00439785 0.0291697 
3747 3747 -0.00974353 0.00228922 0.0225058 
3574 3574 -0.00711399 -0.00315748 0.0141206 
....
12734 12734 -0.00918432 -0.0163555 -0.00586558
3617 3617 -0.00884411 -0.00911498 0.00567046
 
cat file2.txt 
 
12815
3747
...
12734

I want to delete the lines in file1.txt which first column has the number defined in file2.txt. So, output file will be.

 
cat output.txt
3595 3595 -0.00842773 -0.0085077 0.00368851 
3574 3574 -0.00711399 -0.00315748 0.0141206 
....
3617 3617 -0.00884411 -0.00911498 0.00567046

Thanks,

$ fgrep -vf file2.txt file1.txt
3595 3595 -0.00842773 -0.0085077 0.00368851
3574 3574 -0.00711399 -0.00315748 0.0141206
3617 3617 -0.00884411 -0.00911498 0.00567046

It did not work. This command deleted more lines than i was expecting. Can we check by two columns in file1.txt to be sure it deletes the correct lines? It has to be the exact match not the subset.

nawk 'FNR==NR {f2[$1];next} !($1 in f2)' file2 file1
1 Like
awk 'NR==FNR{a[$0]++;next}{if($1 in a){next}}1' file2.txt file1.txt > output.txt

--ahamed

1 Like

If it did not you expected then you have to think again "what was it saying in the forum rules on how to post?"
jayan_jay has been kind enough to show you a code THAT WORKS, I tested...
If it does not for you it is because of the lack of information you gave, I tested on a AIX 6.2 using ksh. Is the sample data enough to figure out all possible cases? What do we know of your OS and environment? Till now nothing...

These two commands work great.

---------- Post updated at 10:54 AM ---------- Previous update was at 10:52 AM ----------

Sorry for the misunderstanding jayan_jay.

I hope you see now why it is important to give the most information possible especially when extracting to sample data (from the little data sample you gave jayan's solution seems to work...), many of us aren't of English mother tongue and most of us peep along during work time and so use copy/paste to create test samples for figuring out how to help you, the more you are clear and give a good sampling the more chances you have to get the right answer...

Have a good weekend