having issues using awk and/or converting to perl


J

jthrumston

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 isawk: 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
 
Ad

Advertisements

P

Paul Lalli

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
 
X

Xicheng

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
 
J

jthrumston

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

Paul Lalli

jthrumston said:
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
 
J

jthrumston

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

Advertisements

J

jthrumston

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

Paul Lalli

jthrumston said:
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.

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
 
J

jthrumston

I apologize for not showing more detail in my post. I will remember to
quote more in the future
 
J

jthrumston

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

Advertisements

P

Paul Lalli

jthrumston said:
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
 
P

Paul Lalli

jthrumston said:
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
 
J

jthrumston

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

Matt Garrish

jthrumston said:
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
 
Ad

Advertisements

J

jthrumston

Xicheng said:
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).
 
J

jthrumston

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

Advertisements

T

Tad McClellan

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
 

Top