CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to chamge awk to CGI Perl syntax correctly

 



digioleg54
User

Jun 5, 2017, 2:28 PM

Post #1 of 7 (1879 views)
How to chamge awk to CGI Perl syntax correctly Can't Post

In my old Perl package, which worked on UNIX AIX I have this lines:

Code
my $if = $data_path."/DTEDMG.FWMARGPOS.EXCEL_IN"; 
my ($i_encd,$i_trdr,$i_secid,$i_secnm,$i_market_price,$i_last_prcdt)=(10,12,18,20,23,28);
my $cmd = ("awk -F'|' '{print \$($i_secid+1) \"\|\" \$($i_trdr+1) \"\|\" \$($i_encd+1) \"\|\" \$($i_market_price+1) \"\|\" \$($i_last_prcdt+1) \"\|\" \$($i_secnm+1)}' $if|sort -u");

print STDOUT "creating if_trade_price from $if\n$cmd";


It gives this output

Code
awk -F'|' '{print $(18+1) "|" $(12+1) "|" $(10+1) "|" $(23+1) "|" $(28+1) "|" $(20+1)}' /appdata/dw18637/cgi-data/DTEDMG.FWMARGPOS.EXCEL_IN|sort -u

how can I change it to GNU, which will run on apache?

Thanks


digioleg54
User

Jun 7, 2017, 9:54 AM

Post #2 of 7 (1865 views)
Re: [digioleg54] How to chamge awk to CGI Perl syntax correctly [In reply to] Can't Post

People, can I have just an advise about this? I understand, that there is a file with many lines. I need to know how to split certain line of the file in the loop


Laurent_R
Veteran / Moderator

Jun 7, 2017, 10:35 AM

Post #3 of 7 (1864 views)
Re: [digioleg54] How to chamge awk to CGI Perl syntax correctly [In reply to] Can't Post

My first comment would be to say that it's probably a bad idea to call an awk script from a Perl program. There is essentially nothing from awk which can't be done in Perl.

Next, not seeing the data on which this script is running, it is quite difficult to understand what it does or why.

Finally, I have told you several times already, Perl is neither running on Apache, nor running on GNU, Perl is running on an operating system, presumably some distribution of Linux in your case.

Because of your poor description of what you are trying to do or what your problem is, I am not able to figure out what you need, and I can't really help.


digioleg54
User

Jun 7, 2017, 11:18 AM

Post #4 of 7 (1860 views)
Re: [Laurent_R] How to chamge awk to CGI Perl syntax correctly [In reply to] Can't Post

I already said that it is old program and I have to find the solution how to change it to work as CGI, that is why I ask for help. It runs on Linux. I don't want to use awk, that is why I asked you to help me to change it to normal Perl command


BillKSmith
Veteran

Jun 7, 2017, 12:15 PM

Post #5 of 7 (1857 views)
Re: [digioleg54] How to chamge awk to CGI Perl syntax correctly [In reply to] Can't Post

I think that this is about what you need.

Code
use strict; 
use warnings;

sub field_shifter {
my $infile = shift;
my @fields = @_;
my @records;

open my $handle, '<', $infile or die;
while (<$handle>) {
push @records, join( '|', (split /\|/ )[@fields]);
}
close $handle;
return sort @records;
}

# Give it a try!

my $i_Secid = 18;
my $i_trdr = 12;
my $i_encd = 10;
my $i_market_price = 23;
my $i_last_prcde = 28;
my $i_secnm = 20;

my @records = field_shifter(
#$data_path."/DTEDMG.FWMARGPOS.EXCEL_IN",
'awktst.dat', # my test file
$i_Secid+1,
$i_trdr+1,
$i_encd+1,
$i_market_price+1,
$i_last_prcde+1,
$i_secnm+1,
);
$" = "\n";
print "@records\n";

Good Luck,
Bill


digioleg54
User

Jun 9, 2017, 7:02 AM

Post #6 of 7 (1832 views)
Re: [BillKSmith] How to chamge awk to CGI Perl syntax correctly [In reply to] Can't Post

I am getting messages:

Code
Use of uninitialized value in join or string at tests1.pl line 52, <$handle> line 1,2....1785

this line

Code
 push @records, join( '|', (split /\|/ )[@fields]);

I am trying to print

Code
foreach my $rec (@records) 
{
print "LINE $rec\n";
}


but get only

Code
LINE ||||| LINE ||||| LINE ||||| LINE ||||| LINE ||||| LINE ||||| LINE ||||| LINE ||||| LINE ||||| LINE ||||| LINE ||||| LINE ||||| LINE ||||| LINE ||||| LINE ||||| LINE ||||| LINE |||||

Seems like something is going wrong


Thanks


digioleg54
User

Jun 9, 2017, 8:23 AM

Post #7 of 7 (1828 views)
Re: [BillKSmith] How to chamge awk to CGI Perl syntax correctly [In reply to] Can't Post

It works great

Thank you

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives