?
Solved

Delete Multiple Line using linux command

Posted on 2014-01-07
7
Medium Priority
?
200 Views
Last Modified: 2014-11-18
Hi,

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

That contains :
isbert@yahoo.com
laysod@gmail.com
gjebos@hotmail.com
sje2@admin2.com
mebes.dk@gmail.com
dojensu@ymail.com
ajs2u@hotmail.com

file two named : delete.txt
sje2@admin2.com
ajs2u@hotmail.com

What I need to ask if how can I delete the lines for old.txt using delete.txt pattern?
0
Comment
Question by:skywill
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
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!
Kent
0
 

Author Comment

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

isbert@yahoo.com
laysod@gmail.com
gjebos@hotmail.com
mebes.dk@gmail.com
dojensu@ymail.com

so the the result is sje2@admin2.com and ajs2u@hotmail.com deleted from old.txt

do ur command work ?
0
 
LVL 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 500 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
0
 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 500 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)
0
 
LVL 27

Expert Comment

by:skullnobrains
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
dojensu@ymail.com
gjebos@hotmail.com
isbert@yahoo.com
laysod@gmail.com
mebes.dk@gmail.com

---

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
isbert@yahoo.com
laysod@gmail.com
gjebos@hotmail.com
mebes.dk@gmail.com
dojensu@ymail.com
0

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

649 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