PDA

View Full Version : Perl Command Line Question




fivetoadsloth
Jan 26, 2012, 05:42 PM
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:

./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.



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
Jan 26, 2012, 05:49 PM
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:
for file in *.txt; do echo -in $file -out ${file%.*} ; done

B

jiminaus
Jan 26, 2012, 05:49 PM
You want to use the printf command to format the number.

for($f=1;$f<=88;f++){
my $s = printf('%02d', $f)
`./program -in file_$s.txt -out file_$s`
}

fivetoadsloth
Jan 26, 2012, 06:52 PM
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:
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.

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 ")

balamw
Jan 26, 2012, 06:56 PM
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

mobilehaathi
Jan 26, 2012, 06:57 PM
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?

balamw
Jan 26, 2012, 07:00 PM
semi-colons?

That too.

B

jiminaus
Jan 26, 2012, 07:10 PM
semi-colons?

Oops. :o And a missing $.


for($f=1;$f<=88;$f++){
my $s = printf('%02d', $f);
system('./program', '-in', 'file_$s.txt', '-out', 'file_$s');
}

fivetoadsloth
Jan 26, 2012, 07:11 PM
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:


#!/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
Jan 26, 2012, 07:12 PM
Still need a semi-colon!

Forgive me, I'm still new to this. I have the following:


#!/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
Jan 26, 2012, 07:14 PM
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

mobilehaathi
Jan 26, 2012, 07:15 PM
You also need $f++ instead of f++. ;)

B

ooh, good catch. I didn't even read the error message!

dmi
Jan 27, 2012, 01:24 AM
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:

./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.



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!

for $f ('001'..'088'){
`./program -in file_$f.txt -out file_$f`
}