awk/sed column replace using column header - help

$ cat log.txt
Name Age Sex Lcation nfld alias xsd
CC 25 M XYZ asx KK Y
BB 21 F XAS awe SS N
SD 21 M AQW rty SD A

How can I replace the column with header "Lcation" with the column with header "alias" and delete the "alias" column?

so that the final output will become:

Name Age Sex alias nfld xsd
CC 25 M KK asx Y
BB 21 F SS awe N
SD 21 M SD rty A

I can do this by specifying the "field number"(like below), but I wanted to do that using "field header name"

$ awk  '$4=$6' log.txt  | awk '{$6=""}1'

Please help.

Basic shift:

 awk '{print $1,$2,$3,$6,$5,$7}' log.txt

The output is:

0000000    N   a   m   e       A   g   e       S   e   x       a   l   i
0000020    a   s       n   f   l   d       x   s   d  \n   C   C       2
0000040    5       M       K   K       a   s   x       Y  \n   B   B
0000060    2   1       F       S   S       a   w   e       N  \n   S   D
0000100        2   1       M       S   D       r   t   y       A  \n
0000117

Your output is:

0000000    N   a   m   e       A   g   e       S   e   x       a   l   i
0000020    a   s       n   f   l   d           x   s   d  \n   C   C
0000040    2   5       M       K   K       a   s   x           Y  \n   B
0000060    B       2   1       F       S   S       a   w   e           N
0000100   \n   S   D       2   1       M       S   D       r   t   y
0000120        A  \n
0000123

You seem to be doing more than what you described. But here's what I did:

gawk 'BEGIN {print "Name Age Sex alias nfld xsd"}
{if(NR > 1) print $1 " " $2 " " $3 " " $6 " " $5 " " $7}' log.txt

Thanks for the replies.

But the reason I asked "based on column header" because I have more than 70 fields in the file (log.txt is just a sample) and field positions are not fixed. I can identify each field based on the field header(Name age sex etc etc).

So can anyone help me ?

Please help.

Hehe, shift the table, grep what you want and shift back:)

I feel I explained my query in a wrong way. Let me explain again:

Input file:

$ cat log.txt
Name Age Sex Lcation nfld alias xsd
CC 25 M XYZ asx KK Y
BB 21 F XAS awe SS N
SD 21 M AQW rty SD A

How can I replace the column with header "Lcation" with the column with header "alias" and delete the alias column?

Required output:

Name Age Sex alias nfld xsd
CC 25 M KK asx Y
BB 21 F SS awe N
SD 21 M SD rty A

I don't want to use field number because my actual file contains more than 70 fields and the field positions are not fixed(the above log.txt is just a sample). Thats why I am looking for a solution which replaces based on column header.

Please help.

Here's what I did to get your output. This should give you idea on what to do:

gawk \
'{
if(NR == 1)
{
  split($0, tmp);

  for(i = 1; i <= NF; i++)
    fields[tmp] = i

  print "Name Age Sex alias nfld xsd"
}
else
{
  print $fields["Name"], $fields["Age"], $fields["Sex"], $fields["alias"], $fields["nfld"], $fields["xsd"]
}
}' log.txt

The key is that if var = 1, $var will print the first column.

$cat awk.file
NR==1{
for(i=1;i<=NF;i++)
       {
        if($i=="Name")  a=i
        if($i=="Age")   b=i
        if($i=="Sex")   c=i
        if($i=="alias") d=i
        if($i=="nfld")  e=i
        if($i=="xsd")   f=i
       }
     }
{
  print $a,$b,$c,$d,$e,$f
}

Run the script:

awk -f awk.file log.txt

Regards,

Thanks danmero, but still as I stated I have more than 70 columns, so specifying each and every column name is something difficult for me.

i.e.

Name Age Sex ............ fldx bloodgrp .....
A 23 M .............. Y A+ ......
D 21 F................ N B+ ........
......
.......

I wanted to replace a column whose header is "Sex" with the column with header "fldx". The field number of both the columns can be dynamic. So I want to replace them based on their column header name.

so that the output will look something like this

Name Age fldx ................. bloodgrp .......
A 23 Y  .................. A+...............
B 21 N ................... B+..............
...............
.............

so the output will not be having the "Sex" column (will be replaced by "fldx" column)

Thanks

So, something like this maybe?

awk -v src="alias" -v dst="Lcation" 'NR == 1 {
  for (i=1; i<=NF; ++i) { if ($i == src) scol=i; if ($i == dst) dcol=i; }
  if (scol == "") print "undefined field src=" src;
  if (dcol == "") print "undefined field dst=" dst;
  if (scol == "" || dcol == "") exit 1;
}
{ $dcol=$scol; $scol=""; print }' log.txt

If your awk doesn't support the -v option, see if you can find nawk, mawk, gawk, or XPG4 awk.

Thanks era.