Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

having issues using awk and/or converting to perl

18 views
Skip to first unread message

jthru...@gmail.com

unread,
Jan 9, 2006, 9:21:07 AM1/9/06
to
Hi, I am not a strong awk user and have little to no experience with
perl.

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

Paul Lalli

unread,
Jan 9, 2006, 9:47:27 AM1/9/06
to
jthru...@gmail.com wrote:
> Hi, I am not a strong awk user and have little to no experience with
> perl.
>
> 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`;

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

Xicheng

unread,
Jan 9, 2006, 9:52:47 AM1/9/06
to
jthru...@gmail.com wrote:
> 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.
If this is what you want, just forget about using awk within perl
scripts, the perl onelier is easy as awk:

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

jthrumston

unread,
Jan 9, 2006, 9:53:43 AM1/9/06
to
I tried this as you suggested 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.

Paul Lalli

unread,
Jan 9, 2006, 9:56:17 AM1/9/06
to
jthrumston wrote:
> I tried this as you suggested

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

jthrumston

unread,
Jan 9, 2006, 9:58:25 AM1/9/06
to
I tried your first line perl -nle 'print if /^F/' infile > outfile

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] ?

jthrumston

unread,
Jan 9, 2006, 10:00:18 AM1/9/06
to
ok, so I tried that and it didn't work either. How would I incorporate
that into a script. This is only 1 part of what my script is going to
do, I actually have 11 files to do repeat this action with.

Paul Lalli

unread,
Jan 9, 2006, 10:03:04 AM1/9/06
to

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

jthrumston

unread,
Jan 9, 2006, 10:03:37 AM1/9/06
to
I apologize for not showing more detail in my post. I will remember to
quote more in the future

jthrumston

unread,
Jan 9, 2006, 10:07:28 AM1/9/06
to
This is also my first time using the boards. 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.
Thanks for the heads up Paul.

Paul Lalli

unread,
Jan 9, 2006, 10:12:19 AM1/9/06
to
jthrumston wrote:
> This is also my first time using the boards.

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

jthrumston

unread,
Jan 9, 2006, 10:15:02 AM1/9/06
to
I got the point already.

Paul Lalli

unread,
Jan 9, 2006, 10:22:25 AM1/9/06
to
jthrumston wrote:
> I got the point already.

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

Matt Garrish

unread,
Jan 9, 2006, 10:11:51 AM1/9/06
to

"jthrumston" <jthru...@gmail.com> wrote in message
news:1136819248.8...@g44g2000cwa.googlegroups.com...

> This is also my first time using the boards. 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.
>

Usenet is not a "board": http://www.faqs.org/faqs/usenet/what-is/part1/

Matt


jthrumston

unread,
Jan 9, 2006, 10:27:07 AM1/9/06
to
I choose not to quote your continually going on about my not quoting
you. I was looking to make sure my new post contained the information
you requested but yet again, you choose to go on about my lack of
proper posting.
Thanks for the help you did provide and the links to how to post Paul.
I am very sorry this got you upset of whatever it is your experiencing.
I might suggest trying decaf. Maybe one day when I have more experience
I will remember your harsh tone and try not to make a first time poster
feel like you have succeeded in making me feel. I came here for a
little help. You decided to turn this into another matter altogether.
Feel better?

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.

jthrumston

unread,
Jan 9, 2006, 10:47:15 AM1/9/06
to
Xicheng wrote:
> jthru...@gmail.com wrote:
> > 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.
> If this is what you want, just forget about using awk within perl
> scripts, the perl onelier is easy as awk:
>
> 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'...

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).

Matt Garrish

unread,
Jan 9, 2006, 10:39:45 AM1/9/06
to

"jthrumston" <jthru...@gmail.com> wrote in message
news:1136820427....@g47g2000cwa.googlegroups.com...

>
> 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 luck. I doubt you'll get any more help here after this display.

Matt


jthrumston

unread,
Jan 9, 2006, 11:01:32 AM1/9/06
to
This is also my first time using these boards. I am not familiar with
the posting guidelines, but rest assured I will read them over. Again,
my apologies for not following protocol.

Tad McClellan

unread,
Jan 9, 2006, 11:14:47 AM1/9/06
to
jthrumston <jthru...@gmail.com> wrote:

> 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

Tad McClellan

unread,
Jan 9, 2006, 11:18:28 AM1/9/06
to
jthru...@gmail.com <jthru...@gmail.com> wrote:

> 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

Josef Moellers

unread,
Jan 9, 2006, 11:31:04 AM1/9/06
to
jthrumston wrote:
> I got the point already.

No, you haven't.

--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

Glenn Jackman

unread,
Jan 9, 2006, 12:01:41 PM1/9/06
to

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

Dr.Ruud

unread,
Jan 9, 2006, 11:41:18 AM1/9/06
to
jthrumston:
> Xicheng:
>> jthru...@gmail.com:

>>> 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."

jthrumston

unread,
Jan 9, 2006, 1:38:30 PM1/9/06
to
This thread went way out of whack. I apologize to all who were offended
by my lack of proper posting. I was only looking for some help and
wasn't aware of the rules (my fault).
To Paul, I am sorry for sounding off on you, I was taken quite by
surprise by my perception of an attack and I responded poorly.

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.

0 new messages