Delete Multiple Line using linux command

Posted on 2014-01-07
Last Modified: 2014-11-18

I have two .txt files which content email list ,
File one named : old.txt

That contains :

file two named : delete.txt

What I need to ask if how can I delete the lines for old.txt using delete.txt pattern?
Question by:skywill
LVL 45

Expert Comment

ID: 39763052
Hi skywill,

You can use any of at least a half dozen utility programs (awk, sed, grep, etc.) to do this.  Join works well, too.

Try this:

 join -v1 -v2 < (sort old.txt) < (sort delete.txt)

Good Luck!

Author Comment

ID: 39763091
what I need is when I run the command , the old.txt will have only left these email :

so the the result is and deleted from old.txt

do ur command work ?
LVL 45

Assisted Solution

Kdo earned 125 total points
ID: 39763106
Those commands will generate the results that you're looking for and write them to standard output.

Just redirect the output to the desired file.  You'll probably want to write the results to a temporary file, then copy that file back over old.txt

 join -v1 -v2 < (sort old.txt) < (sort delete.txt) > new.txt
 cp new.txt old.txt
LVL 37

Accepted Solution

Gerwin Jansen earned 125 total points
ID: 39763137
grep -v -f delete.txt old.txt

(will use patterns from old.txt to find non-matching lines from delete.txt)

grep -v -f delete.txt old.txt  > new.txt

(new.txt will contain your filtered output)

grep -v -f delete.txt old.txt  > new.txt ; mv new.txt old.txt

(and rename new.txt to old.txt)
LVL 26

Expert Comment

ID: 39835407
the proper use of join is :

$ sort old.txt > old.txt.sorted
$ sort delete.txt > delete.txt.sorted
$ join -a1 -v2 old.txt.sorted delete.txt.sorted


this is much simpler and does not change the order of the addresses nor spend time sorting them

$ grep -v -f delete.txt old.txt

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now