MattJayC

Q: Correcting Text Document for a file list

I've decided to post a fresh request, as I figure this should be possible but is currently beyond me.

 

I have some videos that  I need renaming, but my lists need organising first.

 

Here is the  Input text and output text that I would like to achieve via scripting/terminal commands.

I figure there isn't the need to use excel all the time for something like this. Thus relies on less power being used to do so.

 

Many thanks in advance to anyone that can help.

Matt

 

 

Input Text fileOutput Text File

Introducing Scripts

1m 19s

9. Script 2015 Updates

1h 23m

Creating Stripts

10m 57s

Exporting scripts

4m 49s

Previewing your script

4m 11s

11. Actual Working Script

1h 23m

Work Practices

5m 43s

Work scripts

2m 1s

C09E01 _Script Updates_ Creating Stripts

C09E02 _Script Updates_ Exporting scripts

C09E03 _Script Updates_ Previewing your script

C10E01 _Actual Working Script_ Work Practices

C10E02 _Actual Working Script_ Work scripts

iMac, OS X Mavericks (10.9.4)

Posted on Oct 9, 2015 12:38 PM

Close

Q: Correcting Text Document for a file list

  • All replies
  • Helpful answers

  • by Hiroto,

    Hiroto Hiroto Oct 10, 2015 8:34 AM in response to MattJayC
    Level 5 (7,286 points)
    Oct 10, 2015 8:34 AM in response to MattJayC

    Hello

     

    One question. Are you sure what you want is this:

     

    C09E01 _Script Updates_ Creating Stripts
    C09E02 _Script Updates_ Exporting scripts
    C09E03 _Script Updates_ Previewing your script
    C11E01 _Actual Working Script_ Work Practices
    C11E02 _Actual Working Script_ Work scripts
    

     

     

    and NOT this:

     

    C09E01 _Script 2015 Updates_ Creating Stripts
    C09E02 _Script 2015 Updates_ Exporting scripts
    C09E03 _Script 2015 Updates_ Previewing your script
    C11E01 _Actual Working Script_ Work Practices
    C11E02 _Actual Working Script_ Work scripts
    

     

     

    ?

     

    * I presumed C10E01 and C10E02 should read C11E01 and C11E02 respectively.

     

    Assuming the answer is yes, here's a perl script you may try:

     

     

    #!/bin/bash
    
    perl -CSDA -w <<'EOF' - in.txt > out.txt
    use strict;
    my %h = ();
    my $c = 0;
    while (<>) {
        chomp;
        $c = $1 if / ^([0-9]+) \. \s /ox and $1 != $c;
        push @{$h{$c}}, $_ if $c and not /(?:[0-9]+(h|m|s)){1,3}/;
    }
    for my $k (sort {$a <=> $b} keys %h) {
    #   (my $t = $h{$k}->[0]) =~ s/ ^[0-9]+ \.  \s+ //ox;   # remove leading /[0-9]+\.\s+/
        (my $t = $h{$k}->[0]) =~ s/  [0-9]+ \.? \s+ //ogx;  # remove every /[0-9]+\.?\s+/
        for my $i (1 .. @{$h{$k}} - 1) {
            printf qq(C%02dE%02d _%s_ %s\n), $k, $i, $t, $h{$k}->[$i];
        }
    }
    EOF
    

     

     

     

    And an AppleScript wrapper of it:

     

     

    set infile to (choose file of type {"txt"} with prompt "Choose input text file")'s POSIX path
    set outfile to (choose file name default name "out.txt" with prompt "Choose output text file name and location")'s POSIX path
    
    do shell script "perl -CSDA -w <<'EOF' - " & infile's quoted form & " > " & outfile's quoted form & "
    use strict;
    my %h = ();
    my $c = 0;
    while (<>) {
        chomp;
        $c = $1 if / ^([0-9]+) \\. \\s /ox and $1 != $c;
        push @{$h{$c}}, $_ if $c and not /(?:[0-9]+(h|m|s)){1,3}/;
    }
    for my $k (sort {$a <=> $b} keys %h) {
    #   (my $t = $h{$k}->[0]) =~ s/ ^[0-9]+ \\.  \\s+ //ox;     # remove leading /[0-9]+\\.\\s+/
        (my $t = $h{$k}->[0]) =~ s/  [0-9]+ \\.? \\s+ //ogx;    # remove every /[0-9]+\\.?\\s+/
        for my $i (1 .. @{$h{$k}} - 1) {
            printf qq(C%02dE%02d _%s_ %s\\n), $k, $i, $t, $h{$k}->[$i];
        }
    }
    EOF"
    

     

     

    Hope this may help,

    H

  • by MattJayC,

    MattJayC MattJayC Oct 10, 2015 12:03 PM in response to MattJayC
    Level 1 (5 points)
    Desktops
    Oct 10, 2015 12:03 PM in response to MattJayC

    Opps that was a bit of a big typo. Yes your second answer was right!

     

    I'm putting it too the test at the moment and seams to be working well at the moment!

     

    Many Thanks!

  • by MattJayC,

    MattJayC MattJayC Oct 10, 2015 1:39 PM in response to Hiroto
    Level 1 (5 points)
    Desktops
    Oct 10, 2015 1:39 PM in response to Hiroto

    I've found a minor glitch.

     

    Some chapters as the are like a prefix they are marked as 0. as a result all those lines in the chapter get dropped.

    Matt

  • by Hiroto,

    Hiroto Hiroto Oct 10, 2015 11:10 PM in response to MattJayC
    Level 5 (7,286 points)
    Oct 10, 2015 11:10 PM in response to MattJayC

    OK. Here's revised scripts which will treat chapter = 0 correctly and only remove the leading chapter number from chapter title.

     

     

    - perl script in shell

     

     

    #!/bin/bash
    
    perl -CSDA -w <<'EOF' - in.txt > out.txt
    use strict;
    my %h = ();
    my $c = -1;
    while (<>) {
        chomp;
        $c = $1 if / ^([0-9]+) \. \s /ox and $1 != $c;
        push @{$h{$c}}, $_ if ($c >= 0) and not /(?:[0-9]+(h|m|s)){1,3}/;
    }
    for my $k (sort {$a <=> $b} keys %h) {
        (my $t = $h{$k}->[0]) =~ s/ ^[0-9]+ \.  \s+ //ox;   # remove leading /[0-9]+\.\s+/
        for my $i (1 .. @{$h{$k}} - 1) {
            printf qq(C%02dE%02d _%s_ %s\n), $k, $i, $t, $h{$k}->[$i];
        }
    }
    EOF
    

     

     

     

    - applescript wrapper

     

     

    set infile to (choose file of type {"txt"} with prompt "Choose input text file")'s POSIX path
    set outfile to (choose file name default name "out.txt" with prompt "Choose output text file name and location")'s POSIX path
    
    do shell script "perl -CSDA -w <<'EOF' - " & infile's quoted form & " > " & outfile's quoted form & "
    use strict;
    my %h = ();
    my $c = -1;
    while (<>) {
        chomp;
        $c = $1 if / ^([0-9]+) \\. \\s /ox and $1 != $c;
        push @{$h{$c}}, $_ if ($c >= 0) and not /(?:[0-9]+(h|m|s)){1,3}/;
    }
    for my $k (sort {$a <=> $b} keys %h) {
        (my $t = $h{$k}->[0]) =~ s/ ^[0-9]+ \\.  \\s+ //ox; # remove leading /[0-9]+\\.\\s+/
        for my $i (1 .. @{$h{$k}} - 1) {
            printf qq(C%02dE%02d _%s_ %s\\n), $k, $i, $t, $h{$k}->[$i];
        }
    }
    EOF"
    

     

     

     

    Good luck,

    H

  • by Hiroto,Solvedanswer

    Hiroto Hiroto Oct 11, 2015 3:38 AM in response to Hiroto
    Level 5 (7,286 points)
    Oct 11, 2015 3:38 AM in response to Hiroto

    Oops. The previous time code (1h 2m 3s etc) detection pattern is not so precise. Here's correction.

     

     

    - perl script in shell

     

     

    #!/bin/bash
    
    perl -CSDA -w <<'EOF' - in.txt > out.txt
    use strict;
    my %h = ();
    my $c = -1;
    while (<>) {
        chomp;
        $c = $1 if / ^([0-9]+) \. \s /ox and $1 != $c;
        push @{$h{$c}}, $_ if ($c >= 0) and not /^(?:[0-9]+(h|m|s) *){1,3}$/;   # CORRECTED
    }
    for my $k (sort {$a <=> $b} keys %h) {
        (my $t = $h{$k}->[0]) =~ s/ ^[0-9]+ \.  \s+ //ox;   # remove leading /[0-9]+\.\s+/
        for my $i (1 .. @{$h{$k}} - 1) {
            printf qq(C%02dE%02d _%s_ %s\n), $k, $i, $t, $h{$k}->[$i];
        }
    }
    EOF
    

     

     

     

    - applescript wrapper

     

     

    set infile to (choose file of type {"txt"} with prompt "Choose input text file")'s POSIX path
    set outfile to (choose file name default name "out.txt" with prompt "Choose output text file name and location")'s POSIX path
    
    do shell script "perl -CSDA -w <<'EOF' - " & infile's quoted form & " > " & outfile's quoted form & "
    use strict;
    my %h = ();
    my $c = -1;
    while (<>) {
        chomp;
        $c = $1 if / ^([0-9]+) \\. \\s /ox and $1 != $c;
        push @{$h{$c}}, $_ if ($c >= 0) and not /^(?:[0-9]+(h|m|s) *){1,3}$/;   # CORRECTED
    }
    for my $k (sort {$a <=> $b} keys %h) {
        (my $t = $h{$k}->[0]) =~ s/ ^[0-9]+ \\.  \\s+ //ox; # remove leading /[0-9]+\\.\\s+/
        for my $i (1 .. @{$h{$k}} - 1) {
            printf qq(C%02dE%02d _%s_ %s\\n), $k, $i, $t, $h{$k}->[$i];
        }
    }
    EOF"
    

     

     

     

    Cheers,

    H

  • by MattJayC,

    MattJayC MattJayC Oct 13, 2015 1:17 PM in response to Hiroto
    Level 1 (5 points)
    Desktops
    Oct 13, 2015 1:17 PM in response to Hiroto

    which line deletes the first line?

     

    Many Thanks

    All working fine now!