Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Simple bash programming question
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
ggelln
Tux's lil' helper
Tux's lil' helper


Joined: 24 Jan 2003
Posts: 98
Location: Montreal Quebec

PostPosted: Wed Feb 26, 2003 3:45 pm    Post subject: Simple bash programming question Reply with quote

I am trying to make a shell script that will manage
a plethora of cocktail recipes I have.

I have a favorites file which contains a list of absolute pathnames to cocktail recipes that the user has chosen for a given day.

The user enters a name of the cocktail that they would like to remove and the program then searches (grep) each recipe in his list for a match. If a match is found I would like the pathname to be removed from the file.

I'm not sure how to do this as I need to erase a line only if the file it points to contains the given condition . . .

Any tips or comments (even a different way of thinking about the solution, though no perl please :-) ) would be greatly appreciated.

the code so far looks as follows (the sed line does not work)

Code:

source ~/cocktail/etc/COCKTAILENV

USER=$1

if [ -e $LFAVORITES/${USER}.${DATE} ]
then
    for r in `cat $LFAVORITES/${USER}.${DATE}`
    do
        if grep -q $2 $r
        then
            sed 's/$r//' $LFAVORITES/${USER}.${DATE}
        fi
    done
fi


the for loop is used so that the search is done on the actual recipes as not the name of the file.


Gabriel
Back to top
View user's profile Send private message
dannym
n00b
n00b


Joined: 01 Nov 2002
Posts: 13
Location: Austria

PostPosted: Wed Feb 26, 2003 4:07 pm    Post subject: Reply with quote

Hi!

Instead of
sed 's/$r//' $LFAVORITES/${USER}.${DATE}
use:

grep -vi "^${r}$" $LFAVORITES/${USER}.${DATE}.tmp && mv $LFAVORITES/${USER}.${DATE}.tmp $LFAVORITES/${USER}.${DATE}

If you want to remove the string from $r on a line on its own from the given file.
Back to top
View user's profile Send private message
ggelln
Tux's lil' helper
Tux's lil' helper


Joined: 24 Jan 2003
Posts: 98
Location: Montreal Quebec

PostPosted: Wed Feb 26, 2003 4:54 pm    Post subject: Reply with quote

Wow thanks for your quick response :-)

I used the code (modified slightly, 2nd argument to
grep had a .tmp expension which I don't think can work. I imagine it was just a typo, tell me if I'm wrong . . .)

I really like the using the inverse grep listing, I had always wondered what that would be usefull for, now I know!

If you have the inkling can you tell me what the
"^$r$" regex does exactly I know the ^ matches the beginning of the line, but what does the last $ do?

Thanks a lot.

Gabriel
Back to top
View user's profile Send private message
cram
Guru
Guru


Joined: 17 Nov 2002
Posts: 312
Location: Saskatoon, Canada

PostPosted: Wed Feb 26, 2003 9:23 pm    Post subject: Reply with quote

Quote:
but what does the last $ do?

Opposite of the ^, it matches the end of a line
_________________
aaarggghhhh.
Good point Chewie.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum