awk/sed column replace using column header - help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk/sed column replace using column header - help
# 1  
Old 06-17-2008
awk/sed column replace using column header - help

Code:
$ 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:

Code:
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"
Code:
$ awk  '$4=$6' log.txt  | awk '{$6=""}1'

Please help.
# 2  
Old 06-17-2008
Basic shift:
Code:
 awk '{print $1,$2,$3,$6,$5,$7}' log.txt

The output is:
Code:
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:
Code:
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

# 3  
Old 06-17-2008
You seem to be doing more than what you described. But here's what I did:
Code:
gawk 'BEGIN {print "Name Age Sex alias nfld xsd"}
{if(NR > 1) print $1 " " $2 " " $3 " " $6 " " $5 " " $7}' log.txt

# 4  
Old 06-17-2008
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.
# 5  
Old 06-17-2008
Hehe, shift the table, grep what you want and shift backSmilie
# 6  
Old 06-17-2008
I feel I explained my query in a wrong way. Let me explain again:

Input file:

Code:
$ 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:

Code:
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.

Last edited by jkl_jkl; 06-17-2008 at 03:27 PM.. Reason: added color
# 7  
Old 06-17-2008
Here's what I did to get your output. This should give you idea on what to do:
Code:
gawk \
'{
if(NR == 1)
{
  split($0, tmp);

  for(i = 1; i <= NF; i++)
    fields[tmp[i]] = 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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk/sed summation of one column based on some entry in first column

Hi All , I am having an input file as stated below Input file 6 ddk/djhdj/djhdj/Q 10 0.5 dhd/jdjd.djd.nd/QB 01 0.5 hdhd/jd/jd/jdj/Q 10 0.5 512 hd/hdh/gdh/Q 01 0.5 jdjd/jd/ud/j/QB 10 0.5 HD/jsj/djd/Q 01 0.5 71 hdh/jjd/dj/jd/Q 10 0.5 ... (5 Replies)
Discussion started by: kshitij
5 Replies

2. UNIX for Beginners Questions & Answers

awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)

I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy... (10 Replies)
Discussion started by: Kunalcurious
10 Replies

3. Shell Programming and Scripting

Solution for replacement of 4th column with 3rd column in a file using awk/sed preserving delimters

input "A","B","C,D","E","F" "S","T","U,V","W","X" "AA","BB","CC,DD","EEEE","FFF" required output: "A","B","C,D","C,D","F" "S", T","U,V","U,V","X" "AA","BB","CC,DD","CC,DD","FFF" tried using awk but double quotes not preserving for every field. any help to solve this is much... (5 Replies)
Discussion started by: khblts
5 Replies

4. Shell Programming and Scripting

awk or sed: change the color of a column w/o screwing up column spacing

Hey folks. I wrote a little awk script that summarizes /proc/net/dev info and then pipes it to the nix column command to set up column spacing appropriately. Here's some example output: Iface RxMBytes RxPackets RxErrs RxDrop TxMBytes TxPackets TxErrs TxDrop bond0 9 83830... (3 Replies)
Discussion started by: ryran
3 Replies

5. UNIX for Dummies Questions & Answers

Rename a header column by adding another column entry to the header column name

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (1 Reply)
Discussion started by: Vavad
1 Replies

6. Shell Programming and Scripting

Rename a header column by adding another column entry to the header column name URGENT!!

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (4 Replies)
Discussion started by: Vavad
4 Replies

7. Shell Programming and Scripting

awk/sed to search & replace data in first column

Hi All, I need help in manipulating the data in first column in a file. The sample data looks like below, Mon Jul 18 00:32:52 EDT 2011,NULL,UAT Jul 19 2011,NULL,UAT 1] All field in the file are separated by "," 2] File is having weekly data extracted from database 3] For eg.... (8 Replies)
Discussion started by: gr8_usk
8 Replies

8. Shell Programming and Scripting

Replace last row of a column in bash/awk/sed

Hi, I've got a file with 3 columns which ends like this: ... 1234 345 1400 5287 733 1400 8472 874 1400 9317 726 1400 I want to replace the last row of the last column with the value 0. So my new file will end: ... 1234 345 1400 5287 733 1400 8472 874 1400 9317 726 ... (5 Replies)
Discussion started by: jhunter87
5 Replies

9. Shell Programming and Scripting

Awk or Sed, fubd match in column, then edit column.

FILE A: 9780743551526,(Abridged) 9780743551779,(Unabridged) 9780743582469,(Abridged) 9780743582483,(Unabridged) 9780743563468,(Abridged) 9780743563475,(Unabridged) FILE B: c3saCandyland 9780743518321 "CANDYLAND" "MCBAIN, ED" 2001 c3sbCandyland 9780743518321 ... (7 Replies)
Discussion started by: glev2005
7 Replies

10. Shell Programming and Scripting

Find and replace a column that has '' to NULL in a comma delimited using awk or sed

Hi this is my first time posting ever. I'm relatively new in using AWK/SED, I've been trying many a solution. I'm trying to replace the 59th column in a file where if I encounter '' then I would like to replace it with the word NULL. example 0 , '' , '' , 0 , 195.538462 change it to 0... (5 Replies)
Discussion started by: gumal901
5 Replies
Login or Register to Ask a Question