Skip to content

Instantly share code, notes, and snippets.

@nilforooshan
Created March 11, 2024 03:09
Show Gist options
  • Save nilforooshan/abcfc5e2279723be2ea117e7267ecb06 to your computer and use it in GitHub Desktop.
Save nilforooshan/abcfc5e2279723be2ea117e7267ecb06 to your computer and use it in GitHub Desktop.
awk: Iterate over columns of a file and count a pattern in each column

Iterate over columns of a file and count a pattern in each column

Count -9999 occurrences in each column of file.csv.

awk -F',' '{
    for (i = 1; i <= NF; i++) {
        if ($i == -9999) {
            count[i]++
        }
    }
    total_lines++
} END {
    for (i = 1; i <= NF; i++) {
        print "Column " i ": " count[i] " occurrences out of " total_lines " lines"
    }
}' file.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment