Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

move the line if prefix matching

by hyans.milis (Novice)
on Mar 17, 2013 at 13:41 UTC ( #1023909=perlquestion: print w/ replies, xml ) Need Help??
hyans.milis has asked for the wisdom of the Perl Monks concerning the following question:

hi

actually i would like to move the line into different file if prefix matching 628. could u please advice about the code as the output is not correct. thanks

File Input
621001000286 2/13/2013 18:42 3/15/2013 18:42 627001000298 3/13/2013 13:20 4/12/2013 13:20 628001000796 3/12/2013 19:07 4/11/2013 19:07 628001000823 3/13/2013 13:28 4/12/2013 13:28 629001001592 2/8/2013 15:52 3/10/2013 15:52
Output1
621001000286 2/13/2013 18:42 3/15/2013 18:42 627001000298 3/13/2013 13:20 4/12/2013 13:20 629001001592 2/8/2013 15:52 3/10/2013 15:52
Output 2
628001000796 3/12/2013 19:07 4/11/2013 19:07 628001000823 3/13/2013 13:28 4/12/2013 13:28
#!/usr/bin/perl use strict; use warnings; my $file = $ARGV[0] or die "Need to get CSV file on the command line\n +"; my $outfile = $ARGV[1] or die "Need to get output file on the command +line\n"; my $sum = 0; open(my $data, '<', $file) or die "Could not open '$file' $!\n"; open (OUTFILE, "> $outfile") || die "ERROR: opening $outfile\n"; open (OUTFILE_1, "> 1$outfile") || die "ERROR: opening $outfile\n"; while (my $line = <$data>) { chomp $line; my @fields = split "," , $line, -1;; my $sum = "$fields[0]"; #$fields[1] = "$sum"; if ($sum =~ /^628?$/ ) { #s/^628+//; $line= join( ",", @fields ) . "\n" ; print OUTFILE ("$line"); } else { $line= join( ",", @fields ) . "\n" ; print OUTFILE_1 ("$line"); } } close ($data); close (OUTFILE); close (OUTFILE_1);

Comment on move the line if prefix matching
Select or Download Code
Re: move the line if prefix matching
by poj (Curate) on Mar 17, 2013 at 13:52 UTC
    Try changing the regex line from this
    if ($sum =~ /^628?$/ )
    to
    if ($sum =~ /^628/ )
    poj
      ok, thanks so much. it works now

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2014-07-30 04:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls