How to place Record in a row as a column name?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to place Record in a row as a column name?
# 1  
Old 02-16-2016
How to place Record in a row as a column name?

Hi,

I want to know how to place a particular field availble in a record as the columnname.

HTML Code:
Name,Sam,Age,13,School, StJohn
Name,Rita,Age,12,School, StMichael,
Name,John,Age,14,School, StAnthony
I want the output as
HTML Code:
Name Age School
Sam 13 StJohn
Rita 12 StMichael
John 14 StAnthony
I am not sure how to transpose with help of awk
# 2  
Old 02-16-2016
Code:
awk -F, 'BEGIN{print "Name","Age","School"}{print $2,$4,$6}' sidnow.file

This User Gave Thanks to Aia For This Post:
# 3  
Old 02-16-2016
Hi try:
Code:
awk -F, 'NR==1{print $1,$3,$5}{print $2,$4,$6}' file

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 02-16-2016
Thanks for the reply guys, however forgot to mention, the number of columns are dynamic. As in it will not be Name, Age, School always. It can Name, Age, School, some other field etc.

How can I dynamically assign the odd numbered fields as the column name for the even numbered fields?
# 5  
Old 02-16-2016
Try:
Code:
awk -F, '
  NR==1 { 
    s=$1
    for(i=3; i<=NF; i+=2) s=s OFS $i
    print s
  }
  {
    s=$2
    for(i=4; i<=NF; i+=2) s=s OFS $i
    print s
  }
' file

# 6  
Old 02-16-2016
Or with a function
Code:
awk -F, 'function prt(start){for (i=start; i<=NF-2; i+=2) printf "%s ",$i; print $i} NR==1 {prt(1)} {prt(2)}' file


Last edited by MadeInGermany; 02-17-2016 at 08:54 AM.. Reason: removed an extraneous empty print
# 7  
Old 02-16-2016
Code:
awk -F, '
NR==1   {DL = ""
         for (i=1; i<=NF; i+=2) {printf "%s%s", DL, $i
                                 DL = FS
                                }
         printf "\n"
        }
        {DL = ""
         for (i=2; i<=NF; i+=2) {printf "%s%s", DL, $i
                                 DL = FS
                                }
         printf "\n"
        }
' file

---------- Post updated at 01:23 ---------- Previous update was at 01:20 ----------

or, even better:
Code:
awk -F, '
NR==1   {for (i=1; i<NF; i+=2)  printf "%s%s", $i, FS
         printf "%s\n", $i
        }
        {for (i=2; i<NF; i+=2)  printf "%s%s", $i, FS
         printf "%s\n", $i
        }
' file

---------- Post updated at 01:24 ---------- Previous update was at 01:23 ----------

Too late, MadeInGermany's function approach beats this clumsy one by far.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modifying text file records, find data in one place in the record and print it elsewhere

Hello, I have some text data that is in the form of multi-line records. Each record ends with the string $$$$ and the next record starts on the next line. RDKit 2D 15 14 0 0 0 0 0 0 0 0999 V2000 5.4596 2.1267 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 ... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

2. Shell Programming and Scripting

Print first row of column a, last row of column b if column a has the same value

I have a table with this structure: cola colb colc 1 19 lemon 20 31 lemon 32 100 lemon 159 205 cherries 210 500 cherries and need to parse it into this format: cola colb colc 1 100 lemon 159 500 cherries So I need the first row of cola and the last row of colb if colc has the... (3 Replies)
Discussion started by: coppuca
3 Replies

3. Shell Programming and Scripting

Split column data if the table has n number of column's with some record

Split column data if the table has n number of column's with some record then how to split n number of colmn's line by line with records Table --------- Col1 col2 col3 col4 ....................col20 1 2 3 4 .................... 20 a b c d .................... v ... (11 Replies)
Discussion started by: Priti2277
11 Replies

4. UNIX for Dummies Questions & Answers

Match sum of values in each column with the corresponding column value present in trailer record

Hi All, I have a requirement where I need to find sum of values from column D through O present in a CSV file and check whether the sum of each Individual column matches with the value present for that corresponding column present in the trailer record. For example, let's assume for column D... (9 Replies)
Discussion started by: tpk
9 Replies

5. Shell Programming and Scripting

Print row on 4th column to all row

Dear All, I have input : SEG901 5173 9005 5740 SEG902 5227 5284 SEG903 5284 5346 SEG904 5346 9010 SEG905 5400 5456 SEG906 5456 5511 SEG907 5511 9011 SEG908 5572 9015 SEG909 5622 9020 SEG910 5678 5739 SEG911 5739 5796 SEG912 5796 9025 ... (3 Replies)
Discussion started by: attila
3 Replies

6. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

7. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

8. Shell Programming and Scripting

Moving data from a specified column/row to another column/row

Hello, I have an input file like the following: 11_3_4 2_1_35 3_15__ _16989 Where '_' is a space. The data is in a table. Is there a way for the program to prompt the user for x1,y1 and x2,y2, where x1,y1 is the desired number (for example x=6 y=4 is a value of 4) and move to a desired spot... (2 Replies)
Discussion started by: jl487
2 Replies

9. Shell Programming and Scripting

Split a record ( in a row) and make it as new row

Hi All, The following is the scenario id name -------------- 1 William;Johnson 2 Azim;Abdul 3 Grasim . . etc.... I need the following output id name -------------- 1 William 1 Johnson 2 Azim (2 Replies)
Discussion started by: kottursamy
2 Replies

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question

Featured Tech Videos