Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

Need help from PERL programmer for simple perl program

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?


2 Solutions
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";
while (<INPUT>) {
  my $string = (split(/\|/)[2];
  chomp(my $output = `./program $string`);
  print OUTPUT "$string | $output\n";
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now