Find the answer to your Linux question:
Results 1 to 7 of 7
Hello everyone! This is my first post, so I'll try not to sound like a n00b. My scenario is this: I have a Motorola ROKR E6 Mobile (Which runs a ...
  1. #1
    Just Joined!
    Join Date
    Aug 2007
    Posts
    8

    Bash Scripting: Help needed for Sed command.

    Hello everyone! This is my first post, so I'll try not to sound like a n00b. My scenario is this: I have a Motorola ROKR E6 Mobile (Which runs a MontaVista Mobile Linux). My Notebook is running Windows XP tablet Edition. I sync my music from the table to the ROKR successfully on a regular basis. However, I want the .M3U playlists to sync as well. When I do this, they sync in this format:

    Code:
    \MyMusic\Artist - Title.MP3
    However, for the mobile to understand them, I need to have a script edit the M3U playlists (on the mobile) to this format:

    Code:
    /mmc/mmca1/MyMusic/Artist - Title.MP3
    The reason being, the memory card of the phone is mounted to /mmc/mmca1/ Now, I can't change that, it would be too much redirecting in part of ringtones, apps, etc. I can't have MediaMonkey sync the music and playlists to /mmc/mmca1/MyMusic/ as it will show up in the phone as /mmc/mmca1/mmc/mmca1/MyMusic/ which, for reasons obvious to us here, will not read on the phone.

    Anyhow, this is what I've come up w/ so far:

    Code:
    #!/bin/bash
    # M3U Fixer by Imahottguy
    
    cd /mmc/mmca1/MyMusic/Playlists
    
    sed 's_\\MyMusic\\_/mmc/mmca1/MyMusic/_g' *
    Here's where I'm at: I can get it to print on the screen what I want. But what I want is for it to save to the .M3U playlists. There are about 6 of them in a folder (/mmc/mmca1/MyMusic/Playlists). This is why it cd to that directory. I then have it search for \MyMusic\ and then replace w/ /mmc/mmca1/MyMusic/. But this is all I can think of. I've searched the web for about 3 weeks now, and have had no luck. I was thinking something like creating a loop that runs for each file in the directory. But, to be honest, my abilities in scripting are still a bit limited.

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    This is an example to search for the .M3U files in the directory /mmc/mmca1/MyMusic/Playlists and to save the changes with the -i option:

    Code:
    #!/bin/bash
    
    cd /mmc/mmca1/MyMusic/Playlists
    
    for file in $(ls *.M3U)
    do
      sed -i 's_\\MyMusic\\_/mmc/mmca1/MyMusic/_' "$file"
    done
    Regards

  3. #3
    Just Joined!
    Join Date
    Aug 2007
    Posts
    8
    Sorry, but when I tried your example the prompt sput out this:
    Code:
    sed: Invalid option -- I
    I then copied and pasted the script into UE and tried it again, no go... Any ideas? I've never seen the option 'I', what does it do? Perhaps the sed utility in my mobile is outdated

    EDIT:

    After messing around, wouldn't the 'w' flag be the way to go? As in something like this:

    Code:
    #!/bin/bash
    
    cd /mmc/mmca1/MyMusic/Playlists
    
    # Since there is only M3U playlists in the folder i have it just ls
    
    for file in $(ls)
    do
         sed 's_\\MyMusic\\_/mmc/mmca1/MyMusic/_w $file'
    done
    This is what I'm theorizing... It locks up the bash prompt when I enter that in. So I'm sure that there are some discrepancies somewhere in the sed command.

    EDIT AGAIN:

    I've gotten one playlist to change... But none others...

    Code:
    #!/bin/bash
    
    cd /mmc/mmca1/MyMusic/Playlists
    
    # Since there is only M3U playlists in the folder i have it just ls
    
    for file in $(ls)
    do
         sed 's_\\MyMusic\\_/mmc/mmca1/MyMusic/_w $file' "$file"
    done
    EDIT AGAIN::

    I was wrong, none have changed... Still workin' on it

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    The '-i' option means "modify the file in place" (as opposed to just printing the output to stdout). You can duplicate the behavior with:
    Code:
    #!/bin/bash
    
    cd /mmc/mmca1/MyMusic/Playlists
    
    for file in $(ls *.M3U)
    do
      sed 's_\\MyMusic\\_/mmc/mmca1/MyMusic/_' "$file" > temp
      mv temp "$file"
    done
    Note that this will delete the playlist, and replace it with the modified one, so I suggest backing up your playlists until you have the sed command correct.

    EDIT:
    I am not familiar with the 'w' regex flag (nor do I believe it exists). What is it supposed to do?
    DISTRO=Arch
    Registered Linux User #388732

  5. #5
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by Imahottguy View Post
    I've never seen the option 'I', what does it do? Perhaps the sed utility in my mobile is outdated
    The option is '-i' not '-I', not with a capital.

    Regards

  6. #6
    Just Joined!
    Join Date
    Aug 2007
    Posts
    8
    though it printed invalid option -- I, I had inputed a lowercase 'i' as in:

    sed -i ...

    @Cabhan: Thank you very much. Thanks to everyone who helped me! Your method of putting it in temp worked perfectly. The 'w' flag that I referred to was one I read about in tutorial for the sed command. Here is the link: Here. Thank you again.

    Thanks to everyone who helped me! I really appreciate it.

  7. #7
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by Franklin52 View Post
    This is an example to search for the .M3U files in the directory /mmc/mmca1/MyMusic/Playlists and to save the changes with the -i option:

    Code:
    #!/bin/bash
    
    cd /mmc/mmca1/MyMusic/Playlists
    
    for file in $(ls *.M3U)
    do
      sed -i 's_\\MyMusic\\_/mmc/mmca1/MyMusic/_' "$file"
    done
    Regards
    Code:
    sed -i 's_\\MyMusic\\_/mmc/mmca1/MyMusic/_'  *.M3U

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...