Click to See Complete Forum and Search --> : How can I add text to a file?


happybunny
04-15-2004, 04:06 PM
I have an m3u file from, yuck, Windows. File strucure is \folder\folder\song.mp3

I have successfully replaced all the \ with / but now I need to add "/mp3s" to the begining of every line in the file.

file now:
/A's/ABBA/Dancing - Queen.mp3 (sorry, first song that came to mind!)
/Z's/ZZTop/Planet of Women.mp3

it needs to say:

/mp3s/A's/ABBA/Dancing -Queen
/mp3s/Z's/ZZTop/Planet of Women.mp3

How can i automaticaly do this?

Additionally, every other line starts with #something....I do not want to add /mp3s to that line.

Any ideas ?

Dark Ninja
04-15-2004, 04:14 PM
Personally, I would write myself a little Java program (since that's my strongest language) that just adds what you want to add, to the beginning of every line that does not contain a # sign.

You should be able to do something similar in almost any language. (Assuming you know any.)

Hayl
04-15-2004, 04:14 PM
you can do it using perl for sure, or probably with sed

bradfordgd
04-15-2004, 04:41 PM
Let me see if I've got this straight. You have a text file called m3u that contains a bunch of lines similar to the following;

/this/is/a/test
#/this/is/not
/this/is/another/test
#/again/this/isn't

You want to add /MP3 in front of the lines without #'s and get rid of the lines that have the # in front or you want to leave them unchanged?

Getting rid of the lines with # in front you could do something like this;

# cat m3u | grep -v "#" | awk '{print "/MP3" $1}' > new_m3u

happybunny
04-15-2004, 05:17 PM
that is sooo close!! but this is before:

/R's/Radiohead/Radiohead - Bullet Proof . . I Wish I Was.mp3
/R's/Rage Against the Machine/Rage Against The Machine - Bulls On Parade.mp3
/R's/Red Hot Chili Peppers/Red Hot Chili Peppers - Californication.mp3
/R's/REM/R.E.M - Losing My Religion.mp3

and this is the output:
/mp3s/R's/Misc
/mp3s/R's/Radiohead/Radiohead
/mp3s/R's/Rage
/mp3s/R's/Red
/mp3s/R's/REM/R.E.M

I guess awk doesnt like spaces since it is dropping everything after the first space.

Thanks--you have put me on the right track i think....any more help would be great though.

bradfordgd
04-15-2004, 05:50 PM
A space is the default field seperator in awk. So when you saw awk '{print $1}' it was printing only up to the first space. $2 would be the stuff after that up to the second space and so on. You can specify a field seperator with the -F flag. The following will do what you want for what you just showed me;

# cat m3u | grep -v "#" | awk '{print "/MP3" $0}' > new_m3u

Notice it has $0, which uses the entire line and ignores the field seperators.



Did I just see you hopping down the Mass Pike on my way home from work?

;-)

happybunny
04-15-2004, 05:55 PM
oddly enough, no...i am playing hooky today...just had a driveway installed and stayed home to watch it happen.

Thanks for your input...ill go try right now.

AWESOME!!!!! I have music again!!!

thanks again.

I have just begun looking at bash scripting, but couldn't wait to learn it, to fix it, to listen to my music.

Careful drivin' out there!

bradfordgd
04-15-2004, 06:38 PM
Oreilly has a real good book on Awk and Sed if you're interested in learning more about them.