perl - Want to Extract the first word in each line and save them in an output file -


abc1 17898 8779  abc1 68579 7879 abc2 78794 8989 abc2 97857 9897 abc3 79850 9905  . . .  abc120  84889 9897   abc121  87898 7879 abc121  87898 7879  abc121  87898 7879  abc122  87898 7879   abc122  87898 7879 

need help.. want words starting abc each line , save them
in output file.

#!/usr/bin/env perl  use warnings; open (tran_file, "in.txt" ); open (out, " > out.txt"); $count = 0;   while ($line = <tran_file>)  { chomp ($line);  if ($line =~ m/^abc\d*/)     ### matching word abc.     {            if ($line =~ /\s(\d*)\s+\s(\d+\.\d+)\s+\s(\d+\.\d+)\s(.+)/)           ### trying divide content in line , extract            word in lines ie., abc1, abc2, ...                  {                 print out " $1 \n";                    ### print in output file             }      }   }   close (tran_file); 

         open (tf, "in.txt" );           open (out, " > out.txt");          while ($line = <tf>)          {            @names = split / / , $line;              $out = $names[0];              print  out " $out \n ";         }       close(tf); 

added bit corrected code:

use strict; use warnings; use autodie;  open $ifh, '<', 'in.txt'; open $ofh, '>', 'out.txt'; while( $line = <$ifh> ) {         @fields = split / /, $line;         print $ofh $fields[0], "\n"; } close $ifh; close $ofh; 

Comments