eine Schleife in Awk-Befehl

394
Monah

Ich habe eine Datei mit folgenden Eingabedaten:

Sample1  Feature 1 A B C D Feature 2 E F G  Sample2:  Feature 1 H I Feature 2 L O P 

Und ich hätte gerne folgende Ausgabe:

Sample1 Feature 1: 4 Feature 2: 3 Sample2 Feature 1: 2 Feature 2: 3 

Im Grunde versuche ich zu zählen, wie viele Elemente in jedem Feature und für jedes Sample separat vorhanden sind

Ich habe versucht, den folgenden Befehl zu verwenden:

awk ' else } END }' Eingabedatei> Ausgabedatei

Aber im Grunde gab es folgende Ausgabe (es zählte alle Features für alle Samples)

Feature 1: 6 Feature 2: 6 

Kann mir jemand helfen, diesen Befehl zu ändern oder einen anderen vorzuschlagen?

0

1 Antwort auf die Frage

0
glenn jackman

Datei summarize.awk:

function print_feature() { if (feature) print feature ": " n n = 0 feature = "" } NF == 0 { # empty line.  print_feature() # print the feature summary in_feature = 0 # we are no longer counting elements next # do not print the empty line } $1 == "Feature" { # a new feature print_feature() # print the previous feature summary feature = $0 # save this as the new feature in_feature = 1 # indicate we are counting elements next # do not print ... yet } { if (in_feature)  n++ # count this element else # or print # print (e.g. "Sample") } END { print_feature() # if there is no trailing blank line, print the current feature } 

Dann

$ awk -f summarize.awk file Sample1 Feature 1: 4 Feature 2: 3 Sample2: Feature 1: 2 Feature 2: 3