tags:

views:

238

answers:

8

I have a file containing the following content 1000 line in the following format:

abc def ghi gkl

How can I write a Perl script to print only the first and the third fields?

abc ghi
+1  A: 

As perl one-liner:

perl -ane 'print "@F[0,2]\n"' file

Or as executable script:

#!/usr/bin/perl

use strict;
use warnings;

open my $fh, '<', 'file' or die "Can't open file: $!\n";

while (<$fh>) {
    my @fields = split;
    print "@fields[0,2]\n";
}

Execute the script like this:

perl script.pl

or

chmod 755 script.pl  
./script.pl
eugene y
+3  A: 
    perl -lane 'print "@F[0,2]"' < file
mobrule
Using Perl in awk-killer mode - with autosplit.
Jonathan Leffler
+8  A: 
while ( <> ) {
    my @fields = split;
    print "@fields[0,2]\n";
}

and just for variety, on Windows:

C:\Temp> perl -pale "$_=qq{@F[0,2]}"

and on Unix

$ perl -pale '$_="@F[0,2]"'
Sinan Ünür
+1  A: 
while(<>){
  chomp;
  @s = split ;
  print "$s[0] $s[2]\n";
}

please start to go through the documentation as well

ghostdog74
http://perldoc.perl.org/functions/split.html : A split on `/\s+/` is like a `split(' ')` except that any leading whitespace produces a null first field. A split with no arguments really does a `split(' ', $_)` internally.
Sinan Ünür
+11  A: 

That's really kind of a waste for something as powerful as perl, since you can do the same thing in one trivial line of awk.

awk '{ print $1 $3 }'

T.E.D.
That's a waste of time to learn `awk` when you know Perl.
codeholic
@codeholic, if OP *knew* Perl, they wouldn't be asking the question. Using Perl for this is like using a thermonuclear warhead to try and kill a fly. +1 for this answer.
paxdiablo
Doesn't say the platform, but it's easier to get perl than awk for Windows. Also, this could be part of a larger programming task ... I'm sure awk is up to it, but sometimes the job wants what it wants.
Zac Thompson
@Zac: They both require a download and install. awk is part of mingw; download URL is http://sourceforge.net/projects/mingw/files/ . I don't do development work on any Windows PC without downloading mingw onto it. Perl OTOH, I only install when I need it
T.E.D.
+5  A: 

If no answer is good for you yet, I'll try to get the bounty ;-)

#!/usr/bin/perl
# Lines beginning with a hash (#) denote optional comments,
# except the first line, which is required,
# see http://en.wikipedia.org/wiki/Shebang_(Unix)

use strict;   # http://perldoc.perl.org/strict.html 
use warnings; # http://perldoc.perl.org/warnings.html

# http://perldoc.perl.org/perlsyn.html#Compound-Statements
# http://perldoc.perl.org/functions/defined.html
# http://perldoc.perl.org/functions/my.html
# http://perldoc.perl.org/perldata.html
# http://perldoc.perl.org/perlop.html#I%2fO-Operators
while (defined(my $line = <>)) {
    # http://perldoc.perl.org/functions/split.html
    my @chunks = split ' ', $line;
    # http://perldoc.perl.org/functions/print.html
    # http://perldoc.perl.org/perlop.html#Quote-Like-Operators
    print "$chunks[0] $chunks[2]\n";
}

To run this script, given that its name is script.pl, invoke it as

perl script.pl FILE

where FILE is the file that you want to parse. See also http://perldoc.perl.org/perlrun.html. Good luck! ;-)

codeholic
+2  A: 

I'm sure I shouldn't get the bounty since the question asks for the result to be given in perl, but anyway:

In bash/ksh/ash/etc:

cut -d " " -f 1,3 "file"

In Windows/DOS:

for /f "tokens=1-4 delims= " %i in (file) do (echo %i %k)

Advantages: like others said, no need to learn Pearl, Awk, nothing, just knowing some tools. The result of both calls can be saved to the disk by using the ">" and the ">>" operator.

Bruno Brant
+1 for cut. Though cut takes an optional file argument, so I'd probably bypass the cat and the pipe.
Hobo
@Hobo: true, I will correct that. Thanks for the tip.
Bruno Brant
A: 
#!/usr/bin/env perl
open my$F, "<", "file" or die;
print join(" ",(split)[0,2])."\n" while(<$F>);
close $F
3ED