Perl Command Line Question

Discussion in 'Mac Programming' started by fivetoadsloth, Jan 26, 2012.

  1. macrumors 65816

    fivetoadsloth

    Joined:
    Aug 15, 2006
    #1
    I'm trying to automate a process, but am running into a few problems.

    I have a program that I am running with the following command:

    Code:
    ./program -in file_001.txt -out file_001
    where I am reading in the in file, and then outputting files with the end filename.

    I need to do this for around 90 different files (up to file_088.txt).

    I'm trying to put together a perl script that will just run them in the command line, one at a time, incrementing the filename that it reads in, and makes sure that that is equal to the out name.


    Code:
    for($f=1;$f<=88;f++){
    './program -in file_0$f.txt -out file_0$f'
    }
    
    Something is definitely wrong, and I'm not sure how to make sure that for 1,2,3,4,5,6,7,8,9 they are listed as '01,02,...,09' to make the naming scheme work properly.


    Thanks!
     
  2. balamw, Jan 26, 2012
    Last edited: Jan 26, 2012

    Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #2
    This kind of thing is actually quite easy to do as a shell script. Is there a particular reason you need/want to use perl?

    EDIT: if bash is OK you can start here:
    Code:
    for file in *.txt; do echo -in $file -out ${file%.*} ; done
    B
     
  3. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #3
    You want to use the printf command to format the number.
    Code:
    for($f=1;$f<=88;f++){
      my $s = printf('%02d', $f)
      `./program -in file_$s.txt -out file_$s`
    }
    
     
  4. thread starter macrumors 65816

    fivetoadsloth

    Joined:
    Aug 15, 2006
    #4
    Thanks! I guess I'm just not really familiar with shell scripts.
    Thanks! I'm getting the following errors, though:

    Backticks found where operator expected at run_them_all.pl line 5, near ")

    Can't modify constant item in postincrement (++) at run_them_all.pl line 3, near "f++"
    syntax error at run_them_all.pl line 5, near ")
     
  5. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #5
    I've typically used system for this kind of stuff. Backticks are supposed to be for when you want to use the output so you may need to assign that to a variable.

    B
     
  6. macrumors 603

    mobilehaathi

    Joined:
    Aug 19, 2008
    Location:
    The Anthropocene
    #6
    semi-colons?
     
  7. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #7
    That too.

    B
     
  8. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #8
    Oops. :eek: And a missing $.

    Code:
    for($f=1;$f<=88;[COLOR=red]$[/COLOR]f++){
      my $s = printf('%02d', $f)[COLOR=red];[/COLOR]
      system('./program', '-in', 'file_$s.txt', '-out', 'file_$s')[COLOR=red];[/COLOR]
    }
     
  9. thread starter macrumors 65816

    fivetoadsloth

    Joined:
    Aug 15, 2006
    #9
    Forgive me, I'm still new to this. I have the following:

    Code:
    #!/usr/bin/perl
    #
    for($f=1;$f<=88;f++){
      my $s = printf('%02d', $f);
       system("./program -in file_$s.txt -out file_$s");
        }

    but I am getting a
    "Can't modify constant item in postincrement (++) at run_them_all.pl line 3, near "f++"
    syntax error at run_them_all.pl line 5, near ")
    system""

    error.
     
  10. macrumors 603

    mobilehaathi

    Joined:
    Aug 19, 2008
    Location:
    The Anthropocene
    #10
    Still need a semi-colon!

     
  11. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #11
    You also need $f++ instead of f++. ;)

    Break the problem into bits. get the loop working then get the system call working.

    IIRC system wants an array.

    B
     
  12. macrumors 603

    mobilehaathi

    Joined:
    Aug 19, 2008
    Location:
    The Anthropocene
    #12
    ooh, good catch. I didn't even read the error message!
     
  13. dmi
    macrumors regular

    Joined:
    Dec 21, 2010
    #13
    Code:
    for $f ('001'..'088'){
     `./program -in file_$f.txt -out file_$f`
    }
    
     

Share This Page