Making a play-list

approximately the same question as the last time, but unfortunately I didn't get
a working answer.

I made a script with bash and gtkdialog that create a play-list. The output is for example :

gtk-media-pause | CB60471-05 - Gilbert, Brantley - Country Must Be Country Wide.zip | 28897 | /home/floris/Muziek/Karaoke/1341838939/CB60471-05 - Gilbert, Brantley - Country Must Be Country Wide.zip
gtk-media-pause | CB60471-06 - Parton, Dolly - Together You And I.zip | 13746 | /home/floris/Muziek/Karaoke/1341838939/CB60471-06 - Parton, Dolly - Together You And I.zip
gtk-media-pause | CB60471-07 - Campbell, Craig - Fish.zip | 6225 | /home/floris/Muziek/Karaoke/1341838939/CB60471-07 - Campbell, Craig - Fish.zip
gtk-media-pause | CB60471-08 - Rimes, LeAnn - Give.zip | 3386 | /home/floris/Muziek/Karaoke/1341838939/CB60471-08 - Rimes, LeAnn - Give.zip

I'm trying the move a song up the list..
In other words, how can I swap two lines?

I tried:

## $LIST is the play-list file
## $LINE is a random line from the play-list
## $UP_LINE is the line before $LINE
sed "s?$LINE?$UP_LINE?g;s?$UP_LINE?$LINE?g" "$LIST">"$LIST".bak

cat $LIST.bak
gtk-media-pause | CB60471-05 - Gilbert, Brantley - Country Must Be Country Wide.zip | 28897 | /home/floris/Muziek/Karaoke/1341838939/CB60471-05 - Gilbert, Brantley - Country Must Be Country Wide.zip
gtk-media-pause | CB60471-07 - Campbell, Craig - Fish.zip | 6225 | /home/floris/Muziek/Karaoke/1341838939/CB60471-07 - Campbell, Craig - Fish.zip
gtk-media-pause | CB60471-07 - Campbell, Craig - Fish.zip | 6225 | /home/floris/Muziek/Karaoke/1341838939/CB60471-07 - Campbell, Craig - Fish.zip
gtk-media-pause | CB60471-08 - Rimes, LeAnn - Give.zip | 3386 | /home/floris/Muziek/Karaoke/1341838939/CB60471-08 - Rimes, LeAnn - Give.zip

The expected is

gtk-media-pause | CB60471-05 - Gilbert, Brantley - Country Must Be Country Wide.zip | 28897 | /home/floris/Muziek/Karaoke/1341838939/CB60471-05 - Gilbert, Brantley - Country Must Be Country Wide.zip
gtk-media-pause | CB60471-07 - Campbell, Craig - Fish.zip | 6225 | /home/floris/Muziek/Karaoke/1341838939/CB60471-07 - Campbell, Craig - Fish.zip
gtk-media-pause | CB60471-06 - Parton, Dolly - Together You And I.zip | 13746 | /home/floris/Muziek/Karaoke/1341838939/CB60471-06 - Parton, Dolly - Together You And I.zip
gtk-media-pause | CB60471-08 - Rimes, LeAnn - Give.zip | 3386 | /home/floris/Muziek/Karaoke/1341838939/CB60471-08 - Rimes, LeAnn - Give.zip

I hope that someone give me an example with sed, awk, perl or any other line editor.

note: The play-list is random made. I only know the line I have selected. So a
solution with line numbers doesn't work. Also please notice that the variables can
contain almost any character.

Hi
Something like that ?

awk '{if (match($0,/Campbell/)) print; else { if (p) print p; p=$0 } } END {if (p) print p}' songs
1 Like

Thanks Chirel that worked.:b:
Can you explain the awk line, because I want to understand why it works?

Thanks again.

Hi

sure i can

awk '                         ## Let's call awk
{                             ## For each line of the file
if (match($0,/Campbell/))     ## Test if the word Campbell is in current line
  print;                      ## If so then just print it.
else {                        ## Else If current Line don't have the word Campbell
  if (p)                      ## Is there a previous line ?
    print p;                  ## Then print previous line.
  p=$0                        ## Previous line = current line.
}
}
END {                         ## We reach end of file
  if (p)                      ## Is there a Previous line left to print ?
    print p                   ## Then print it.
}'                            ## End of awk script.
 songs                        ## File name to read with awk.