1

In my shell script after executing a few commands, I'm getting the output below:

Name      English    Maths    science   Social studies
------    --------  --------  -------- ---------------
James     20        25        30        40

(many values)
(or)

Roll Num   Name     English   Maths   science   Social studies
---------  ----    --------  -------  --------  ---------------
  1        James     20        25        30        40

(Many values)

I want to print the values based on a column heading match.

Example:
cmd(matching Maths) test.txt

o/p:25

I have tried few awk and sed commands,But I didn't get proper output.

Can someone help me how to get this?

Prani
  • 23
  • @don_crissti If i'm getting duplicate values also fine.Just i want print the values based on a column heading match. – Prani Nov 09 '15 at 12:58

1 Answers1

0
    awk -v subj="Maths" '{
    for(n=1;n<=NF;n++){
      if ($n==subj){
        subp=n;nrr=NR+2
   }};
   if(NR==nrr){
      print $subp
   }}' test.txt

Here if subject is Maths the field position subp and nrr (NR +2 where the value for the subj Maths lies) is assigned.

jijinp
  • 1,401