extract column data from file using perl

Perl programming forum.

extract column data from file using perl

Postby kamil on January 31st, 2008, 8:14 pm

I need to extract the 3rd column from a text file, sort it, uniq it and write it to another file... I have to do it in perl... but for illustration, here is a solution using awk, sort and uniq:
Code: Select all
awk '{if($3~/[a-z]/){print $3}}' filein.txt | sort | uniq > fileout.txt

an example input file fragment, ~phones.mlf:
Code: Select all
...
31025000 31438750 vcl
31438750 31641250 b
31641250 32191250 ix
32191250 33081250 cl
33081250 34275000 t
34275000 35450000 h#
.
"/data/db/timit_full/timit/train/dr8/mtcs0/sx82.lab"
0 2468750 h#
2468750 2768750 g
2768750 3525000 uh
3525000 3918750 vcl
3918750 4090625 d
...

corresponding output file fragment, ~phones.txt:
Code: Select all
aa
ae
ah
ao
aw
ax
axh
ay
b
cl
d
dh
dx
eh
en
epi
...
User avatar
kamil
Linux Adept
 
Posts: 118
Joined: January 18th, 2008, 1:22 am
Location: Brisbane, Australia

Re: extract column data from file using perl

Postby kamil on February 1st, 2008, 3:19 pm

first attempt:
Code: Select all
open FILE, '< ~phones.mlf' or die "Can't read ~phones.mlf: $!";
my(@phones);
while (<FILE>) {
    my($t_start, $t_end, $phone) = split(/ /);
    if(defined $phone and $phone=~/[a-z]+\#*/) {
        push(@phones, $phone);
    }
}
close FILE;

undef my(%seen);
@phones = sort(grep(!$seen{$_}++, @phones));

open FILE, '> ~phones.txt' or die "Can't write ~phones.txt: $!";
print FILE @phones;
close FILE;
User avatar
kamil
Linux Adept
 
Posts: 118
Joined: January 18th, 2008, 1:22 am
Location: Brisbane, Australia


Return to Perl

Who is online

Users browsing this forum: No registered users and 0 guests

cron

dsplabs homelinux bloglinux forums new! travel photographyawklores new! cryptographyjames' home
©2009 dsplabs.com.au