Referring to this question : Finding missing value in text file

I have 2 files containing below data


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



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

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

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

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

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      

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

All comments inside the text

awk '
  # make array with FEATURE elements from file "feature"
  # 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
        # 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 
            # construct apropriate row
            if(c in r)
              # if row exist add value to it
              r[c]=r[c] " " n
              # if not exist put apropriate spaces before value
              r[c]=s n
            # find maximum row number between all FEATUREs
        # 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.