Appending new column to existing files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Appending new column to existing files
# 1  
Old 01-10-2012
Appending new column to existing files

Hi, i want to add another column to existing files containing strings and need to have the final output as a csv file. i have quite a number of files, each with varying number of rows and i need to append the string "test" for all the valid rows for each file. my sample raw files looks like this one:
Code:
123.1 12
123.2 12.1
123.3 12.2
123.4 12.3

my desired output is the one below:
Code:
test,123.1,12
test,123.2,12.1
test,123.3,12.2
test,123.4,12.3

any help and solution is greatly appreciated. thanks in advance.
# 2  
Old 01-10-2012
Code:
awk '{gsub(/ /,",",$0); print "test,"$0}' infile

This User Gave Thanks to zaxxon For This Post:
# 3  
Old 01-10-2012
If you want to add a constant string "test" at the beginning of every string.. One way using Perl.
Code:
$ perl -lane 'unshift(@F,"test");print join(",",@F)' inputfile
test,123.1,12
test,123.2,12.1
test,123.3,12.2
test,123.4,12.3

This will work even if the columns are separated by any number of spaces (spaces, tabs).
This User Gave Thanks to balajesuri For This Post:
# 4  
Old 01-10-2012
Thank you guys, both ways very helpful as always,Smilie
# 5  
Old 01-10-2012
Another way using sed. He, he Smilie
Code:
sed 's/^/test,/;s/ /,/' inputfile

# 6  
Old 01-10-2012
Code:
 
sed -e 's/ /,/' -e s'/^/test,/' input.txt

This User Gave Thanks to itkamaraj For This Post:
# 7  
Old 01-10-2012
hi, on the side, i need to save the new generated files into unique filenames:
if i do the loop on the final output if will be something like:
Code:
199901.csv

i want to save the final output as:
Code:
199901test.csv

how to i append the "test" part to the filename. thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Matching column then append to existing File as new column

Good evening I have the below requirements, as I am not an experts in Linux/Unix and am looking for your ideas how I can do this. I have file called file1 and file2. I need to get the second column which is text1_random_alphabets and find that in file 2, if it's exists then print the 3rd... (4 Replies)
Discussion started by: mychbears
4 Replies

2. Shell Programming and Scripting

Increment existing column in file

Hi All, I have a file with 3 millions records in which 3rd column is same throughout say its value is 0 throughout.for example: Col1 Col2 Col3 Col4 A 1 0 5 B 2 0 6 C 3 0 7 D 4 0 9 I want my output as : Col1 Col2 Col3 Col4 A 1 ... (4 Replies)
Discussion started by: Pinky456
4 Replies

3. Shell Programming and Scripting

Adding a new column to an existing one

I have a data file that looks like this: 0.01 1 3822 4.97379915032e-14 4.96982253992e-09 0 0.01 3822 1 4.97379915032e-14 4.96982253992e-09 0 0.01 2 502 0.00993165137406 993.165137406 0 0.01 502 2 0.00993165137406 993.165137406 0 0.01 4 33 0.00189645523539 189.645523539 0 0.01 33 4... (3 Replies)
Discussion started by: kayak
3 Replies

4. Shell Programming and Scripting

Appending column to rows

Hi All, Input.txt KGO Id "003" .......... .......... Par "CPara" BIN RECGET Name "DIR_PATH" Prompt "DIR_PATH" END RECGET KGO ............ .......... ............... KGO Id "077" .......... .......... (7 Replies)
Discussion started by: unme
7 Replies

5. Shell Programming and Scripting

Appending a CPIO to an existing archive

I created a CPIO archive I wanted to add addition data to it but am having issues: -rw-r--r-- 1 test test 629295104 2011-10-28 12:41 /home/test/Downloads/test.cpio I tried: sudo find /tmp -depth | cpio -oAO /home/test/Downloads/test.cpio cpio: premature end of file and (1 Reply)
Discussion started by: metallica1973
1 Replies

6. UNIX for Dummies Questions & Answers

Appending lines from an existing list to each line in another existing list

Evening all ! I would like to ask your expertise on how to accomplish the following ; I have 2 lists, and would like each line from list2 to be appended to each line in list1, resulting in list3 ; List1; alpha beta charlie List2; one two three (4 Replies)
Discussion started by: TAPE
4 Replies

7. Shell Programming and Scripting

Adding a new column in a file with other existing columns

Hi All , Kindly help me with this soln awk '{printf "%s %7s \n", $1,$c}' infile where value of variable c I am externally giving input But executing the above command shows all the columns of infile where as I want only 1st column of infile and 2nd column should print value c (8 Replies)
Discussion started by: Pratik4891
8 Replies

8. Shell Programming and Scripting

How to rewrite a existing value in a column inside a file?

I am having 4 field in a file name age date status i want to update or rewrite a value of status with another value how it can be done i used awk & sed but it shows result but not updating in original file help me out... Thanks (4 Replies)
Discussion started by: ragavendar
4 Replies

9. Shell Programming and Scripting

appending column file

Hi all, I have two files with the same number of lines the first file is a.dat and looks like 0.000 1.000 1.000 2.000 ... the fields are tab separated the second file is b.dat and looks like 1.2347 0.546 2.3564 0.321 ... the fields are tab separated I would like to have a file c.dat... (4 Replies)
Discussion started by: f_o_555
4 Replies

10. Shell Programming and Scripting

how to add a new column in an existing file

Hi guys, Please help me if u have some solution. I have a file with three columns separated by ':' - INPUT_FILE C416722_2 : calin Dirigent : Dirigent AC4174_6 : Jac : cal_co TC4260_5 : [no : lin kite BC426302_1 : [no : calin Dirigent lin JC426540_3 : lin Pymo_bin : calin TC428_3 : no7... (4 Replies)
Discussion started by: sam_2921
4 Replies
Login or Register to Ask a Question