user2260271
user2260271

Reputation: 69

Search for a column by name in awk

I have a file that has many columns. Let us say "Employee_number" "Employee_name" "Salary". I want to display all entries in a column by giving all or part of the column name. For example if my input "name" I want all the employee names printed. Is it possible to do this in a simple manner using awk? Thanks

Upvotes: 3

Views: 931

Answers (2)

Chris Seymour
Chris Seymour

Reputation: 85855

Simple one-liner will do the trick:

$ cat file
a b c 
1 2 3
1 2 3
1 2 3

$ awk -v c="a" 'NR==1{for(i=1;i<=NF;i++)n=$i~c?i:n;next}n{print $n}' file
1
1
1

$ awk -v c="b" 'NR==1{for(i=1;i<=NF;i++)n=$i~c?i:n;next}n{print $n}' file
2
2
2

$ awk -v c="c" 'NR==1{for(i=1;i<=NF;i++)n=$i~c?i:n;next}n{print $n}' file
3
3
3  

# no column d so no output
$ awk -v c="d" 'NR==1{for(i=1;i<=NF;i++)n=$i~c?i:n;next}n{print $n}' file

Note: as in your requirement you want name to match employee_name just be aware if you give employee you will get the last column matching employee this is easily changed however.

Upvotes: 4

Simon
Simon

Reputation: 10841

Given a script getcol.awk as follows:

BEGIN {
    colname = ARGV[1]
    ARGV[1] = ""
    getline
    for (i = 1; i <= NF; i++) {
        if ($i ~ colname) {
            break;
        }
    }
    if (i > NF) exit
}

{print $i}

... and the input file test.txt:

apple   banana  candy   deer    elephant
A   B   C   D   E
A   B   C   D   E
A   B   C   D   E
A   B   C   D   E
A   B   C   D   E
A   B   C   D   E
A   B   C   D   E

... the command:

$ awk -f getcol.awk b <test.txt

... gives the following output:

B
B
B
B
B
B
B

Note that the output text does not include the first line of the test file, which is treated as a header.

Upvotes: 3

Related Questions