Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7189687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:21:18+00:00 2026-05-28T19:21:18+00:00

I have an awk script that processes a csv file and produces a report

  • 0

I have an awk script that processes a csv file and produces a report that counts the number of rows for each column, named in the header field, that contain data /[A-Za-z0-9]/. What I would like to do is enhance the script and print the top 5 most duplicated data elements in each column.

Here is sample data:

Food|Type|Spicy
Broccoli|Vegetable|No
Lettuce|Vegetable|No
Spinach|Vegetable|No
Habanero|Vegetable|Yes
Swiss Cheese|Dairy|No
Milk|Dairy|No
Yogurt|Dairy|No
Orange Juice|Fruit|No
Papaya|Fruit|No
Watermelon|Fruit|No
Coconut|Fruit|No
Cheeseburger|Meat|No
Gorgonzola|Dairy|No
Salmon|Fish|
Apple|Fruit|No
Orange|Fruit|No
Bagel|Bread|No
Chicken|Meat|No
Chicken Wings|Meat|Yes
Pizza||No

This is the current script that SiegeX has substantially contributed:

$ cat matrix2.awk 
NR==1{
  for(i=1;i<=NF;i++)
    head[i]=$i
  next
}
{
  for(i=1;i<=NF;i++)
  {
    if($i && !arr[i,$i]++)
      n[i]++
    if(arr[i,$i] > 1)
      f[i]=1
  }
}
END{
  for(i=1;i<=length(head);i++) {
    printf("%-6d%s\n",n[i],head[i])
    if(f[i]) {
      for(x in arr) {
        split(x,b,SUBSEP)
        if(b[1]==i && b[2])
          printf("% -6d %s\n",arr[i,b[2]],b[2])
      }
    }
  }
}

This is the current output:

$ awk -F "|" -f matrix2.awk testlist.csv 
20    Food
6     Type
 6     Fruit
 4     Vegetable
 3     Meat
 1     Fish
 4     Dairy
 1     Bread
2     Spicy
 17    No
 2     Yes

And this is the desired output:

$ awk -F "|" -f matrix2.awk testlist.csv 
20    Food
6     Type
 6     Fruit
 4     Vegetable
 4     Dairy
 3     Meat
 1     Fish
2     Spicy
 17    No
 2     Yes

The only thing left that I would like to add is a general function that limits each columns output to the top 5 most duplicated fields. As mentioned below, a columnar version of sort |uniq -c |sort -nr |head -5.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:21:19+00:00Added an answer on May 28, 2026 at 7:21 pm

    The following script is both extensible and scalable as it will work with an arbitrary number of columns. Nothing is hardcoded

    awk -F'|' '
    NR==1{
      for(i=1;i<=NF;i++)
        head[i]=$i
      next
    }
    {
      for(i=1;i<=NF;i++)
      {
        if($i && !arr[i,$i]++)
          n[i]++
        if(arr[i,$i] > 1)
          f[i]=1
      }
    }
    END{
      for(i=1;i<=length(head);i++) {
        printf("%-32s%d\n",head[i],n[i])
        if(f[i]) {
          for(x in arr) {
            split(x,b,SUBSEP)
            if(b[1]==i && b[2])
              printf("    %-28s%d\n",b[2],arr[i,b[2]])
          }
        }
      }
    }' infile
    

    Output

    $ ./report
    Food                            9
    Type                            5
        Meat                        2
        Bread                       1
        Vegetable                   2
        Fruit                       2
        Fish                        1
    Spicy                           2
        Yes                         2
        No                          6
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small awk script that does some in-place file modifications (to a
Have been trying to write an awk script which processes a log file, but
I have an awk script that I have defined thus: #!/usr/bin/env awk BEGIN {
I have a shell script that constructs an awk program as a string then
I have the following code that uses 'paste' and AWK script inside Perl. use
Suppose you have a file that contains IP addresses, one address in each line:
I have a script that uses strace, cp, awk, and stat, to create a
I have a script that greps and awks and counts for ip addresses with
I have a small bash script that greps/awk paragraph by using a keyword. But
I have a script file that uses import and load commands to bring in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.