add (append) a column in a tab delimited file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers add (append) a column in a tab delimited file
# 1  
Old 01-22-2012
add (append) a column in a tab delimited file

I have a file having the following entries:

test1 test2 test3
11 22 33
22 44 66
99 99 44
---

I want to add a column so that the above file becomes:
test1 test2 test3 notest
11 22 33 *
22 44 66 *
99 99 44 *
---

Thanks
# 2  
Old 01-22-2012
Code:
awk 'NR==1 {print}  NR>1 {printf("%s\t%s\n", $0, "*") }' input.csv > newoutput.csv


Last edited by jim mcnamara; 01-22-2012 at 01:47 PM.. Reason: assume first line is column headers
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 01-22-2012
Quote:
Originally Posted by jim mcnamara
Code:
awk '{printf("%s\t%s\n", $0, "*") }' input.csv > newoutput.csv

Thanks for reply.

how to add header in the added column
# 4  
Old 01-22-2012
Changed the original post. So we don't step through the "let's change this next" game
please post a sample showing all the desired outputs, and a small sample of input.
# 5  
Old 01-22-2012
Quote:
Originally Posted by jim mcnamara
Changed the original post. So we don't step through the "let's change this next" game
please post a sample showing all the desired outputs, and a small sample of input.
Thanks jim.

Input file
$ cat file.txt
test1 test2 test3
11 22 33
22 23 66
99 24 44
12 25 34
11 23 67

output file

test1 test2 test3 newCol
11 22 33 *
22 23 66 *
99 24 44 *
12 25 34 *
11 23 67 *

I have a solution based on Jim's solution. Is there any 'sed' equivalemt command to do the same.

Code:
$ awk 'NR==1 {printf("%s\t%s\n", $0, "newCol")}  NR>1 {printf("%s\t%s\n", $0, "*") }' file.txt
test1	test2	test3	newCol
11	22	33	*
22	23	66	*
99	24	44	*
12	25	34	*
11	23	67	*

Thanks

Last edited by mary271; 01-22-2012 at 03:04 PM..
# 6  
Old 01-22-2012
Code:
$ cat input
test1    test2    test3
11    22    33
22    23    66
99    24    44
12    25    34
11    23    67
$
$ sed '1s/$/\tnewCol/; 2,$s/$/\t*/' input
test1    test2    test3    newCol
11    22    33    *
22    23    66    *
99    24    44    *
12    25    34    *
11    23    67    *
$
$

This User Gave Thanks to balajesuri For This Post:
# 7  
Old 01-22-2012
Worked fine for me.
Thanks

Quote:
Originally Posted by balajesuri
Code:
$ cat input
test1    test2    test3
11    22    33
22    23    66
99    24    44
12    25    34
11    23    67
$
$ sed '1s/$/\tnewCol/; 2,$s/$/\t*/' input
test1    test2    test3    newCol
11    22    33    *
22    23    66    *
99    24    44    *
12    25    34    *
11    23    67    *
$
$

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace a column in tab delimited file with column in other tab delimited file,based on match

Hello Everyone.. I want to replace the retail col from FileI with cstp1 col from FileP if the strpno matches in both files FileP.txt ... (2 Replies)
Discussion started by: YogeshG
2 Replies

2. Shell Programming and Scripting

Append data to first column delimited file

Hi, I have a data like Input: 12||34|56|78 Output: XYZ|12||34|56|78 I tried like this , but it puts it on another line awk -F "|" ' BEGIN {"XYZ"} {print $0} 'file Any quick suggessitons in sed/awk ? am using HP-UX (3 Replies)
Discussion started by: selvankj
3 Replies

3. Shell Programming and Scripting

Delete an entire column from a tab delimited file

Hi, Can anyone please tell me about how we can delete an entire column from a tab delimited file? Mu input_file.txt looks like this: And I want the output as: I used the below code nawk -v d="1" 'BEGIN{FS=OFS="\t"}{$d=""}{print}' input_file.txtBut in the output, the first column is... (5 Replies)
Discussion started by: sampoorna
5 Replies

4. Shell Programming and Scripting

Convert a 3 column tab delimited file to a matrix

Hi all, I have a 3 columns input file like this: CPLX9PC-4943 CPLX9PC-4943 1 CPLX9PC-4943 CpxID123 0 CPLX9PC-4943 CpxID126 0 CPLX9PC-4943 CPLX9PC-5763 0.5 CPLX9PC-4943 CpxID13 0 CPLX9PC-4943 CPLX9PC-6163 0 CPLX9PC-4943 CPLX9PC-6164 0.04... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

5. Shell Programming and Scripting

[Solved] Append an header to a tab delimited file

Dear All, I would like to find an automatic way to add a given code which belong to a class at the end of the column , for example this is my input file: 0610009O20Rik V$VMYB_01 310 (+) 1 0.971 v-Myb V$EVI1_04 782 (-) 0.763 0.834 Evi-1 V$ELK1_02 1966 (-) 1 0.984 Elk-1... (4 Replies)
Discussion started by: paolo.kunder
4 Replies

6. Shell Programming and Scripting

Extract second column tab delimited file

I have a file which looks like this: 73450 articles and news developmental psychology 2006-03-30 16:22:40 1 http://www.usnews.com 73450 articles and news developmental psychology 2006-03-30 16:22:40 2 http://www.apa.org 73450 articles and news developmental psychology 2006-03-30... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

7. Shell Programming and Scripting

Using sed on 1st column of tab delimited file

Hi all, I'm new to Unix and work primarily in bioinformatics. I am in need of a script which will allow me to replace "1" with "chr1" in only the first column of a file which looks like such: 1 10327 rs112750067 T C . PASS ASP;RSPOS=10327;... (4 Replies)
Discussion started by: Hkins552
4 Replies

8. UNIX for Dummies Questions & Answers

Add a new column to a tab delimited text file

I want to add a new column to a tab delimited text file. It will be the first column and it will just be 1's. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

9. Shell Programming and Scripting

How to append database column to a delimited file

Hi, I have the below flat filewith ~ as delimiter emp.no~dept.name I need to append corresponding emp.name column which will come from database based on emp.no in the flat file. I need the output as dept.name~emp.name Can anyone please help me in resolving this issue.. I tried the... (2 Replies)
Discussion started by: siri_886
2 Replies

10. Shell Programming and Scripting

Delete first column in tab-delimited text-file

I have a large text-file with tab-delimited genetic data that looks like: KSC112 KSC234 0 0 1 1 A G C T I simply wan to delete the first column, but since the file has 600 000 columns, it is not possible with awk (seems to be limited at 32k columns). Does anyone have an idea how to do this? (2 Replies)
Discussion started by: andmal
2 Replies
Login or Register to Ask a Question