Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > having issues using awk and/or converting to perl

Reply
Thread Tools

having issues using awk and/or converting to perl

 
 
jthrumston@gmail.com
Guest
Posts: n/a
 
      01-09-2006
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

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      01-09-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
Xicheng
Guest
Posts: n/a
 
      01-09-2006
(E-Mail Removed) 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

 
Reply With Quote
 
jthrumston
Guest
Posts: n/a
 
      01-09-2006
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.

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      01-09-2006
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

 
Reply With Quote
 
jthrumston
Guest
Posts: n/a
 
      01-09-2006
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] ?

 
Reply With Quote
 
jthrumston
Guest
Posts: n/a
 
      01-09-2006
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.

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      01-09-2006
jthrumston wrote:
> 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

 
Reply With Quote
 
jthrumston
Guest
Posts: n/a
 
      01-09-2006
I apologize for not showing more detail in my post. I will remember to
quote more in the future

 
Reply With Quote
 
jthrumston
Guest
Posts: n/a
 
      01-09-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting a sed / grep / awk / . . . bash pipe line into python hofer Python 11 09-03-2008 09:56 PM
awk's NF in Perl Thorsten Gottschalk Perl 1 03-05-2004 08:04 PM
using perl on the command line, like sed or awk gorda Perl Misc 3 10-21-2003 01:50 PM
using perl on the command line, like sed or awk gorda Perl 2 10-21-2003 06:38 AM
sed awk or perl for this? NNTP Perl 13 09-30-2003 06:49 AM



Advertisments