append column and row header to a file in awk script.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers append column and row header to a file in awk script.
# 1  
Old 08-07-2012
append column and row header to a file in awk script.

Hi! Is there a way to append column and row header to a file in awk script.
For example if I have
Code:
Jane   F   39   manager
Carlos M  40   system administrator
Sam    F   20   programmer

and I want it to be

Code:
# name   gend   age   occup
1 Jane     F       39     manager
2 Carlos   M       40     system administrator
3 Sam      F        20     programmer

I just have no ideas... Smilie
Thanks in advance


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 08-07-2012 at 08:46 AM.. Reason: code tags
# 2  
Old 08-07-2012
Code:
$ awk 'BEGIN{printf("%-3s%-8s%-6s%-5s%-20s\n","#","name","gend","age","occup")}{printf("%-3s%-8s%-6s%-5s%-20s\n",NR,$1,$2,$3,$4)}' infile
#  name    gend  age  occup
1  Jane    F     39   manager
2  Carlos  M     40   system
3  Sam     F     20   programmer

This User Gave Thanks to zaxxon For This Post:
# 3  
Old 08-07-2012
Quote:
Originally Posted by zaxxon
Code:
$ awk 'BEGIN{printf("%-3s%-8s%-6s%-5s%-20s\n","#","name","gend","age","occup")}{printf("%-3s%-8s%-6s%-5s%-20s\n",NR,$1,$2,$3,$4)}' infile
#  name    gend  age  occup
1  Jane    F     39   manager
2  Carlos  M     40   system
3  Sam     F     20   programmer

Thanks but if it is not a secret where the numbers 3, 8, 6, 5, 20 are taken from in
Code:
{printf("%-3s%-8s%-6s%-5s%-20s\n","#","name","gend","age","occup")}

# 4  
Old 08-07-2012
No secret. If you count the number and the arguments that are following, separated by a comma, you see that the 1st part containing the numbers is defining the layout, and the 2nd part is just filling the data in the "fields".
The minus says it is left aligned, the 3 says the column is 3 characters wide, and the value supplied should be taken as a s which stands for "string".
And so on...
I have just checked how broad the columns of the heading and your input file is and have defined them for the header as well as for every line that will go through awk.
printf works like this in shell, C, awk, ..., very handy for printing formatted Smilie

Last edited by zaxxon; 08-07-2012 at 09:22 AM.. Reason: spelling
This User Gave Thanks to zaxxon For This Post:
# 5  
Old 08-07-2012
Quote:
Originally Posted by zaxxon
No secret. If you count the number and the arguments that are following, separated by a comma, you see that the 1st part containing the numbers is defining the layout, and the 2nd part is just filling the data in the "fields".
The minus says it is left aligned, the 3 the column is 3 characters wide, and the value supplied should be take as a s which stands for "string".
And so on...
I have just checked how broad the columns of the heading and your input file is and have defined them for the header as well as for every line that will go through awk.
printf works like this in shell, C, awk, ..., very handy for printing formatted Smilie
Thanks Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Script to do column to row in awk

Hi , Can anyone help me suggesting - how to do the below trick with awk Input 120 130 140 210 310 410 645 729 800 Output 120 130 140 (6 Replies)
Discussion started by: Indra2011
6 Replies

2. Shell Programming and Scripting

awk script to append suffix to column when column has duplicated values

Please help me to get required output for both scenario 1 and scenario 2 and need separate code for both scenario 1 and scenario 2 Scenario 1 i need to do below changes only when column1 is CR and column3 has duplicates rows/values. This inputfile can contain 100 of this duplicated rows of... (1 Reply)
Discussion started by: as7951
1 Replies

3. Shell Programming and Scripting

awk script row to column

Hi.. I have data : Report testing1 20180419 08:00 Report testing2 20180419 07:35 Report testing 20180419 08:01 Source = data1 Report testing4 20180419 08:05 Source = data1 Report testing5 20180419 08:10 Source = data2 Report testing6 20180419 08:01 Report testing7 20180419 08:19... (4 Replies)
Discussion started by: buncit8
4 Replies

4. Shell Programming and Scripting

Matching column value from 2 different file using awk and append value from different column

Hi, I have 2 csv files. a.csv HUAWEI,20LMG011_DEKET_1296_RTN-980_IDU-1-11-ISV3-1(to LAMONGAN_M),East_Java,20LMG011_DEKET_1296_RTN-980_IDU-1,20LMG011,20LMG 027_1287_LAMONGAN_RTN980_IDU1,20LMG027,1+1(HSB),195.675,20LMG011-20LMG027,99.9995,202.6952012... (7 Replies)
Discussion started by: tententen
7 Replies

5. Shell Programming and Scripting

Awk/sed script for transposing any number of rows with header row

Greetings! I have been trying to find out a way to take a CSV file with a large number of rows, and a very large number of columns (in the thousands) and convert the rows to a single column of data, where the first row is a header representing the attribute name and the subsequent series of... (3 Replies)
Discussion started by: tntelle
3 Replies

6. Shell Programming and Scripting

Add column header and row header

Hi, I have an input like this 1 2 3 4 2 3 4 5 4 5 6 7 I would like to count the no. of columns and print a header with a prefix "Col". I would also like to count the no. of rows and print as first column with each line number with a prefix "Row" So, my output would be ... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

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

8. 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

9. Shell Programming and Scripting

AWK Script - Print a column - within a Row Range

Hi, Please read the whole thread. I have been working on this script below. It works fine, feel free to copy and test with the INPUT File below as well. example: PACKET DATA PROTOCOL CONTEXT DATA APNID PDPADD EQOSID VPAA PDPCH PDPTY PDPID 10 ... (6 Replies)
Discussion started by: panapty
6 Replies

10. UNIX for Dummies Questions & Answers

split header row into one column

So, I have a massive file with thousands of columns I want a list of the headers in one column in another file. So I need to strip off the top line (can use head-1) But how can I convert from this format: A B C D E F G to A B C D E F G (6 Replies)
Discussion started by: polly_falconer
6 Replies
Login or Register to Ask a Question