LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-16-2010, 04:16 AM   #1
packets
Member
 
Registered: Oct 2005
Posts: 339

Rep: Reputation: 32
script to exclude words from a lists


I'm trying to figure out how to delete string inside the file addresses.txt. The string I want to delete on addresses.txt was on removed.txt. I have a file addresses.txt and it includes the ff format:

test@foo.com
true@fee.com
fee@tik.com
ew@kit.com
test@foo.com
true@fee.com
fee@tik.com
ew@kit.com

Now, I have a file removed.txt. removed.txt contains a word

test@foo.com
ew@kit.com

How can I remove it on addresses.txt. If I have one entry on removed.txt, I do it this way

Quote:
address=/root/scripts/addresses.txt
remove=`cat /root/scripts/removed.txt`

cat $address | grep -v $remove
However, if there is more than 1 entries on removed.txt, it will now encounter an error using grep.

I believe I can do this using sed. Just don't know the exact parameters.

I do try the ff but it seems do not work:

Quote:
#!/bin/bash

address=/root/scripts/addresses.txt
remove=`cat /root/scripts/removed.txt`

for REMOVE in remove;do
sed -i "/$REMOVE/d" $address
done
Any advise?
 
Old 06-16-2010, 04:28 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Code:
while read REMOVE; do
     sed -i "/$REMOVE/d" $address
done < /root/scripts/removed.txt
This reads the "removed" file on line at a time.

Please use [CODE] tags whenever posting code samples. (Advanced mode---# button)

Last edited by pixellany; 06-16-2010 at 04:29 AM.
 
Old 06-16-2010, 04:31 AM   #3
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware 14.1 64-bit, slackware 14.2 64-bit, SystemRescueCD
Posts: 533

Rep: Reputation: 129Reputation: 129
1. backup your files
2. try this
Code:
for l in $(cat removed.txt)
do
	cmd="sed -i '/$l/d' addresses.txt"
	eval "$cmd"
done
This reads removed.txt line by line and removes that
line from addresses.txt
 
Old 06-16-2010, 04:35 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Or simply:
Code:
grep -vf removed.txt addresses.txt
 
1 members found this post helpful.
Old 06-16-2010, 04:59 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
You da man colucix

(beat me too it)
 
Old 06-16-2010, 09:40 PM   #6
packets
Member
 
Registered: Oct 2005
Posts: 339

Original Poster
Rep: Reputation: 32
Quote:
grep -vf removed.txt addresses.txt
Nice. Thanks for this.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
CVS Exclude : Exclude sub directories from check out On Linux from command line shajay12 Linux - Newbie 1 08-03-2009 12:36 AM
LXer: Unique Sorting Of Lists And Lists Of Lists With Perl For Linux Or Unix LXer Syndicated Linux News 0 09-05-2008 01:50 PM
How to exclude repetitions of words in the shell output? qnxqnx Linux - Newbie 11 07-30-2008 01:08 PM
script to exclude strings... NiallC Programming 10 03-28-2005 06:46 PM
Script that lists #of permissions nabil Linux - General 6 04-23-2002 03:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration