Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 26, 2012, 05:42 PM   #1
fivetoadsloth
macrumors 65816
 
fivetoadsloth's Avatar
 
Join Date: Aug 2006
Perl Command Line Question

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!
__________________
Lasers not Losers
N ⊂ Z ⊂ Q ⊂ R ⊂ C.
fivetoadsloth is offline   0 Reply With Quote
Old Jan 26, 2012, 05:49 PM   #2
balamw
Moderator
 
balamw's Avatar
 
Join Date: Aug 2005
Location: New England, USA
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
__________________
MBA (13" 1.7 GHz 128GB), UMBP (15" SD 2.8 GHz), UMB (13" 2.4 GHz), iMac (17" Yonah), 32GB iPad 3 WiFi+LTE, 64 GB iPad WiFi, 32 GB iPhone 5, Airport Extreme

Last edited by balamw; Jan 26, 2012 at 05:57 PM.
balamw is offline   0 Reply With Quote
Old Jan 26, 2012, 05:49 PM   #3
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: 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`
}
jiminaus is offline   0 Reply With Quote
Old Jan 26, 2012, 06:52 PM   #4
fivetoadsloth
Thread Starter
macrumors 65816
 
fivetoadsloth's Avatar
 
Join Date: Aug 2006
Quote:
Originally Posted by balamw View Post
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.
Quote:
Originally Posted by jiminaus View Post
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 ")
__________________
Lasers not Losers
N ⊂ Z ⊂ Q ⊂ R ⊂ C.
fivetoadsloth is offline   0 Reply With Quote
Old Jan 26, 2012, 06:56 PM   #5
balamw
Moderator
 
balamw's Avatar
 
Join Date: Aug 2005
Location: New England, USA
Quote:
Originally Posted by fivetoadsloth View Post
Backticks found where operator expected
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
__________________
MBA (13" 1.7 GHz 128GB), UMBP (15" SD 2.8 GHz), UMB (13" 2.4 GHz), iMac (17" Yonah), 32GB iPad 3 WiFi+LTE, 64 GB iPad WiFi, 32 GB iPhone 5, Airport Extreme
balamw is offline   0 Reply With Quote
Old Jan 26, 2012, 06:57 PM   #6
mobilehaathi
macrumors 601
 
mobilehaathi's Avatar
 
Join Date: Aug 2008
Location: Bay Area, CA
Quote:
Originally Posted by fivetoadsloth View Post
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?
__________________
The true way is along a rope that is not spanned high in the air, but only just above the ground. It seems intended more to cause stumbling than to be walked along.
mobilehaathi is offline   0 Reply With Quote
Old Jan 26, 2012, 07:00 PM   #7
balamw
Moderator
 
balamw's Avatar
 
Join Date: Aug 2005
Location: New England, USA
Quote:
Originally Posted by mobilehaathi View Post
semi-colons?
That too.

B
__________________
MBA (13" 1.7 GHz 128GB), UMBP (15" SD 2.8 GHz), UMB (13" 2.4 GHz), iMac (17" Yonah), 32GB iPad 3 WiFi+LTE, 64 GB iPad WiFi, 32 GB iPhone 5, Airport Extreme
balamw is offline   0 Reply With Quote
Old Jan 26, 2012, 07:10 PM   #8
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Quote:
Originally Posted by mobilehaathi View Post
semi-colons?
Oops. And a missing $.

Code:
for($f=1;$f<=88;$f++){
  my $s = printf('%02d', $f);
  system('./program', '-in', 'file_$s.txt', '-out', 'file_$s');
}
jiminaus is offline   0 Reply With Quote
Old Jan 26, 2012, 07:11 PM   #9
fivetoadsloth
Thread Starter
macrumors 65816
 
fivetoadsloth's Avatar
 
Join Date: Aug 2006
Quote:
Originally Posted by balamw View Post
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.
__________________
Lasers not Losers
N ⊂ Z ⊂ Q ⊂ R ⊂ C.
fivetoadsloth is offline   0 Reply With Quote
Old Jan 26, 2012, 07:12 PM   #10
mobilehaathi
macrumors 601
 
mobilehaathi's Avatar
 
Join Date: Aug 2008
Location: Bay Area, CA
Still need a semi-colon!

Quote:
Originally Posted by fivetoadsloth View Post
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.
__________________
The true way is along a rope that is not spanned high in the air, but only just above the ground. It seems intended more to cause stumbling than to be walked along.
mobilehaathi is offline   0 Reply With Quote
Old Jan 26, 2012, 07:14 PM   #11
balamw
Moderator
 
balamw's Avatar
 
Join Date: Aug 2005
Location: New England, USA
Quote:
Originally Posted by fivetoadsloth View Post
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
__________________
MBA (13" 1.7 GHz 128GB), UMBP (15" SD 2.8 GHz), UMB (13" 2.4 GHz), iMac (17" Yonah), 32GB iPad 3 WiFi+LTE, 64 GB iPad WiFi, 32 GB iPhone 5, Airport Extreme
balamw is offline   0 Reply With Quote
Old Jan 26, 2012, 07:15 PM   #12
mobilehaathi
macrumors 601
 
mobilehaathi's Avatar
 
Join Date: Aug 2008
Location: Bay Area, CA
Quote:
Originally Posted by balamw View Post
You also need $f++ instead of f++.

B
ooh, good catch. I didn't even read the error message!
__________________
The true way is along a rope that is not spanned high in the air, but only just above the ground. It seems intended more to cause stumbling than to be walked along.
mobilehaathi is offline   0 Reply With Quote
Old Jan 27, 2012, 01:24 AM   #13
dmi
macrumors member
 
Join Date: Dec 2010
Quote:
Originally Posted by fivetoadsloth View Post
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`
}
dmi is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Tags
perl

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Dictonary from command line Crusader007 OS X 1 Feb 25, 2014 07:21 PM
General: iOS7 command line... Equivalent of "say" command? RobertoG Jailbreaks and iOS Hacks 0 Jan 19, 2014 05:05 PM
Command Line newb jeanniebeannie Mac Basics and Help 8 Oct 9, 2013 11:25 AM
Get Full Model Name from the Command Line cranfordio OS X 3 Mar 21, 2013 01:07 PM
Need Command-line help EricBrian Community Discussion 1 Feb 20, 2013 08:40 PM

Forum Jump

All times are GMT -5. The time now is 05:01 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC