Need help from PERL programmer for simple perl program

Posted on 2004-09-07
Last Modified: 2010-03-05
Hello there,

  I am not a Perl programmer, and would love to get assistance for a Perl script that does the following:

1.      open a file named “input.txt” in the current directory
The file is a text file containing lines such as follow:
“String1.1|String2.1|string3.1| string4”

2.  The perl script will execute a C++ program named “program” which takes as an argument arg[1]   , the third string , in this case, eg. “string3”  .
The program will cout a string in the standard out.

  Executing “ ./program  string3    “ will produce an output in the standard out , e.g  “output1” .

3.      the perl script will then produce an output file “output.txt” in the current directory, containing results of the execution,  the format as follow:
“string3 |  output1”
“string3.1| output2”
“string3.2| output3”  and so on…

The script can be Perl or Shell script… can anyone help me out please?


Question by:kekokadi
LVL 48

Accepted Solution

Tintin earned 25 total points
ID: 12002536
Here's a shell and perl solution


while read line
  string=`echo "$line"|cut -d'|' -f3`
   ./program $string >output$i
   echo "$string | output$i" >>output.txt
   i=`expr $i + 1`
done <input.txt

If you have ksh/bash, the above script can be made a bit more efficient.

A corresponding Perl script would be

use strict;

open INPUT, "input.txt" or die "Can not open input.txt $!\n";
open OUTPUT,">output.txt" or die "Can not open output.txt $!\n";

my $i=1;

while (<INPUT>) {
  my $string = (split(/\|/)[2];
  system "./program $string >output$i";
  print OUTPUT "$string | output$i\n";
LVL 84

Assisted Solution

ozo earned 25 total points
ID: 12003526
while (<INPUT>) {
  my $string = (split(/\|/)[2];
  chomp(my $output = `./program $string`);
  print OUTPUT "$string | $output\n";

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sending email via Perl on Windows 3 162
Need Helping Editing Perl Script for PowerChute Network Shutdown 3 82
PERL - Find newest folder 12 133
Perl tar error 8 64
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In a recent question ( here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question