LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   trying to delete a line with sed (https://www.linuxquestions.org/questions/linux-general-1/trying-to-delete-a-line-with-sed-273423/)

deoren 01-03-2005 04:23 PM

trying to delete a line with sed [SOLVED]
 
Hi,

I'm working on a script to kill a current song being played by ogg123, and I have gotten as far as retrieving the location of the song.

It is stored in a text file (playlist) in this format:
Quote:

various_artists/wow_hits_2005_disc_2/never_alone__barlowgirl.ogg
That is also the format of the string that is stored in CURRENT_PLAYLIST once
Code:

CURRENT_SONG=`tail nohup.out | grep "Playing:" | cut -c 12-500`
is executed in the script.

I am able to get the current song without any trouble, but am not sure how to use sed to delete the current song from the playlist.

Here is the script (sed part is not working):

Code:

#!/bin/sh

# script to remove songs from a playlist and/or skip to the next song

####################################
# Variables
####################################
PLAYLIST_DIR="/music"
PLAYLIST="bedtime.playlist2"
DEBUG="1"

####################################
# Get the current playing song
####################################


# Change to root directory
cd /
CURRENT_SONG=`tail nohup.out | grep "Playing:" | cut -c 12-500`

if [ ${DEBUG} = "1" ]; then
    echo ${CURRENT_SONG}
fi

# We now have the current song, let's remove it from the playlist.

if [ ${DEBUG} = "1" ]; then
    echo Here is the sed command we are going to run: sed -e !\ ${CURRENT_SONG}!\d
fi

mv ${PLAYLIST_DIR}/${PLAYLIST} ${PLAYLIST_DIR}/${PLAYLIST}.$$
sed -e !\ ${CURRENT_SONG} !\d ${PLAYLIST_DIR}/${PLAYLIST}.$$ \
    > ${PLAYLIST_DIR}/${PLAYLIST}

if [ ${DEBUG} = "1" ]; then
    cat ${PLAYLIST_DIR}/${PLAYLIST}
fi

exit 0

I used \ \ around the expression, but now the variable is being used literallly instead of being replaced.

Help would be great, because sed is alien to me. Thanks.

wapcaplet 01-03-2005 08:51 PM

There's a page of handy sed one-liners that may help, but to me the easiest way seems to be using grep. Usually, grep prints matching lines; you can give it the -v option to print only lines that don't match:

Code:

grep -v "$CURRENT_SONG" "$PLAYLIST" > "$PLAYLIST"
The double-quotes help avoid potential problems with spaces in filenames.

deoren 01-03-2005 09:26 PM

grep saves the day
 
Quote:

Originally posted by wapcaplet
Usually, grep prints matching lines; you can give it the -v option to print only lines that don't match
You're exactly right. I was thinking over everything and instead of trying to remove something from the file, why not just use create a new file without that one line.

Thanks for replying!


All times are GMT -5. The time now is 03:47 PM.