Improve company productivity with a Business Account.Sign Up

x
  • 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|string2|string3|string4”
“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?


Thanks,

John
0
kekokadi
Asked:
kekokadi
2 Solutions
 
TintinCommented:
Here's a shell and perl solution

#!/bin/sh
i=1

while read line
do
  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

#!/usr/bin/perl
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";
  $i++;
}
0
 
ozoCommented:
while (<INPUT>) {
  my $string = (split(/\|/)[2];
  chomp(my $output = `./program $string`);
  print OUTPUT "$string | $output\n";
}
0
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