i have file column-separated values, first row shown here columns titles next rows real data. real set of columns considerably longer in example, btw, why automatize job need do.
main-cat id affil perm ff plan aca yes edu yes no no aca yes edu no yes no aca yes edu no no yes
what need extract, every row, columns value other "no". want such column printed on format:
column title=row value
example: 3 examples rows above should printed as:
main-cat=aca id=yes affil=edu perm=yes main-cat=aca id=yes affil=edu ff=yes main-cat=aca id=yes affil=edu plan=yes
my best suggestion endless list of conditions (there many columns), saying if column1 not "no", print "main-cat="+row-value, if column 2 not not "no", print "id="+row-value. surely there must more efficient way achieve this? use (g)awk and/or shell script. grateful suggestions.
i'd say
awk 'nr == 1 { split($0, colnames); next } { sep = ""; for(i = 1; <= nf; ++i) if($i != "no") { printf("%s%s=%s", sep, colnames[i], $i); sep = ofs } print "" }' filename
that is
nr == 1 { # in first line split($0, colnames) # remember column names next # nothing else } { # in other lines: sep = "" # reset separator token for(i = 1; <= nf; ++i) { # wade through fields if($i != "no") { # aren't "no" printf("%s%s=%s", sep, colnames[i], $i) # print them remem- # bered column name sep = ofs # set sep ofs here # fields have # separator in front, starting # second } } print "" # when done, add newline. }