2016-05-16 1 views
0

基本的に私はそれが何もないように見えるので、空白行の列が続くに貼り付ける最初の3つの列を持って取得していたファイルはcolumn4コマンド置換は、

Iに追加取得されますそれは、入力ファイルが非常に大きい場合に役立ちます(約私はおそらく私がコマンド置換で作成した変数を使用すべきではないように感じるが、私は私がそうでなければ

#!/bin/sh # the first file in the expression of a bunch of patients to be made into data files that can be put into the graph 
awk '{print "hs"$1,"\t",$2,"\t",$3}' $1 > temp1.txt  #important columns saved 
numLines=`wc -l $1`  
touch column4.txt  #creates a column for the average of column 6- 
for ((s=0;s<$numlines;s++)); do     
     currentRow=0       #Will eventually be the average of column 6- for the row of focus 
     for ((i=6;i<=106;i++)); do    
       addition=`cut -f $i $1 | head -n $s | tail -n 1`  # cuts out the number at the row and column of focus for this loop 
       currentRow=`expr $currentRow + $addition`    # adding the newly extracted number to the total 
     done 
     currentRow=`expr $currentRow/101`       #divides so the number is an average instead of a really big number 
     echo $currentRow >> column4.txt         #appends this current row into a text file that can be pasted onto the first three columns 
done 
paste temp1.txt column4.txt 
rm temp1.txt column4.txt 

を必要とするこれらの番号にアクセスする方法がわかりませんよ106列、および数万行)が表示されますが、これはその外観の例です

Important identifier line grant regis 76 83 02 38 0 38 29 38 48 (..up to to 106 columns) 
another important identifier bill susan 98 389 20 29 38 20 94 29 0 (.. same point) 

そして出力は(私たちは後に列を除外すると仮定...)のようになります

Important identifier line 34.88 
another important identifier 79.67 

申し訳ありませんが何かは不明であるならば、何かがあなたがありますかどうか尋ねるだけで、それを明確にするために全力を試してみました「に疑問を抱いて再と私は救助に

お礼

+2

割り当てでは、左側から '$ 'を削除します。 – choroba

+0

もちろん、ありがとう、それは問題を解決しませんでしたので、私はこの質問で他の何かを把握しようとしているので質問のうちそれらを編集しましたが、あなたの助けに感謝します! – Jacob

+1

'numlines'は' numLines'と同じではありません。 – choroba

答えて

0

awkを編集したり、コメントします!あなたが偶数のため、この

$ awk '{for(i=6;i<=NF;i++) a[i-5]=$i; 
     asort(a); 
     mid=(NF-4)/2; print mid, a[mid]}' file 

5 38 
5 29 

を行うことができ、中央値(フィールドの奇数)用のサンプル入力

$ awk '{for(i=6;i<=NF;i++) sum+=$i; 
     printf "%s %s %s %.2f\n", $1,$2,$3, sum/(NF-5); 
     sum=0}' file 

Important identifier line 39.11 
another important identifier 79.67 

の値を使用して、このスクリプトですべてを置き換えることができます

、一般的なアプローチは隣接数の平均をとっています(距離でも加重平均できます)。

+0

ありがとうございました@karakfa!私はawkがこれを行うことができるのか分からず、awkについてもう少し学ぶのに良いところを知っていますか?私はクラスの1つで数時間それを学んだが、私はその能力の詳細を学びたいと思うように感じる – Jacob

+0

また!これで中央値を取得する方法はありますか? – Jacob

0

次を使用しようとすることができます:

perl -MList::Util=sum -lanE '@n=grep{/^\d+$/}@F; say "@F[0..4] ",sum(@n)/@n' 

プリント:精密

perl -MList::Util=sum -lanE '@n=grep{/^\d+$/}@F; printf "@F[0..4] %.2f\n",sum(@n)/@n' 

Important identifier line grant regis 39.11 
another important identifier bill susan 79.67 

Important identifier line grant regis 39.1111111111111 
another important identifier bill susan 79.6666666666667 

かについて上記の内のすべての数値の平均値を算出し、この線。正確6-は、例えば、使用することができるため:

perl -MList::Util=sum -lanE 'say "@F[0..4] ",sum(@F[[email protected]])/(@F-6)' 

両方平均メジアン(奇数または要素もNUM)

を印刷する

Important identifier line grant regis 39.1111111111111 
another important identifier bill susan 79.6666666666667 

を印刷します

perl -MList::Util=sum -lanE ' 
    @s = sort { $a <=> $b } @F[[email protected]]; 
    $m = int(@s/2); 
    printf "@F[0..4] %.2f %d\n", 
    sum(@s)/(@s-1), 
    (@s % 2) ? @s[$m] : sum(@s[$m-1,$m])/2 
' filename 

プリント:

Important identifier line grant regis 39.11 38 
another important identifier bill susan 79.67 29 

最後に、素晴らしい変数を含むperlスクリプトとして同じです。

use strict; 
use warnings; 
use List::Util qw(sum); 

while(<>) { 
    chomp; 
    my(@text) = split; 
    my(@sorted_numbers) = sort { $a <=> $b } grep { /^\d+$/ } splice @text, 5; 

    my $average = sum(@sorted_numbers)/@sorted_numbers; 

    my $median; 
    my $mid = int(@sorted_numbers/2); 

    if(@sorted_numbers % 2) { 
     $median = $sorted_numbers[$mid]; 
    } else { 
     $median = sum(@sorted_numbers[$mid-1,$mid])/2; 
    } 
    printf "@text %.2f %d\n", $average, $median; 
} 
関連する問題