sed swap lines

Hi,

I think it is possible with sed, but I'm not sure...

I've a file that contains some text and filenames:

gtk-media-pause | CB60471-05 - Gilbert, Brantley - Country Must Be Country Wide.zip | 8175 | /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 | 2924 | /home/floris/Muziek/Karaoke/1341838939/CB60471-06 - Parton, Dolly - Together You And I.zip
gtk-media-pause | CB60471-07 - Campbell, Craig - Fish.zip | 17646 | /home/floris/Muziek/Karaoke/1341838939/CB60471-07 - Campbell, Craig - Fish.zip
gtk-media-pause | CB60471-08 - Rimes, LeAnn - Give.zip | 15597 | /home/floris/Muziek/Karaoke/1341838939/CB60471-08 - Rimes, LeAnn - Give.zip

Is it possible to swap two lines, so the output would be like:

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

Edit: the file is made randomly, the only know variable is field 3 (8175, 17646, ect...) and also these numbers are random

thanks,

jkfloris

I would suggest that you take a look at

http://sed.sourceforge.net/sed1line.txt

Check out: the code the reverses line order.

And then come back to considering awk. Sed scripts are sometimes hard to decode and maintain.

I have read the one-liners many times, unfortunately I still get an error

$cat list
gtk-media-pause | CB60471-05 - Gilbert, Brantley - Country Must Be Country Wide.zip | 25958 | /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 | 18800 | /home/floris/Muziek/Karaoke/1341838939/CB60471-06 - Parton, Dolly - Together You And I.zip
gtk-media-pause | CB60471-07 - Campbell, Craig - Fish.zip | 22067 | /home/floris/Muziek/Karaoke/1341838939/CB60471-07 - Campbell, Craig - Fish.zip
gtk-media-pause | CB60471-08 - Rimes, LeAnn - Give.zip | 528 | /home/floris/Muziek/Karaoke/1341838939/CB60471-08 - Rimes, LeAnn - Give.zip

$cat script.sh
#!/bin/bash

MUZIEK="22067"

LINES=$(grep -B1 "| $MUZIEK |" list)
NEW_LINES=$(grep -B1 "| $MUZIEK |" list | tac)
sed -i "s?$LINES?$NEW_LINES?g" list

I already use the "?" as the delimiter,
if I run script.sh, sed gives me:

sed: expressie #1, teken 172: onafgemaakte 's'-opdracht
(in English: sed expression #1,  character 172: command `s' unfinished)

I think the problem is the newline character in $LINES and $NEW_LINES, but
I hoped that sed was the "ultimate" stream editor and was able to do the trick.

---------- Post updated 25-07-12 at 04:20 PM ---------- Previous update was 24-07-12 at 07:20 PM ----------

can you give me an example in awk?

I tried:

LINES=$(grep -B1 "| $MUZIEK |" "$LIST")
NEW_LINES=$(grep -B1 "| $MUZIEK |" "$LIST" | tac)
awk "{gsub(/$LINES/,$NEW_LINES)} 1" "$LIST">$LIST.bak

uncut output:

++ grep -B1 '| 12890 |' /tmp/karsch/list
+ LINES='gtk-media-pause | CB60471-05 - Gilbert, Brantley - Country Must Be Country Wide.zip | 7659 | /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 | 12890 | /home/floris/Muziek/Karaoke/1341838939/CB60471-06 - Parton, Dolly - Together You And I.zip'
++ grep -B1 '| 12890 |' /tmp/karsch/list
++ tac
+ NEW_LINES='gtk-media-pause | CB60471-06 - Parton, Dolly - Together You And I.zip | 12890 | /home/floris/Muziek/Karaoke/1341838939/CB60471-06 - Parton, Dolly - Together You And I.zip
gtk-media-pause | CB60471-05 - Gilbert, Brantley - Country Must Be Country Wide.zip | 7659 | /home/floris/Muziek/Karaoke/1341838939/CB60471-05 - Gilbert, Brantley - Country Must Be Country Wide.zip'
+ awk '{gsub(/gtk-media-pause | CB60471-05 - Gilbert, Brantley - Country Must Be Country Wide.zip | 7659 | /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 | 12890 | /home/floris/Muziek/Karaoke/1341838939/CB60471-06 - Parton, Dolly - Together You And I.zip/,gtk-media-pause | CB60471-06 - Parton, Dolly - Together You And I.zip | 12890 | /home/floris/Muziek/Karaoke/1341838939/CB60471-06 - Parton, Dolly - Together You And I.zip
gtk-media-pause | CB60471-05 - Gilbert, Brantley - Country Must Be Country Wide.zip | 7659 | /home/floris/Muziek/Karaoke/1341838939/CB60471-05 - Gilbert, Brantley - Country Must Be Country Wide.zip)} 1' /tmp/karsch/list
awk: 1: unexpected character '.'
awk: line 2: syntax error at or near CB60471
awk: line 3: syntax error at or near CB60471
awk: line 3: extra ')'