Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Newbie wants a simple script in perl

0 views
Skip to first unread message

2007

unread,
Apr 5, 2008, 3:41:33 PM4/5/08
to
Hi

I have a file called "list1" - its got about 80 rows - each row is
alpha numeric name w/ ".txt" extension - each name refers to a file in
the dir.

The script reads the list - for each name in the list, it will perform
an operation (an executable of a "C-program" and writes to same name
with a different extension, say ".mp3".

Thanks

Rui Maciel

unread,
Apr 5, 2008, 4:09:10 PM4/5/08
to

Nice. What have you written so far? And why didn't you opted for bash?


Rui Maciel

2007

unread,
Apr 5, 2008, 6:22:40 PM4/5/08
to

I changed it to bash but the code didn't work

THis is what I really need:
In a summary:
1) say about 80 files (but varies) in a dir with filenameX.txt
extension.

2) CONV1 runs one at a time and gives output w/ a fixed name, CONV.out

3) CONV2 takes CONV.out as well as the final output file (which is
filenameX.mp3). Ideally it puts them in a specified directly.

I really appreciate your help - I want something that works - I
started off with t shell - I am open to change to bash or perl. I
prefer a simpler looking program

Jürgen Exner

unread,
Apr 5, 2008, 6:24:02 PM4/5/08
to

Ok.
What is your question (if you got any) or the point of your posting?

jue

2007

unread,
Apr 5, 2008, 6:47:52 PM4/5/08
to

I need help in writing please

Also, if I use "print" within perl to the dump a executable command to
the monitor, will the process be executed and the perl will wait
before it throws the next one to screen?

Andrew DeFaria

unread,
Apr 5, 2008, 6:48:16 PM4/5/08
to
Based on your sketchy requirements I'd say (in bash):
$ cat list1 | while read filename; do
>   CONV1 $filename
>   CONV2 CONV.out
> done
Am I missing something?
--
Andrew DeFaria
There's only two things that money can't buy and that's true love and home grown tomatoes. - Guy Clark

Rui Maciel

unread,
Apr 5, 2008, 7:52:47 PM4/5/08
to
On Sat, 05 Apr 2008 15:48:16 -0700, Andrew DeFaria wrote:

> Based on your sketchy requirements I'd say (in bash):
>
> $ cat list1 | while read filename; do
> > CONV1 $filename
> > CONV2 CONV.out
> > done
>
> Am I missing something?

Yes, he was trying to get someone to do his work. He obviously never even
gave it a try, not in perl and obviously not in bash. After all, it's one
of the recurring examples in each and every bash tutorial on the web.


Rui Maciel

A. Sinan Unur

unread,
Apr 5, 2008, 9:09:15 PM4/5/08
to
2007 <FW3...@sbcglobal.net> wrote in
news:720e7bce-09bc-4870-8877-
607b3e...@p39g2000prm.googlegroups.co
m:

> On Apr 5, 3:24 pm, Jürgen Exner <jurge...@hotmail.com> wrote:

...

>> What is your question (if you got any) or the point of your
>> posting?

...


>
> I need help in writing please
>

First, try to help yourself.

If you want the script written for you, I am sure there will be a
few people who'd do it for $500. If you want tech support to go with
that, maybe $750. If you would like me to write it, the rate goes up
to $2500.

If you are willing to pay for the job, then post an announcement at
http://jobs.perl.org/

Otherwise, try to solve your problem first on your own (make sure to
consult the various resources available to you) and then post
concrete questions here.

Sinan

--
A. Sinan Unur <1u...@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/

Jürgen Exner

