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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
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…
Six Sigma Control Plans

695 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