0

As a part of my bash routine, I am using the following gawk code integrated to bash function which loop the CSVs and do their analysis:

test_ranking44 () {
    for csv in "${rescore}"/*str*.csv; do
gawk 'BEGIN { FS=", *"; OFS=", " }
NR > 1 {
    cnts[1][$1]++
    cnts[2][$2]++
}
END {
    numRows = 5
    numCols = 2

    PROCINFO["sorted_in"] = "@val_num_desc"
    for (colNr=1; colNr<=numCols; colNr++) {
        rowNr = 0
        for (key in cnts[colNr]) {
            vals[++rowNr][colNr] = sprintf("%s (number of cases: %d)", key, cnts[colNr][key])
        }
    }
    
    print FILENAME, NR
    print "TOP PROT", "TOP LIG"
    for (rowNr=1; rowNr<=numRows; rowNr++) {
        for (colNr=1; colNr<=numCols; colNr++) {
            printf "%s%s", vals[rowNr][colNr], (colNr<numCols ? OFS : ORS)
        }
    }
}' ${csv} >> "${rescore}"/test_ranking.log
done
}

Briefly, the GAWK part calculates the occurrence of the elements in a given column (ignoring the first line, which is a header part) of each CSV (contained str glob pattern in its name), via focusing on the indices in the first as well as the second columns. Eventually it prints top five indexes from the both columns to the test_ranking.log contained info for all processed CSVs at once. Is it possible to modify directly the GAWK code in order to consider only the first N (says first 100 lines) of each csv (now it treats all lines) ? Or alternatively should I pipe it to head:

for csv in "${rescore}"/*str*.csv; do
head -n 101 ${csv} | gawk 'my_code' >> "${rescore}"/test_ranking.log 
done

Any further suggestions?

2
  • 1
    You could either pipe head -n 100 csv | gawk .... or add a check in the gawk to exit if NR reaches 100.
    – Fanatique
    Jun 4, 2021 at 10:53
  • may you demonstrate an example for the second one (but I need to be sure that it consider the lines from the begining still ignorring the header like in my example!)
    – Hot JAMS
    Jun 4, 2021 at 11:35

2 Answers 2

0

You can do

awk 'NR > 1 && NR < 101' {Your Code}
0

Consider changing the following part of the code, as this:

...
NR > 1 && NR < 101 { # Change this condition
    cnts[1][$1]++
    cnts[2][$2]++
}

NR == 100 { exit } # And add this exit statement

END {
...

However, that exit statement will prevent the line print FILENAME, NR from displaying the actual number of lines of FILENAME; NR would return 100 instead. Just remove that line if you want it the other way.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .