Match the column heading and print the values of the column using awk

0

I have a data in the file in columns. I want to write a script, which displays the data of the column matching the column heading.

C-1 C-2 C-3 C-4 C-5 C-6 C-7 C-8 C-9
10  30  35  20  9   65  87  30  29
40  32  67  78  30  54  24  21  13
50  43  32  12  43  65  78  67  54

if the user choose to display C-8 column then the out put must be

C-8
30
21
67

I am not sure how to match the column name and print the output using awk.

Thanks, KJ

KumarJohn

Posted 2015-06-17T10:51:19.253

Reputation: 223

Answers

1

Create a script for example called script.sh with this:

awk -v COLT=$1 '
        NR==1 {
                for (i=1; i<=NF; i++) {
                        if ($i==COLT) {
                                title=i;
                                print $i;
                        }
                }
        }
        NR>1 {
                if (i=title) {
                        print $i;
                }
        }
' file

Where file is the file with the data in columns.

If you issue script.sh C-8 then the result will be:

C-8
30
21
67

jcbermu

Posted 2015-06-17T10:51:19.253

Reputation: 15 868

Thanks for the solution, it works perfectly. Can you please explain what "column=${column:(-1)}" is working. – KumarJohn – 2015-06-17T11:15:57.813

Takes the last character of variable column – jcbermu – 2015-06-17T11:18:28.320

I made a change to use the *title column` instead of a number – jcbermu – 2015-06-17T11:19:03.020

This works fine if the column numbers are matching what if the column numbers does not match and column name are some text like name, Rollno, this script will not work in that case. – KumarJohn – 2015-06-17T11:26:38.523

If there is not maching, it shows nothing. If text on column is like Rollno for example, change variable to_remove on the script. – jcbermu – 2015-06-17T11:37:15.013

This is not working, I am getting complete table as the output. – KumarJohn – 2015-06-17T11:43:58.420

Let us continue this discussion in chat.

– jcbermu – 2015-06-17T11:46:02.767

I am trying to enter the chat but its taking time – KumarJohn – 2015-06-17T11:53:01.170

@KumarJohn Now you can use any name you want. – jcbermu – 2015-06-17T13:32:35.583

1

Without awk, you can use the following command:

cut $FILE -f `head -1 $FILE | tr "\t" "\n" | grep -n "^$COLUMNTITLE"'$' | cut -f 1 -d :`

Works only if there is a single column matching the exact $COLUMNTITLE

Joce

Posted 2015-06-17T10:51:19.253

Reputation: 637

1

Try with this:

function proj() {
    awk -v c="$1" 'NR==1 {for (i=1; i<=NF; i++) if ($i==c) break} {print $i}' "$2"
}

Use it like:

proj C-8 table.txt

If you expect to pass in column names which are not present in the table you should check that: add i!=NF+1 before {print $i}, otherwise you'll get as many empty lines as the rows of the table.

You can also put it in a separate file instead using a function.

cYrus

Posted 2015-06-17T10:51:19.253

Reputation: 18 102