2

Referring to this question : Finding missing value in text file

I have 2 files containing below data

example

Name             Feature
Marry            Lecturer
Marry            Student
Marry            Leader
Bob              Lecturer
Bob              Student
Som              Student

feature

 Lecturer
 Student
 Leader 

I'm following below code to find missing feature against any name in example file :

#!/bin/bash
rm -f *.missing names.all
feature=feature
sed -n '1!p' example.txt | cut -d ' ' -f 1 | sort -u > names.all
for i in $(cat $feature)
do
  fgrep $i example.txt | cut -d ' ' -f 1 | cat - names.all | sort | uniq -u >  $i.missing 
done 

This code gives me 3 different files, like lecturer.missing, student.missing and leader.missing and contains all the name which doesn't have this feature.

but I want the data to be in same file and output should be :

I need ouput like this :

Lecturer   Student   Leader
  Som                 bob
                      Som

I tried using appending the data in same file, but it doesn't work.

  • 2
    Please include everything needed to answer in your question. You can mention previous questions as background but each question should be able to stand alone. – terdon Nov 12 '14 at 16:47
  • Thanks for the edit, much better. Could you also clarify whether the header is part of the file? I mean, does the file actually contain Name Feature? Also, please mention your OS in the question, not in a tag (and Solaris is not Linux by the way, why did you tag with both?). – terdon Nov 12 '14 at 17:05
2

This code

awk '
  NR == FNR {feature[$1]=1; next} 
  $1 != "Name" {name[$1]=1; role[$1,$2]=1} 
  END {
    for (f in feature)
      printf "%-12s", f
    print ""
    for (n in name) { 
      for (f in feature) 
        printf "%-12s", (n SUBSEP f in role ? " " : n)
      print ""
    }
  }
' features roles 

gives this output

Lecturer    Student     Leader      

                        Bob         
Som                     Som         

close enough?

  • this is giving syntx error , m working on version SunOS adbuas03 5.10 Generic_150401-10 i86pc i386 i86pc– – user21546 Dec 30 '14 at 8:56
  • Try with nawk or /usr/xpg4/bin/awk – glenn jackman Dec 30 '14 at 12:04
0

All comments inside the text

awk '
  # make array with FEATURE elements from file "feature"
  FNR==NR{f[$1]=1;next}
  # collect to array all FEATUREs NAME by NAME
  FNR>1{e[$1]=e[$1]" "$2}
  # loop for each element in FEATURE array
  END{for (i in f) {
        # produce a head row with FEATURE elements
        r[0]=r[0] i" "
        # starts row counts for each FEATURE elements
        c=0
        # call all NAMEs 
        for (n in e)
          # check if the FEATURE do not exist for the NAME  
          if(e[n] !~ i){
            # produce next row number 
            ++c
            # construct apropriate row
            if(c in r)
              # if row exist add value to it
              r[c]=r[c] " " n
            else
              # if not exist put apropriate spaces before value
              r[c]=s n
            # find maximum row number between all FEATUREs
            if(c>l)
              l=c
          }
        # make shift in row for next FEATURE  
        s=s" "
      }
      # prints row by row
      for (k=0;k<=l;k++)
        print r[k]
  }' feature example | column -tn
  • this is giving syntx error , m working on version : SunOS adbuas03 5.10 Generic_150401-10 i86pc i386 i86pc– – user21546 Dec 30 '14 at 8:56

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.