LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-03-2005, 04:23 PM   #1
deoren
Member
 
Registered: Oct 2003
Location: USA
Distribution: Ubuntu
Posts: 214

Rep: Reputation: 30
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.

Last edited by deoren; 04-11-2005 at 08:37 AM.
 
Old 01-03-2005, 08:51 PM   #2
wapcaplet
Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
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.
 
Old 01-03-2005, 09:26 PM   #3
deoren
Member
 
Registered: Oct 2003
Location: USA
Distribution: Ubuntu
Posts: 214

Original Poster
Rep: Reputation: 30
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!

Last edited by deoren; 01-03-2005 at 09:28 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] delete the end of each line in a file with sed angel115 Linux - Newbie 3 11-16-2005 04:41 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
delete line specified by variable wihin shel script with sed lnchatterbox Linux - Newbie 2 02-23-2004 01:24 PM
cannot delete certain files with sed ganninu Linux - General 6 12-10-2003 03:31 PM
sed: replace one line with >one line bbeers Programming 3 11-19-2002 05:27 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration