prompt to delete each record when pattern is found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting prompt to delete each record when pattern is found
# 1  
Old 11-26-2010
Data prompt to delete each record when pattern is found

Hello!. I am working on a very simple program and I have been trying different things. This is so far what I have done and there is one small detail that still does not work. It finds all the records in a phonebook per say:
Code:
./rem Susan
More than one match; Please select the one to remove: 
Susan1 banana
Susan2 apple   remove (y/n)?
n

The problem is that it only prompts to remove the last record and skips the first record. BUT if I type "y" then it will remove both records from the phonebook file, if I type "n" then it removes none. Here is my program so far.
Code:
if [ "$#" -ne 1 ]
then
    echo "Incorrect number of arguments."
    echo "Usage: rem name"
    exit 1
fi

name=$1

#
# Find number of matching entries
#

matches=$(grep "$name" phonebook | wc -l)
names=$(grep "$name" phonebook | awk '{print $1" " $2}')
echo "$names" 
#p="$1"

#
# If more than one match, issue message, else remove it
#

if [ "$matches" -gt 1 ]
then
    
    echo "More than one match; Please select the one to remove: "
    for i in "$names"
     do
        echo "$i   remove (y/n)?"
        read ans
        if [ $ans != n ]
            then
            grep -v "$name" phonebook > /tmp/phonebook
            mv /tmp/phonebook phonebook
        fi

    done
    
elif [ "$matches" -eq 1 ]
then
    grep -v "$name" phonebook > /tmp/phonebook
    mv /tmp/phonebook phonebook
else
    echo "I couldn't find $name in the phone book"
fi

Does anybody would be so kind to tell me what is wrong with my program? I appreciate any help. Thank you, thank you.

Last edited by Scott; 11-27-2010 at 05:37 AM.. Reason: Code tags
# 2  
Old 11-27-2010
I would have a look here first:
Code:
if [ $ans != n ]

n is unkonwn and it is not a variable
$ans is not being used later on

--
On a different note, I would change this:
Code:
grep -v "$name" phonebook > /tmp/phonebook
mv /tmp/phonebook phonebook

to this:
Code:
grep -v "$name" phonebook > /tmp/phonebook && mv /tmp/phonebook phonebook

In other words, only overwrite the original file if the operation was successful.

--
also, in future please use the code button and use proper indenting so your code becomes easier to read.

Last edited by Scrutinizer; 11-27-2010 at 03:50 AM..
# 3  
Old 11-27-2010
Thank you very much for your help Mr. Scrutinizer. Your second comment was very helpful, I made the change and that works perfect. I would like to explain my idea behind the line that you mentioned.
if [ $ans != n ] the variable $ans is the answer from the user (y/n) it suppose to enter the loop and prompt for every pattern found. Which means that IF the user types 'y' THEN it will proceed to remove the record #1, BUT, IF $ans = n THEN it will go to the line prinf "Record will not be removed" BUT still the program will go to the second pattern found IF any, and prompt me to remove the record going through the same process. I still cannot make it do that. Please that is the only detail. Thanks.

---------- Post updated at 05:59 PM ---------- Previous update was at 04:59 PM ----------

Here I wanted to put it this way for simplicity.

rnames=$(grep Susan phonebook | awk '{print $1" " $2}')

for i in "$rnames"; do echo "$i remove? (y/n)"; done
Susan1 banana
Susan2 apple remove? (y/n)


Stills puts the remove? question only at the end. What do i need to do to prompt for the first record?

Last edited by bartsimpsong; 11-27-2010 at 09:06 PM..
# 4  
Old 11-28-2010
The variable i is being specified as the variable in the for loop, but what is used inside the loop in the grep -v statement the variable name .

Further, you need to use for i in $names without the double quotes (otherwise it will loop only once for the whole string). But you would need to change the IFS variable in this case, since you are using multiple words for matching and you want to only use the newline character as the field separator.

Another option would be to use:
Code:
echo "$names" | 
while read i
do
  ....
done

I would use:
Code:
if [ "$ans" = "y" ]; then

