2017-02-27 19 views
0

正如標題所示,我試圖在大型tsv文件中查找所有行,其中至少有50%的列的值大於值x AWK。awk - 列的一半大於x的所有行

E.g對於x = 5:

9 6 7 2  3 
0 1 2 7  6 
1 3 8 9 10 

應該返回

9 6 7 2  3 
1 3 8 9 10 
+1

你試過了什麼?它是如何失敗的? – choroba

+0

我在R中做過,但是文件太大,無法完全讀入內存。所以我想用bash預處理它。 – Flomp

回答

3

awk來救援!

$ awk -v t=5 '{c=0; for(i=1;i<=NF;i++) c+=($i>t)} c/NF>0.5' file 

9 6 7 2  3 
1 3 8 9 10 
+0

非常感謝! 就像預期的那樣工作。 – Flomp

0

使用Perl:

perl -ane '$x = 5; print if @F/2 <= grep $_ > $x, @F' -- file.tsv 
0

使用輸入.tsv文件看起來像這樣:

Num1 Num2 Num3 Num4 Num5 
9  6  7  2  3 
0  1  2  7  6 
1  3  8  9  10 

此代碼將做一個awk腳本。我已經留下了意見,看到 腳本的形式,所以你可以相應地調整。

#!/usr/bin/awk -f 

# reads from stdin. 
# Usage: $ ./bigcols.awk < input1.tsv 


# Run at start. 
BEGIN { 
#  print "Start" 
#  print "TSV setting. Field seperator set to tab." 
     FS = "\t" 
     # He wants to find lines with avg greater than var x 
     x=5 
} 

# main. Run for each record. This code uses newlines to denote records. 
{ 
     # Find lines which are of this form: (skip header) 
     # #+, 
     # ie. start with one or more numbers in column 1. 
     if ($1 ~ /^[0-9]+/) { 
       the_avg = ($1 + $2 + $3 + $4 + $5)/5 
       if (the_avg > x) { 
        print $1, $2, $3, $4, $5 
       } 
     } 
} 

# run at end 
#END { print "Stop" }