I was reading a thread about how to use awk in a perl script (cause I
haven't figured out how to do the same in perl)
Here is my awk command
awk '$1 ~ /^F/ {print $0 }' /tmp/frhtest/ADDC.bak >
/tmp/frhtest/ADDC.$date
this works fine at the command line. I tried to do this to run inside
my perl script.
$call = `awk '$1 ~ /^F/ {print $0 }' /tmp/frhtest/ADDC.bak >
/tmp/frhtest/ADDC.$date`;
system("$call");
and I get this result
syntax error The source line is 1.
The error context is
>>> ~ <<<
awk: Quitting
The source line is 1.
it generates a zero record file.
Basically all I am doing is pulling all records out of a file that
start with "F" and creating a new file of just those records.
any suggestions would be greatfully taken.
Thanks
In Perl, back-ticks interpolate variables. Therefore, Perl's $1 and $0
variables are being passed to your awk script, and awk is not being
asked to look at it's own $1 and $0 variables. Backslash each of
those $ characters.
> system("$call");
perldoc -q vars
> and I get this result
> syntax error The source line is 1.
> The error context is
> >>> ~ <<<
> awk: Quitting
> The source line is 1.
> it generates a zero record file.
>
> Basically all I am doing is pulling all records out of a file that
> start with "F" and creating a new file of just those records.
perl -ne'print if /^F/' oldfile.txt > newfile.txt
Paul Lalli
perl -nle 'print if /^F/' infile > outfile
you dont need to separate line into columns except that you need to
check contents in a specific column, like:
perl -anle 'print if $F[2] =~ /^F/' infile > outfile
this check if column-3 begins with 'F'...
your awk command can be written as:
awk '/^F/' infile > outfile
Xicheng
Bareword found where operator expected at ./trial line 6, near "'print
if /^F/' ADDC"
(Missing operator before ADDC?)
syntax error at ./trial line 6, near "-ne"
Execution of ./trial aborted due to compilation errors.
What is "this"? Who is "you"?
Please quote context when posting a reply, as I have done by including
your comments in my reply.
> and got the following error.
>
> Bareword found where operator expected at ./trial line 6, near "'print
> if /^F/' ADDC"
> (Missing operator before ADDC?)
> syntax error at ./trial line 6, near "-ne"
> Execution of ./trial aborted due to compilation errors.
The code I posted was a "one-liner", just as your original awk script
was. It is not meant to be run inside of any other source code. Run
that code directly at the command line.
Paul Lalli
and got this
String found where operator expected at ./trial line 6, at end of line
(Missing semicolon on previous line?)
Can't find string terminator "'" anywhere before EOF at ./trial line 6.
on the second one, does that mean column 1 would be $F[0] ?
I have asked you once already to please quote context when posting a
reply. Your decision not to do this is not making me overly anxious to
help you.
Please go read the posting guidelines for this group. They are posted
here twice a week. Take note of, in particular, effective follow-up
style, and the request to post a short-but-complete script that
demonstrates your error. "it didn't work" does not help in the least.
Once you have composed a message that conforms to these Posting
Guidelines, you are far more likely to receive help in figuring out
your problem.
Paul Lalli
You are not posting to "boards". You are posting to Usenet. The
difference is significant.
> I did not read the
> procedure as I should have. I will read them now to make sure my posts
> are more informative and comply with standards. Again I apologize for
> not being more clear.
Courtesy of "Dr. Ruud":
For jthrumston and everybody else with "User-Agent: G2/#.#":
"How can I automatically quote the previous message
when I post a reply?"
http://groups.google.co.uk/support/bin/answer.py?answer=14213
See also:
http://www.safalra.com/special/googlegroupsreply/
What's good 'netiquette' when posting to Usenet?
http://groups.google.co.uk/support/bin/answer.py?answer=12348
http://directory.google.com/Top/Computers/Usenet/Etiquette/
Paul Lalli
No, clearly, you haven't. Because after 5 posts, you are still not
quoting any context in your replies. I have now reached my level of
tolerance.
Good luck, and good bye.
Paul Lalli
Usenet is not a "board": http://www.faqs.org/faqs/usenet/what-is/part1/
Matt
For those who are reading this. I apologize. I will try and get more
precise information into my postings. I still need to resolve the perl
issue. Hopefully without being sidetracked.
Good day to you sir.
This did exactly what I need (by changing the column to 0) from the
command line. I thank you Xicheng for suggesting that. My question now
would be, how do I incorporate that line inside a perl script? I have
11 files to do this exact operation on (though the column is not always
the same from report to report).
Good luck. I doubt you'll get any more help here after this display.
Matt
> I choose not to quote
I choose to ignore all of your future posts.
[snip whining]
> Feel better?
Do you?
--
Tad McClellan SGML consulting
ta...@augustmail.com Perl programming
Fort Worth, Texas
> I was reading a thread about how to use awk in a perl script (cause I
> haven't figured out how to do the same in perl)
>
> Here is my awk command
>
> awk '$1 ~ /^F/ {print $0 }' /tmp/frhtest/ADDC.bak >
The "a2p" (awk-to-perl) translator is part of the standard
perl distribution. You can use it to see how to do awk stuff
using perl instead:
echo '$1 ~ /^F/ {print $0 }' | a2p
No, you haven't.
--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett
You could do something like this (untested), which will do a
line-oriented regexp search over a set of files:
my %search_conditions = (
file1 => qr{^F},
file2 => qr{suffix$},
...
);
foreach my $filename (keys %search_conditions) {
open my $f, '<', $filename or die "can't open '$filename': $!\n";
while (<$f>) {
# if match, output the filename, the line number and the line
print "$filename:$.:$_" if /$search_conditions{$filename}/;
}
close $f;
}
--
Glenn Jackman
Ulterior Designer
>>> Basically all I am doing is pulling all records out of a file that
>>> start with "F" and creating a new file of just those records.
>>
>> you dont need to separate line into columns except that you need to
>> check contents in a specific column, like:
>>
>> perl -anle 'print if $F[2] =~ /^F/' infile > outfile
>>
>> this check if column-3 begins with 'F'...
>
> This did exactly what I need (by changing the column to 0) from the
> command line [...]
> how do I incorporate that line inside a perl script? I have
> 11 files to do this exact operation on (though the column is not
> always the same from report to report).
Now it is time for you to show us some code.
First check `perldoc perlrun` for what the "-anle" means.
A lean version without columns:
#!/usr/bin/perl
use warnings;
use strict;
while (<>) {
print if /^F/
}
--
Affijn, Ruud
"Gewoon is een tijger."
I will not be posting here again, at least until I figure out how to do
it without fearing a flame response.
I do realize this is not a "board" I said the wrong thing.
I have found a solution to my original question. Thank you very much to
those who helped out.
Have a good day.