grep: \" and " both Give Errors

W

William

In my perl script, I currently have:

#!/usr/bin/perl -w

use strict;

my $InputFileDir = "/mkapp/webapps/mxrt-cgi/upload_vol/upload_files/";
my $savedfilename = "trs_vol.txt.49.22.224.163.VERGAJ2.20050215.10.21.21";
my $filename = "trs_vol.txt";

my $originalList = $InputFileDir . $savedfilename;
my $originalListBack = $InputFileDir . $filename;

system "sed -e 's/^M//' $originalList | grep \"USD FNM\" > $originalListBack";


where $originalList is a file that contains:
|USD CHL|USD NYSE|Securities|1d|0|ATM|16.3194164633751|16.3194164633751|
|USD CHL|USD NYSE|Securities|3d|0|ATM|16.3215860724449|16.3215860724449|
|USD CHL|USD NYSE|Securities|1w|0|ATM|16.3243487477303|16.3243487477303|
|USD CHL|USD NYSE|Securities|2w|0|ATM|16.3260638713837|16.3260638713837|
|USD CHL|USD NYSE|Securities|3w|0|ATM|16.3272082805634|16.3272082805634|
|USD CHL|USD NYSE|Securities|1m|0|ATM|16.3357391953468|16.3357391953468|
|USD CHL|USD NYSE|Securities|3m|0|ATM|17.0192211866379|17.0192211866379|
|USD CHL|USD NYSE|Securities|4m|0|ATM|17.4577638506889|17.4577638506889|
|USD CHL|USD NYSE|Securities|6m|0|ATM|17.9224327206612|17.9224327206612|
|USD CHL|USD NYSE|Securities|9m|0|ATM|18.4148281812668|18.4148281812668|

$originalListBack is an (uncreated) file named trs_vol.txt

My problem: $originalListBack is not being created by this line:
system "sed -e 's/^M//' $originalList | grep \"USD FNM\" > $originalListBack";


I tried the following on the command line:
sed -e 's/^M//' trs_vol.txt.49.22.224.132.LEUNGW5.20051227.16.36.13 | grep
\"USD FNM\" > trs_vol.txt


but I got the following error message:
grep: can't open FNM"
Broken Pipe


However, if I try the following line on the command line, I got a 0 KB
trs_vol.txt file, which is what I desire.

sed -e 's/^M//' trs_vol.txt.49.22.224.132.LEUNGW5.20051227.16.36.13 | grep
"USD FNM" > trs_vol.txt

But if I remove the \ from the system "sed ..." line, then my Perl script
does not compile.

My question: what is the correct way to obtain a 0 KB trs_vol.txt file
using Perl (not command line)?
 
L

Larry

William said:
In my perl script, I currently have:

#!/usr/bin/perl -w

use strict;

my $InputFileDir = "/mkapp/webapps/mxrt-cgi/upload_vol/upload_files/";
my $savedfilename = "trs_vol.txt.49.22.224.163.VERGAJ2.20050215.10.21.21";
my $filename = "trs_vol.txt";

my $originalList = $InputFileDir . $savedfilename;
my $originalListBack = $InputFileDir . $filename;

system "sed -e 's/^M//' $originalList | grep \"USD FNM\" > $originalListBack";


where $originalList is a file that contains:
|USD CHL|USD NYSE|Securities|1d|0|ATM|16.3194164633751|16.3194164633751|
|USD CHL|USD NYSE|Securities|3d|0|ATM|16.3215860724449|16.3215860724449|
|USD CHL|USD NYSE|Securities|1w|0|ATM|16.3243487477303|16.3243487477303|
|USD CHL|USD NYSE|Securities|2w|0|ATM|16.3260638713837|16.3260638713837|
|USD CHL|USD NYSE|Securities|3w|0|ATM|16.3272082805634|16.3272082805634|
|USD CHL|USD NYSE|Securities|1m|0|ATM|16.3357391953468|16.3357391953468|
|USD CHL|USD NYSE|Securities|3m|0|ATM|17.0192211866379|17.0192211866379|
|USD CHL|USD NYSE|Securities|4m|0|ATM|17.4577638506889|17.4577638506889|
|USD CHL|USD NYSE|Securities|6m|0|ATM|17.9224327206612|17.9224327206612|
|USD CHL|USD NYSE|Securities|9m|0|ATM|18.4148281812668|18.4148281812668|

$originalListBack is an (uncreated) file named trs_vol.txt

My problem: $originalListBack is not being created by this line:
system "sed -e 's/^M//' $originalList | grep \"USD FNM\" > $originalListBack";


I tried the following on the command line:
sed -e 's/^M//' trs_vol.txt.49.22.224.132.LEUNGW5.20051227.16.36.13 | grep
\"USD FNM\" > trs_vol.txt


but I got the following error message:
grep: can't open FNM"
Broken Pipe


However, if I try the following line on the command line, I got a 0 KB
trs_vol.txt file, which is what I desire.

sed -e 's/^M//' trs_vol.txt.49.22.224.132.LEUNGW5.20051227.16.36.13 | grep
"USD FNM" > trs_vol.txt

But if I remove the \ from the system "sed ..." line, then my Perl script
does not compile.

My question: what is the correct way to obtain a 0 KB trs_vol.txt file
using Perl (not command line)?

That is pretty curious. The only explanation I can think of is that
the "system" command is using a different shell than the one you are
using interactively and that's why it's acting differently?

But why are you using double-quotes in the "grep" call anyway? You are
using single quotes for "sed"... just use single quotes for "grep" too.
The double-quotes are just making it more complicated.

But the best solution would be to avoid doing a "system" call at all.
You can do what you want entirely in Perl:

open $inF, $originalList;
open $outF, ">$originalListBack";
while (<$inF>) {
s/\r//;
print $outF if /USD FNM/;
}
 
L

Larry

However, if I try the following line on the command line, I got a 0 KB
trs_vol.txt file, which is what I desire.
sed -e 's/^M//' trs_vol.txt.49.22.224.132.LEUNGW5.20051227.16.36.13 | grep
"USD FNM" > trs_vol.txt
But if I remove the \ from the system "sed ..." line, then my Perl script
does not compile.

Oops... I was misintreting the sed input before. This is the corrected
Perl equiv:

open $inF, $originalList;
open $outF, ">$originalListBack";
while (<$inF>) {
s/^M//;
print $outF if /USD FNM/;
}
 
B

Big and Blue

William said:
In my perl script, I currently have:

#!/usr/bin/perl -w

use strict;

my $InputFileDir = "/mkapp/webapps/mxrt-cgi/upload_vol/upload_files/";
my $savedfilename = "trs_vol.txt.49.22.224.163.VERGAJ2.20050215.10.21.21";
my $filename = "trs_vol.txt";

my $originalList = $InputFileDir . $savedfilename;
my $originalListBack = $InputFileDir . $filename;

system "sed -e 's/^M//' $originalList | grep \"USD FNM\" > $originalListBack";

Why have you written this in Perl, then used sed and grep as external
commands to do all of the work?

If you want to use sed and grep then just use a shell script around them.

If you want to use Perl then do the sed and grep work in Perl - it's
very good at it! It also means you don't have quoting problems in system()
calls.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top