View previous topic :: View next topic |
Author |
Message |
ggelln Tux's lil' helper
Joined: 24 Jan 2003 Posts: 98 Location: Montreal Quebec
|
Posted: Wed Feb 26, 2003 3:45 pm Post subject: Simple bash programming question |
|
|
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 |
|
|
dannym n00b
Joined: 01 Nov 2002 Posts: 13 Location: Austria
|
Posted: Wed Feb 26, 2003 4:07 pm Post subject: |
|
|
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 |
|
|
ggelln Tux's lil' helper
Joined: 24 Jan 2003 Posts: 98 Location: Montreal Quebec
|
Posted: Wed Feb 26, 2003 4:54 pm Post subject: |
|
|
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 |
|
|
cram Guru
Joined: 17 Nov 2002 Posts: 312 Location: Saskatoon, Canada
|
Posted: Wed Feb 26, 2003 9:23 pm Post subject: |
|
|
Quote: | but what does the last $ do? |
Opposite of the ^, it matches the end of a line _________________ aaarggghhhh.
Good point Chewie. |
|
Back to top |
|
|
|