Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 06-16-2010, 05:16 AM   #1
Registered: Oct 2005
Posts: 324

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:

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

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

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:


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

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

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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 05:29 AM.
Old 06-16-2010, 05:31 AM   #3
Registered: Apr 2009
Distribution: slackware 14.1 64-bit, slackware 14.2 64-bit, SystemRescueCD
Posts: 468

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

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

Rep: Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943
You da man colucix

(beat me too it)
Old 06-16-2010, 10:40 PM   #6
Registered: Oct 2005
Posts: 324

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


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 01: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 02:50 PM
How to exclude repetitions of words in the shell output? qnxqnx Linux - Newbie 11 07-30-2008 02:08 PM
script to exclude strings... NiallC Programming 10 03-28-2005 07:46 PM
Script that lists #of permissions nabil Linux - General 6 04-23-2002 04:06 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:17 AM.

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