Isolate lines in a text file and perform replacements

Discussion in 'Perl Misc' started by Robert Neville, Mar 30, 2007.

  1. I developed a shell script for renaming my mp3 files. I understand
    that this newsgroup revolves around Perl, yet my script uses Perl for
    full regular expression support (so I don't have to escape the
    patterns in sed). I just need fresh ideas for my shell script, which
    may involve the Perl portions.

    The script uses Perl and regular expressions to normalize the file
    names. The patterns reside in a preset file. My current development
    efforts involve applying these regex patterns to playlist and xml
    files. I want to process the text inside these files. Eventually, the
    script would help me rename my itunes database, yet it has several
    purposes beyond mp3 renaming.

    The script performs a recursive search and finds files with these
    extensions (m3u, sfv, and xml). It iterates (while loop) through an
    external file with regular expression patterns. Then it combines the
    patterns and placed them in a variable, which is passed to Perl. The
    script below performs this task (yet has not been thorough tested). It
    has a major shortcoming that the Perl line works on the entire file
    when it should only replace the lines with mp3 pointers. Here's the
    call for assistance since I am having code block. Maybe, someone could
    help me with different logic or a traditional grep solution.

    find ./ -regex ".*\(m3u\|sfv\|xml\)$" -type f -print | while read FILE
    do
    while read -r REGEX REPLACE line
    do
    CODE="$CODE; s/$REGEX/$REPLACE/g"
    done < "$PRESET"
    echo "$CODE"
    echo "perl -pi.bak -e "s/$REGEX/$REPLACE/g" $FILE"
    #PRESET may contain over twenty five regex patterns
    done

    btw I am doing this script in Bash, because Perl is foreign territory.
     
    Robert Neville, Mar 30, 2007
    #1
    1. Advertising

  2. Robert Neville

    Guest

    On Mar 30, 11:16 am, Robert Neville <>
    wrote:

    > It has a major shortcoming that the Perl line works on the entire file


    That's because you told Perl to work on the entire file.

    > Maybe, someone could help me with different logic or a traditional
    > grep solution.


    You want to selectively alter MP3 tags, right? And you don't want
    your alterations to accidentally apply so some other data in the file,
    right?

    To do this "properly" you need program logic which can identify
    specific tags, and only operate on those tags. That's gonna be hard
    to do when most of the code is a korn script.

    > btw I am doing this script in Bash, because Perl is foreign territory.


    Then I respectfully submit that you ought to learn a little Perl,
    because Perl will make this task (and many others) rather simple. In
    fact, you can use a CPAN module (such as MP3::Info, my personal
    favorite) to do most of the work (it will allow you to query and
    modify specific tags).

    If you post a specific example of which tag you want to modify, and a
    few specific regexps you wish to apply, someone (maybe me) might post
    a bit of sample code to get you started.


    --
    The best way to get a good answer is to ask a good question.
    David Filmer (http://DavidFilmer.com)
     
    , Mar 30, 2007
    #2
    1. Advertising

  3. Robert Neville

    Mumia W. Guest

    On 03/30/2007 01:16 PM, Robert Neville wrote:
    > [...] script below performs this task (yet has not been thorough tested). It
    > has a major shortcoming that the Perl line works on the entire file
    > when it should only replace the lines with mp3 pointers. Here's the
    > call for assistance since I am having code block. Maybe, someone could
    > help me with different logic or a traditional grep solution.
    >
    > find ./ -regex ".*\(m3u\|sfv\|xml\)$" -type f -print | while read FILE
    > do
    > while read -r REGEX REPLACE line
    > do
    > CODE="$CODE; s/$REGEX/$REPLACE/g"
    > done < "$PRESET"
    > echo "$CODE"
    > echo "perl -pi.bak -e "s/$REGEX/$REPLACE/g" $FILE"


    Is this what you're looking for?

    echo "perl -pi.bak -e \"s/$REGEX/$REPLACE/g if /\.mp3\b/\" $FILE"


    > #PRESET may contain over twenty five regex patterns
    > done
    >
    > btw I am doing this script in Bash, because Perl is foreign territory.
     
    Mumia W., Mar 30, 2007
    #3
  4. On Fri, 30 Mar 2007 20:53:45 GMT, "Mumia W."
    <> wrote:

    >On 03/30/2007 01:16 PM, Robert Neville wrote:
    >> [...] script below performs this task (yet has not been thorough tested). It
    >> has a major shortcoming that the Perl line works on the entire file
    >> when it should only replace the lines with mp3 pointers. Here's the
    >> call for assistance since I am having code block. Maybe, someone could
    >> help me with different logic or a traditional grep solution.
    >>
    >> find ./ -regex ".*\(m3u\|sfv\|xml\)$" -type f -print | while read FILE
    >> do
    >> while read -r REGEX REPLACE line
    >> do
    >> CODE="$CODE; s/$REGEX/$REPLACE/g"
    >> done < "$PRESET"
    >> echo "$CODE"
    >> echo "perl -pi.bak -e "s/$REGEX/$REPLACE/g" $FILE"

    >
    >Is this what you're looking for?
    >
    >echo "perl -pi.bak -e \"s/$REGEX/$REPLACE/g if /\.mp3\b/\" $FILE"
    >
    >
    >> #PRESET may contain over twenty five regex patterns
    >> done
    >>
    >> btw I am doing this script in Bash, because Perl is foreign territory.

    >

    Thanks Mumia

    This approach looks like a strong possibility. How would I search for
    more information on this construct? The perl man page is extensive and
    the "if" keyword is too general. Please let me know if you have links
    or examples where this construct has been used. Here's the pseudo code
    from your suggestion.


    find ./ -regex ".*\(m3u\|sfv\|xml\)$" -type f -print | while read FILE
    do
    while read -r REGEX REPLACE line
    do
    CODE="$CODE; s/$REGEX/$REPLACE/g"
    done < "$PRESET"
    #PRESET may contain over twenty five regex patterns
    perl -pi.bak -e \""$CODE" if /\.mp3\b/\" "$FILE"
    done
     
    Robert Neville, Apr 3, 2007
    #4
  5. Robert Neville

    Mumia W. Guest

    On 04/02/2007 09:43 PM, Robert Neville wrote:
    > On Fri, 30 Mar 2007 20:53:45 GMT, "Mumia W."
    > <> wrote:
    >
    >> On 03/30/2007 01:16 PM, Robert Neville wrote:
    >>> [...] script below performs this task (yet has not been thorough tested). It
    >>> has a major shortcoming that the Perl line works on the entire file
    >>> when it should only replace the lines with mp3 pointers. Here's the
    >>> call for assistance since I am having code block. Maybe, someone could
    >>> help me with different logic or a traditional grep solution.
    >>>
    >>> find ./ -regex ".*\(m3u\|sfv\|xml\)$" -type f -print | while read FILE
    >>> do
    >>> while read -r REGEX REPLACE line
    >>> do
    >>> CODE="$CODE; s/$REGEX/$REPLACE/g"
    >>> done < "$PRESET"
    >>> echo "$CODE"
    >>> echo "perl -pi.bak -e "s/$REGEX/$REPLACE/g" $FILE"

    >> Is this what you're looking for?
    >>
    >> echo "perl -pi.bak -e \"s/$REGEX/$REPLACE/g if /\.mp3\b/\" $FILE"
    >>
    >>
    >>> #PRESET may contain over twenty five regex patterns
    >>> done
    >>>
    >>> btw I am doing this script in Bash, because Perl is foreign territory.

    > Thanks Mumia
    >
    > This approach looks like a strong possibility. How would I search for
    > more information on this construct? The perl man page is extensive and
    > the "if" keyword is too general. Please let me know if you have links
    > or examples where this construct has been used. Here's the pseudo code
    > from your suggestion.
    >
    >
    > find ./ -regex ".*\(m3u\|sfv\|xml\)$" -type f -print | while read FILE
    > do
    > while read -r REGEX REPLACE line
    > do
    > CODE="$CODE; s/$REGEX/$REPLACE/g"
    > done < "$PRESET"
    > #PRESET may contain over twenty five regex patterns
    > perl -pi.bak -e \""$CODE" if /\.mp3\b/\" "$FILE"
    > done


    The information is located in several places. Perlretut talks about
    "\b". /Somewhere/ in the documentation the need to backslash
    double-quotes within double-quoted strings is discussed; however, that
    is not needed for the "perl" command above.

    I would write it like so:

    perl -pi.bak -e "$CODE if /\.mp3\b/" "$FILE"
     
    Mumia W., Apr 3, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian McCullough
    Replies:
    0
    Views:
    499
    Brian McCullough
    Feb 16, 2007
  2. Murali
    Replies:
    2
    Views:
    572
    Jerry Coffin
    Mar 9, 2006
  3. ThatsIT.net.au

    web config file section replacements

    ThatsIT.net.au, Sep 12, 2008, in forum: ASP .Net
    Replies:
    3
    Views:
    504
    ThatsIT.net.au
    Sep 27, 2008
  4. Jeff Thies

    making replacements in a file

    Jeff Thies, Jul 11, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    111
    Mina Naguib
    Jul 11, 2003
  5. George Mpouras

    multiple text replacements from a hash

    George Mpouras, Apr 1, 2013, in forum: Perl Misc
    Replies:
    2
    Views:
    173
    George Mpouras
    Apr 1, 2013
Loading...

Share This Page