If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Shell Script required for deleting lines from text file

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-19-05, 13:40
sbk sbk is offline
Registered User
 
Join Date: Jan 2005
Posts: 18
Shell Script required for deleting lines from text file

Hi,

I need some help here. Any help is very much appreciated.

I have a text file and have some email addresses in it, one on each line.

I need to write a script which takes the email address as argument......opens the file ( say emails.txt ).....and deletes the email address given as argument.

Conditions are:
1. Check for availability of the file
2. Check for validitiy of email format i.e the email address in the first argument should be in the form 'lastname.firstname@company.com'


Thanks a million,
sbk
Reply With Quote
  #2 (permalink)  
Old 01-20-05, 00:20
srsjc srsjc is offline
Registered User
 
Join Date: Jun 2004
Posts: 20
  • check for the existense of file
    Do validation for the command line argument(in thsi case the email id to be deleted)
    grep -v $mailid email.txt >temp.txt
    mv temp.txt email.txt
Reply With Quote
  #3 (permalink)  
Old 01-20-05, 15:10
sbk sbk is offline
Registered User
 
Join Date: Jan 2005
Posts: 18
Question Thank You, but new problem

Thanks srsjc,

I tested that script & made a slight modification because, when we try to move the file from temp.txt to email.txt, it says the file exists, so I did it this way:
_________________________________
#!/bin/sh

email=$1
mv email.txt email2.txt
egrep -v $email email2.txt > email.txt
rm email2.txt
_________________________________
Now I have a new problem, instead of taking the email address as argument for calling the script, I need to remove the email addresses from this email.txt by taking one email address at a time by reading another file called remove.txt.

The remove.txt file consists of email addresses (one on each line) that need to be removed from email.txt. That is, we are subtracting remove.txt from email.txt.

I understand that the logic for doing this is we need to read one line from remove.txt and then remove it from email.txt and do it until we read all email addresses or lines present in remove.txt. I know how to do it in C or C++ or Java, but not in Shell scripting.

Appreciate a lot if someone could respond to this email.

Thanks in advance,
sbk
Reply With Quote
  #4 (permalink)  
Old 01-20-05, 15:21
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
[not tested]
Code:
#!/bin/ksh

emailFile='email.txt'
rmFile='remove.txt'
email=$1

while read rem
do
   /bin/echo "/${rem}/d"'\nwq' | ex -s "${emailFile}"
done < "${rmFile}";
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #5 (permalink)  
Old 01-20-05, 15:26
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
you could try something like that:
egrep -v -f remove.txt email.txt
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #6 (permalink)  
Old 01-20-05, 18:55
sbk sbk is offline
Registered User
 
Join Date: Jan 2005
Posts: 18
Unhappy Thanks...but syntax error

Thanks vgersh99,

I tested the first code that you sent, but it says "no lines in buffer". So, I tried the second solution which I think is very much understandable, atleast for me , but even it exits saying "syntax error". I don't know what I'm missing here. I checked the man pages also but have no clue on how to fix this problem.

Thanks,
sbk
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On