-1

EXAMPLE CSV

I need the script to go through each column and check for blanks and group it up for me into 1 column... so e.g. 1st row: banana , 2nd row: rachel, 5th row: rachel, monica, chandler, joey, janice, banana

  • 2
    Hello and welcome to UL SE. Can you please provide us an example CSV file, illustrating your problem, and the specific output you would like to get in that example. – Bananguin Aug 7 '18 at 8:54
  • I've attached a paint file of what the CSV file looks like and the bottom one is how I want the result to look like...sorry if i'm being unclear - i'm very new to this!! – infiniteoscillation Aug 7 '18 at 9:05
  • 1
    :D we need to see an example for the actual CSV file, the text file that the program should read in order to generate the result you want. but keep the paint-ing it does help describe your problem – Bananguin Aug 7 '18 at 9:08
  • I won't be able to share that information as it is private to the company. I am an intern here and they've handed me a bash project - got no experience so I'm feeling a bit lost haha .. this is for an ITRS dashboard btw – infiniteoscillation Aug 7 '18 at 9:43
  • 1
    :D we need to see an example for the actual CSV file, the text file that the program should read in order to generate the result you want. But meanwhile you have to possible answers. You should try them out. – Bananguin Aug 7 '18 at 12:19
1

See how far this gets you

awk -F\; '
    {TMP = ""
     for (i=1; i<=NF; i++) if ($i == "") TMP = sprintf ("%s,%c", TMP, 64+i)
     print substr (TMP,2)
    }
' /test1.csv
B,F,H
A,G,H

A,C,F

Adapt the field separator if need be. Will fail beyond Column 26.

Adapted to the new requirements

    awk -F\; '
    NR == 1 {MX = split ($0, HDR)
             next
            }
            {TMP = ""
             for (i=1; i<=MX; i++) if ($i == "") TMP = sprintf ("%s,%s", TMP, HDR[i])
             print substr (TMP,2)
            }
    ' file
banana
rachel
banana
rachel,monica,chandler,joey,janice,banana
rachel,monica,chandler,joey,banana
0

I think you are looking for something along the lines of

awk '
BEGIN { FS=";" }
NR==1 {
        for(i = 1; i <= NF; i++) { heads[i]=$i; }
      }

      { for(i = 1; i <= NF; i++) {
          if ($i == "") { printf "%s ",heads[i] }
        }
        print "";
      } 
'

This should shred the fields of the first row into an array heads. For every row that is not the first row, awk iterates over the columns and prints the name of the column, if the field is empty. Didn't have time to test though. May contain bugs. YMMV

  • You might want to check for the banana fields. – RudiC Aug 7 '18 at 13:12
  • @RudiC I don't let anybody near my banana fields :D – Bananguin Aug 7 '18 at 16:11
0

Try this one too:

BEGIN {FS=OFS=","}
{
    if (NR == 1)
        split($0, hdr)

        if (nr) {nr = 0; print("")}
        for(f=1; f<=NF; f++)
            if ($f == "") {
                if (!nr)
                    printf("%s", hdr[f])
                else
                    printf("%c%s", OFS, hdr[f])
                nr = 1
            }
}

Well, here is a special BANANA column implementation:

BEGIN {FS = OFS = ","}

NR == 1 {
        printf("%s%c%s\n", $0, OFS, "BANANA")
        split($0, hdr)
        next
}

{
        for(f = 1; f <= NF; f++)
            if ($f == "") {
                printf("%s%c%s\n", $0, OFS, hdr[f])
                break
            }
}
  • 1
    You might want to check for the banana fields. – RudiC Aug 7 '18 at 13:12
  • @RudiC Ahaha, you did everything superperfect already to finish his homework :) – Mikhail Zakharov Aug 7 '18 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.