2

I want to read the file (1600 rows) and get rows only the columns have different values (sno1, sno2, sno3 & sno4-should be not be equal value) and it should be above 50%.The example of output given below

input.txt (tab-delimited)

id sno1 sno2 sno3 sno4
R1 98.4 88.8 98.4 67.6
R2 100  100  100  100
R3 33.4 23.5 98.8 45.5
R4 53.5 78.7 88.8 67.5
R5 0    0    0    0
R6 88.8 98.8 67.6 100

ouput.txt

R4 53.5 78.7 88.8 67.5
R6 88.8 98.8 67.6 100

Here in R4 & R6 rows- all column values have not equal to each other and all are above 50%. Any help in awk/sed/perl is appreciated.

3

A perl-oneliner:

perl -nae 'undef %saw ; next if $. == 1; shift @F; next if grep { $_ < 50 or $saw{$_}++ } @F;  print ' input.txt

This basically translates to:

#!/usr/bin/env perl
use strict;

while (<>) {
  my @F = split(' ');  # split the current line
  my %seen;
  next if $. == 1;  # skip the heading
  shift @F;  # ignore first element

  next if grep { $_ < 50 or $seen{$_}++ } @F;  # ignore lines with
                                               # duplicate entries and
                                               # entries less than 50
  print;  # print current line
}
2

This might work for you (GNU sed):

sed '1d;/\S\+ \(\S\+\) .* \1/d;/\S\+ \S\+ \(\S\+\) .* \1/d;/\S\+ \S\+ \S\+ \(\S\+\) .* \1/d;/\S\+\( \(100\|[56789][0-9]\..\)\)\{4\}/!d' file
  • wow impressive, care to explain? – Ulrich Dangel Aug 7 '12 at 20:04
-1

We can compare each field with other fields in the row for nonequality and can also compare each field is greater than 50 value

awk '($2!=$3) &&($2!=$4) && ($2!=$5) && ($3!=$4) &&($3!=$5) &&($4!=$5) && ($2>50) && ($3>50) && ($4>50) && ($5>50) {print $0}' input.txt 

PS: I assumed the number of columns are fixed.

  • This answer would be improved with some explanation of what your code block does and how. – Michael Homer Mar 9 at 1:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.