Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2005
    Posts
    18

    Unanswered: 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

  2. #2
    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

  3. #3
    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

  4. #4
    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> |
    +-----------------------+

  5. #5
    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> |
    +-----------------------+

  6. #6
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •