Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pull only rows where field 2 is 'WI' in tab-delimited file

Status
Not open for further replies.

polka4ever

Technical User
Jan 25, 2006
42
0
0
US
hello! I have a story problem for you. I have a tab-delimited file. The 2nd field is the "state" field, and I want to pull out of this file, and write to a NEW file, only the rows that have Wisconsin as their state. 'WI' I believe I need to use "split" but not sure how. This is how far I've gotten. I'm horrible at Perl.

#!/usr/bin/perl
open (FILE, "<lotsofstates.data");
while (<FILE>) {
 
I bet it is very similar to what was posted for kre1973 ... except that I need the "WI" or "OR" or "NE" or whatever state I need to occur in field 2 - instead of, say, the last name field or something.
 
You could do it a couple of different ways, but I'd probably go with the split as you suggested and check the value of the second element.
Code:
#Assign the filename to a variable
my $filename = 'lotsofstates.data';
my @fields;

#Assign the state to look for
my $state_to_find='WI';

#Open the file or print an error message on failure
open FILE, "< $filename" or die "$filename : $!\n";

#Assign the lines of the file to an array
my @lines = <FILE>; 

close FILE;

#Loop through the lines
foreach $this_line (@lines){
    
    #Split the line on commas
    @fields = split (',', $this_line);
    
    #Check the value of the 2nd field (array elements start at 0)
    if ($fields[1] eq $state_to_find){
        #code that does stuff;
    }
}
Note: This is assuming the data does not contain any newline/CRLF chracters or commas in the data. Those cases will require special handling.

_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
Since you have a tab limited file, change line
Code:
@fields = split (',', $this_line);
to
Code:
@fields = split ('\t', $this_line);
given by RottPaws

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Alternative crude solution. has the benefit of allowing you to specify the state to select on the command line, and search a whole list of files if you need to. Just pipe the output to a file of your choice...

Code:
use strict;
use warnings;
my $state = uc(shift);

while (<>) {
    print if (/\t$state\t/);
}

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Good catch, spookie. I deal with comma delimited files so much I missed that....

But you'll need to use double-quotes instead of single quotes. As in:

Code:
@fields = split ("\t", $this_line);

_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
RottPaws
It works with ' as well.


--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
But you'll need to use double-quotes instead of single quotes. As in:

Not in the split function. The first argument of split() is a regexp pattern, so the single-quotes are just replacing the forward slash as the delimiters of the regexp. Now if it were the join() function, double-quotes would be necessary as the first argument to join is a string, not a regexp pattern.
 
I didn't know that. Thanks.[peace]

_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top