so that only the letter y will lead to the deletion and not any character but n.

To allow case independence and multiple possible answers you could use this:
Code:
case $ans in
  y|Y|yes|YES) grep -v "$i" phonebook > /tmp/phonebook && mv /tmp/phonebook phonebook
esac

One more difficulty is that you are using:
names=$(grep Susan phonebook | awk '{print $1" " $2}')
So that there is only a single space between $1 and $2, even if there were for instance two spaces in the input file. If you then later would like to delete the line based on these two variables, grep will no longer find them

For example:
Code:
$ cat phonebook
Susan1 banana
Susan2    apple
$ grep "$name" phonebook | awk '{print $1" " $2}'
Susan1 banana
Susan2 apple
$ names=$(grep "$name" phonebook | awk '{print $1" " $2}')
$ echo "$names" | while read i; do echo "$i"; done
Susan1 banana
Susan2 apple
$ grep -v "Susan2 apple" phonebook
Susan1 banana
Susan2    apple


Last edited by Scrutinizer; 11-28-2010 at 02:40 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete only if duplicates found in each record

Hi, i have another problem. I have been trying to solve it by myself but failed. inputfile ;; ID T08578 NAME T08578 SBASE 30696 EBASE 32083 TYPE P func just test func chronology func cholesterol func null INT 30765-37333 INT 37154-37318 Link 5546 Link 8142 (4 Replies)
Discussion started by: redse171
4 Replies

2. Shell Programming and Scripting

If first pattern is found, look for second pattern. If second pattern not found, delete line

I had a spot of trouble coming up with a title, hopefully you'll understand once you read my problem... :) I have the output of an ldapsearch that looks like this: dn: cn=sam,ou=company,o=com uidNumber: 7174 gidNumber: 49563 homeDirectory: /home/sam loginshell: /bin/bash uid: sam... (2 Replies)
Discussion started by: samgoober
2 Replies

3. Shell Programming and Scripting

Delete a pattern present in file 2 from file 1 if found in file 1.

I have two files File1 ==== 1|2000-00-00|2010-02-02|| 2| 00:00:00|2012-02-24|| 3|2000-00-00|2011-02-02|| File2 ==== 2000-00-00 00:00:00 I want the delete the patterns which are found in file 2 from file 1, Expected output: File1 ==== (5 Replies)
Discussion started by: machomaddy
5 Replies

4. Shell Programming and Scripting

Reject the record if the record in the next line does not satisfy the pattern

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten The output should be 1one 2two 3three 1four 2five 3six (2 Replies)
Discussion started by: supchand
2 Replies

5. Shell Programming and Scripting

Delete line if pattern not found

I thought that this was going to be quit simple using sed but i wasn't able to find a way to delete the second line of a text file if my pattern was not found in the line With awk i am completly useless :rolleyes: Any ideas? (2 Replies)
Discussion started by: jepeto
2 Replies

6. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

7. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies

8. Shell Programming and Scripting

Finding Last occurance of another pattern when a pattern is found.

Hi, I have two files viz, rak1: $ cat rak1 rak2: $ cat rak2 sdiff rak1 rak2 returns: I want the lines that got modified, changed, or deleted preceding with the section they are in. I have done this so far: (1 Reply)
Discussion started by: rakeshou
1 Replies

9. Shell Programming and Scripting

How can I parse a record found in /etc/passwd into variables?

I am working with the Oracle 10.2.0.3 job scheduler on Solaris 10, and unfortunately, the scheduler executes scripts in such a way that several default shell environment variables are not defined. For example, $HOME, $USER, and $LOGNAME are missing. How can I parse the appropriate record in... (7 Replies)
Discussion started by: shew01
7 Replies

10. Shell Programming and Scripting

Delete a block of text delimited by blank lines when pattern is found

I have a file which contains blocks of text - each block is a multi-lines text delimited by blank lines eg. <blank line> several lines of text ... pattern found on this line several more lines of text ... <blank line> How do you delete the block of text (including the blank lines) when... (17 Replies)
Discussion started by: gleu
17 Replies
Login or Register to Ask a Question