Add column header and row header


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add column header and row header
# 1  
Old 03-01-2013
Add column header and row header

Hi,

I have an input like this

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

Code:
        Col1 Col2 Col3 Col4
Row1     1     2     3     4
Row2     2     3     4     5
Row3     4     5     6     7

# 2  
Old 03-01-2013
A crude first suggestion, that you could refine as you see fit:
Code:
awk 'NR==1{for(i=1; i<=NF; i++) h=h OFS "Col" i; print h}{$1="Row" NR OFS $1}1' OFS='\t' file


Last edited by Scrutinizer; 03-01-2013 at 03:40 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 03-01-2013
Code:
awk ' {
        if(!c)
        {
                for (i = 1; i <= NF; i++)
                {
                        printf "\tCol%d", i
                }
        }

        c = 1

        printf "\nRow%d\t", NR

        for (i = 1; i <= NF; i++)
        {
                printf "%d\t", $i
        }

} END {
        printf "\n"
} ' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find header in a text file and prepend it to all lines until another header is found

I've been struggling with this one for quite a while and cannot seem to find a solution for this find/replace scenario. Perhaps I'm getting rusty. I have a file that contains a number of metrics (exactly 3 fields per line) from a few appliances that are collected in parallel. To identify the... (3 Replies)
Discussion started by: verdepollo
3 Replies

2. Shell Programming and Scripting

awk to skip header row and add string to field

The awk below does put in VUS in the 9th field but I can not seem to skip the header then add the VUS. I tried to incorporate NR >=2 and NR > 1 with no luck. Thank you :). input Chr Start End Ref Alt Func.refGene PopFreqMax CLINSIG Classification chr1 43395635 ... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

Convert rows to column and add header

Hi, I need help to convert rows in input file into a table. inputfile 192.98.1 192.98.192.98.17 VVC family Zorro 10 192.98.1 192.98.192.98.17 VVC family Ace 1 192.98.1 192.98.192.98.17 VVC family ... (4 Replies)
Discussion started by: redse171
4 Replies

4. Shell Programming and Scripting

Add Header to column

hello, can someone helpe me on below issue. i have a list of names and IDs, mick 3424 noah 134 samuel 787 jack 434 i want to add a head for both colum using awk or sed or any tool output NAME ID mick 3424 noah 134 samuel 787 (10 Replies)
Discussion started by: Sara_84
10 Replies

5. UNIX for Dummies Questions & Answers

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 Jane F 39 manager Carlos M 40 system administrator Sam F 20 programmer and I want it to be # name gend age occup 1 Jane F 39 manager 2 Carlos M ... (4 Replies)
Discussion started by: FUTURE_EINSTEIN
4 Replies

6. UNIX for Dummies Questions & Answers

Merge all csv files in one folder considering only 1 header row and ignoring header of all others

Friends, I need help with the following in UNIX. Merge all csv files in one folder considering only 1 header row and ignoring header of all other files. FYI - All files are in same format and contains same headers. Thank you (4 Replies)
Discussion started by: Shiny_Roy
4 Replies

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

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

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

10. Linux

Reading the header of a tar file(posix header)

say i have these many file in a directory named exam. 1)/exam/newfolder/link.txt. 2)/exam/newfolder1/ and i create a tar say exam.tar well the problem is, when i read the tar file i dont find any metadata about the directories,as you cannot create a tar containig empty directories. on the... (2 Replies)
Discussion started by: Tanvirk
2 Replies
Login or Register to Ask a Question