?
Solved

awk in perl

Posted on 2003-03-26
5
Medium Priority
?
1,659 Views
Last Modified: 2008-03-03
system("awk '{print $1 $2 $3}' temp.txt");

I expect only the first 3 columns of the file to get printed. But it prints out all the columns, Why is that?
Thanks
Thomas
0
Comment
Question by:gr8thomas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 60 total points
ID: 8214792
system(q"awk '{print $1 $2 $3}' temp.txt");
0
 
LVL 48

Expert Comment

by:Tintin
ID: 8215015
Why would you even consider forking a process to call awk from Perl, when Perl is a superset of awk?

You can use a2p to convert an awk script to Perl (although the results can be  a little ugly), but for something as basic as what you have, just use:

#!/usr/bin/perl
while (<>) {
   chomp;
   @field = split;
   print "$field[0] $field[1] $field[2]\n";
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 8215052
perl -ane 'print"@F[0..2]\n"'
0
 
LVL 2

Expert Comment

by:EarleAke
ID: 8220705
My guess is by the time is reaching the shell, you do not have anything defined for $1, $2 or $3.  When it hits awk, the command has then become:

system("awk '{print }' temp.txt");

which would print out all the columns of the file.

How about this instead of the system call:

open(FILE, "<temp.txt");
while($line=<FILE>) {
  (@F) = split(" ", $line);
  printf ("%s\n", join(" ", @F[0..2]));
}
close(FILE);
0
 
LVL 48

Expert Comment

by:Tintin
ID: 8242232
If you are forking out to do awk in Perl, you're completely missing the point of using Perl.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question