On 9/1/11 Thu  Sep 1, 2011  4:38 PM, "Rajeev Prasad" <rp.ne...@yahoo.com>
scribbled:

> from linux:
>  
> cut -f1,5- -d" " file |grep -v "^0" | sort -n > to_file;   <======this line:
> how to achieve this in perl?
>  
>  
> will below work,  in perl?
>  
> if ( ! -s $sourcedir/somefile ){
> 
>  open(tmpFH,"<","file2");
>  @tmpAR = <tmpFH>;
>  close(tmpFH);
> 
>  push(@tmpAR2,$tmpAR[0],$tmpAR[5..]);        #select on columns 1 and 5

The array @tmpAR contains the lines of the original file, one line per
element. You need to select part of each line. The above line selects whole
lines (lines 0, 5, 6, 7, ...).

If the columns in your original file are separated by whitespace or other
characters that do not appear in the fields, you can use the split function:

  my @fields = split(' ',$tmpAR[0]);
  my $newline = join(' ',@tmpAR[0,5..$#fields]);
  push(@tmpAR2,$newline);

>  my @tmpAR3 = grep {!^[0 ]} @tmpAR2;         #omit line with 0 in the
> beginning

I would try

  my @tmpAR3 = grep { $_ != /^0/ } @tmpAR2;


> 
>  @tmpAR3 = sort(@tmpAR3);                        # sort on the first column

If you want a numerical sort, you will have to use an explicit sort
comparator:

  my @tmpAR4 = sort { $a <=> $b } @tmpAR3;

>  }
> 
> we can then print tmpAR3 to a file.

If you need more help, please consider:

  1. Including a complete, short, runnable program that demonstrates the
problem you are having, and that include data using the <DATA> file handle.

  2. Some indication of what you expect this program to do and why it does
not meet your expectations.



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to