Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Extracting selected column names and values from tab delimited text file

by Ratna_Ranjan (Novice)
on Oct 21, 2009 at 21:26 UTC ( [id://802539]=perlquestion: print w/replies, xml ) Need Help??

Ratna_Ranjan has asked for the wisdom of the Perl Monks concerning the following question:

I have a tab delimited text file which is of format
colname1 colname2 colname3 val1 val2 val3 val11 val21 val31 val12 val22 val32
I have an array which has a list of columnnames such as @colname={colname1,colname3}; Now I want the colnames and values from the file which matches the names in the array. So that my final output is of the format
colname1 colname3 val1 val3 val11 val31 val12 val32
Any suggestion on how to go about achieving this?

Replies are listed 'Best First'.
Re: Extracting selected column names and values from tab delimited text file
by Fletch (Bishop) on Oct 21, 2009 at 21:41 UTC
    require 'rubygems' require 'ruport' puts Ruport::Data::Table.load( ARGV.shift, :csv_options => { :col_sep +=> "\t" } ).sub_table( ARGV ).as( :csv )

    Of course if you maybe showed what you've tried so far you might get some actual help with Perl code instead.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Extracting selected column names and values from tab delimited text file
by moritz (Cardinal) on Oct 21, 2009 at 22:03 UTC
    Here's a working Perl solution:
    use v6; my $data = q[colname1 colname2 colname3 val1 val2 val3 val11 val21 val31 val12 val22 val32]; my @lines = $data.split: "\n"; my @rows; my @header = @lines.shift.words; for @lines -> $l { my %h = @header Z $l.words; @rows.push: {%h}; } say @rows.perl; my @colname = <colname1 colname3>; say @colname.join("\t"); say $_{@colname}.join("\t") for @rows;

    Though I admit that the output could be formatted a bit prettier, and some parts of the code are ugly hacks.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: Extracting selected column names and values from tab delimited text file
by GrandFather (Saint) on Oct 21, 2009 at 22:14 UTC

    You may find Text::CSV_XS is helpful.


    True laziness is hard work
Re: Extracting selected column names and values from tab delimited text file
by jakobi (Pilgrim) on Oct 21, 2009 at 21:43 UTC

    This reads like the infamous make-my-homework request. You might have more success when also providing a short version of the Perl code you're currently using (that survives perl -c, which @colname={colname1,colname3} probably won't (excluding subs)).

    Darn it, should have checked once more before pressing create; such a tempting opener, and I was too slow...

Re: Extracting selected column names and values from tab delimited text file
by Bloodnok (Vicar) on Oct 21, 2009 at 23:01 UTC
    Yep, use Text::xSV - using an appropriate field delimiter setting in the constructor.

    A user level that continues to overstate my experience :-))
Re: Extracting selected column names and values from tab delimited text file
by Marshall (Canon) on Oct 22, 2009 at 00:27 UTC
    Here is another solution variant for you. The problem is easier if you are working with column numbers instead of names for the selection criteria. But I did that conversion for you. Column print order is same order as names in @desired_cols.

    Code winds up with an ordered list of column positions to print and then does that for the data in the file by using list slice.

    #!/usr/bin/perl -w use strict; my @desired_cols = qw(colname1 colname3); #order matters here # reads first line to get actual column names my $header_line = (<DATA>); my @actual_cols = split(/\s+/,$header_line); # get column number of the actual column names my $pos =0; my %col2_num = map {$_ => $pos++}@actual_cols; # translate the desired col names into position numbers my @slice = map{$col2_num{$_}}@desired_cols; print join("\t",@desired_cols),"\n"; #header line while (<DATA>) { my @row = (split)[@slice]; print join("\t",@row),"\n"; #each data row } =pod Prints: colname1 colname3 val1 val3 val11 val31 val12 val32 =cut __DATA__ colname1 colname2 colname3 val1 val2 val3 val11 val21 val31 val12 val22 val32
Re: Extracting selected column names and values from tab delimited text file
by planetscape (Chancellor) on Oct 22, 2009 at 05:29 UTC
Re: Extracting selected column names and values from tab delimited text file
by bichonfrise74 (Vicar) on Oct 21, 2009 at 23:18 UTC
    Here's something to get you started. Hopefully you can find this helpful.
    #!/usr/bin/perl use strict; my @required_cols = qw( colname1 colname3 colname4 ); my %record; while (<DATA>) { my (@temp_data) = split( /\s+/ ); push( @{ $record{$_} }, $temp_data[$_] ) for (0 .. $#temp_data); } my (%final_data, $count); for my $i ( keys %record ) { $count = 0; for my $j ( @{ $record{$i} } ) { push( @{ $final_data{$count} }, $j ) if ( grep /\b$record{$i}->[0]\b/, @required_cols ); $count++; } } print "@{ $final_data{$_} }\n" for ( sort keys %final_data ); __DATA__ colname1 colname2 colname3 val1 val2 val3 val11 val21 val31 val12 val22 val32

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://802539]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-04-23 12:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found