Mac Perl Command Line Question

fivetoadsloth

macrumors 65816
Original poster
Aug 15, 2006
1,035
0
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!
 

balamw

Moderator
Staff member
Aug 16, 2005
19,366
978
New England
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
 
Last edited:

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
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`
}
 

fivetoadsloth

macrumors 65816
Original poster
Aug 15, 2006
1,035
0
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
Thanks! I guess I'm just not really familiar with shell scripts.
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`
}
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 ")
 

mobilehaathi

macrumors G3
Aug 19, 2008
9,350
6,291
The Anthropocene
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 ")
semi-colons?
 

fivetoadsloth

macrumors 65816
Original poster
Aug 15, 2006
1,035
0
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
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.
 

mobilehaathi

macrumors G3
Aug 19, 2008
9,350
6,291
The Anthropocene
Still need a semi-colon!

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.
 

balamw

Moderator
Staff member
Aug 16, 2005
19,366
978
New England
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""
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
 

dmi

macrumors regular
Dec 21, 2010
120
2
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!
Code:
for $f ('001'..'088'){
 `./program -in file_$f.txt -out file_$f`
}
 

Similar threads

  • jasnw
2
Replies
2
Views
1K
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.