unread,
Apr 5, 2008, 9:36:36 PM4/5/08
to
2007 <FW3...@sbcglobal.net> wrote:
>> >I have a file called "list1" - its got about 80 rows - each row is
>> >alpha numeric name w/ ".txt" extension - each name refers to a file in
>> >the dir.
>>
>> >The script reads the list - for each name in the list, it will perform
>> >an operation (an executable of a "C-program" and writes to same name
>> >with a different extension, say ".mp3".
>>
>I need help in writing please

Ok, what do you have so far? Show us your current code, explain what it
is supposed to do (you got that part already), what it is actually
doing, or where you are stuck. I am sure someone will be happy to show
you how to get unstuck.

>Also, if I use "print" within perl to the dump a executable command to
>the monitor,

This sentence doesn't compute. I have absolutely no idea what you are
talking about.

>will the process be executed and the perl will wait
>before it throws the next one to screen?

print() has nothing to do with process control. Where did you get that
idea?

jue

Andrew DeFaria

unread,
Apr 5, 2008, 10:04:08 PM4/5/08
to
A. Sinan Unur wrote:
First, try to help yourself.

If you want the script written for you, I am sure there will be a few people who'd do it for $500. If you want tech support to go with that, maybe $750. If you would like me to write it, the rate goes up
to $2500.
Ow, ow! I'll do it for $250! ;-)
--
Andrew DeFaria
Marriage changes passion. Suddenly you're in bed with a relative.

Andrew DeFaria

unread,
Apr 5, 2008, 10:14:51 PM4/5/08
to
2007 wrote:
I need help in writing please
As others have already said - show us what you got already!

Also, if I use "print" within perl to the dump a executable command to the monitor, will the process be executed and the perl will wait before it throws the next one to screen?
You can always tell noobies as they seem to invent strange terminologies! Print doesn't "dump" executable commands to monitor. Print prints things. What you tell it to print. It's displayed on stdout, which nowadays is rarely a monitor but a terminal emulator. The process will be executed if and when you tell Perl to execute a process - and not before. Oh, and Perl doesn't throw anything to a screen... not at all!

Perhaps you meant this:

"If I use 'print' to print the executable command to stdout will the process be executed?"

Well print doesn't execute processes. The system Perl function will and/or commands in `` will execute external processes. Printing out the contents of your command before executing it will not cause it not to be executed. Instead do something like this. Let's say you were executing a command stored in $cmd.
my $cmd = "CONV1 $filename";
my @output = `$cmd`;
my $status = $?;
die "Unable to execute $cmd" if $status != 0;

$cmd = "CONV2 $filename2";
@output = `$cmd`;
die "Unable to execute $cmd" if $status != 0;
Change this to:
my $cmd = "CONV1 $filename";
print "About to execute \"$cmd\"\n";
#my @output = `$cmd`;
#my $status = $?;
#die "Unable to execute $cmd" if $status != 0;

print "About to execute \"$cmd\"\n";
#$cmd = "CONV2 $filename2";
#@output = `$cmd`;
#die "Unable to execute $cmd" if $status != 0;
So here I'm just printing out what $cmd contains. Note I commented out the actual execution of the commands on the assumption that you didn't want to actually execute them unless you saw that they looked OK. Actually I would do this in Perl's debugger instead stopping at the statement that was about to execute the $cmd and display it's contents and only stepping forward if it were correct.
--
Andrew DeFaria
Why is the word abbreviation so long?

Johann Kappacher

unread,
Apr 6, 2008, 5:16:56 AM4/6/08
to
Hi,

Rui Macial has already told you: "Do not let others do your homework!"
Try some coding first.
Moreover, many neebies have asked the same question, you will find
helpful replies.

This works at least in ksh/bash: (no shell builtins for var-substitution)

while read file
do
f_short=$(echo $file | cut -d . -f 1) # strip the .txt suffix
C_PROGRAM $file > "${f_short}.mp3"
done < list1

--jk

Tad J McClellan

unread,
Apr 6, 2008, 10:32:44 AM4/6/08
to
2007 <FW3...@sbcglobal.net> wrote:

> Subject: Newbie wants a simple script in perl


Newbie should begin writing a simple script in Perl.

Newbie should post the broken code here when you get stuck.

Then we will help newbie fix the broken code.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Joe Smith

unread,
Apr 7, 2008, 2:34:17 AM4/7/08
to
2007 wrote:

> Also, if I use "print" within perl to the dump a executable command to
> the monitor, will the process be executed and the perl will wait
> before it throws the next one to screen?

1) As written: It simply does not happen that way.

2) I use something like that all the time, by piping the output
from a perl script into 'sh' or 'bash'.

perl generate_commands.pl input.txt | sh

The logic for generate_commands.pl is quite simple.

1) Use <> to read list of names, one line at a time.
2) Parse the input line to get the name of an input data file.
3) Come up with the name of an output file based on that.
4) Execute an external command, giving it the names of the
input data file and output file.
5) Execute a second external command.
6) Loop back to step 1 until end of list.

There are several ways to perform step 4:

my $results = `$command1 $in_file $out_file`;
or
my $error_code = system "$command1 $in_file $out_file";
warn "Something went wrong: $? ($!)" unless $error_code == 0;
or
print PIPE "$command1 $in_file $out_file\n";

The other steps are so trivial that you won't have any problem
finding them in any Perl tutorial.

That should be enough for you to write the script yourself. Good luck.
-Joe

0 new messages