Visit Our UNIX and Linux User Community

Unix File - Adding columns in the middle

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix File - Adding columns in the middle
# 1  
Old 12-07-2010
Unix File - Adding columns in the middle


I have a comma separated flat file. It contains some 20 columns. I want to add two new columns at position 2,3. So that file will have 22 columns. I am providing here sample data with file having 4 columns. Appreciate your help in finding solution for this.

data in input file:

required output
call_id,key, value,conn_id,result,ani,

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 12-07-2010 at 02:42 PM.. Reason: Please use code tags!
# 2  
Old 12-07-2010
Where the 2 new adding columns come from ?

assuming they are in a file called in3 maybe something like

cut -d"," -f1 input >in1.tmp
cut -d"," -f2- input >in2.tmp
paste -d"," - - - in1.tmp in3 in2.tmp >output
rm in?.tmp

... lazy way ... waiting for Scrutinizer to shoot an awk 1 liner Smilie

Last edited by ctsgnb; 12-07-2010 at 03:05 PM..
# 3  
Old 12-07-2010
They are just hard coded values. Actually let me correct the output data. Sorry for the confusion.

call_id,key, value,conn_id,result,ani,

Last edited by vgersh99; 12-07-2010 at 02:57 PM.. Reason: code tags, PLEASE!
# 4  
Old 12-07-2010
nawk -F, '{$1=$1 OFS "key1" OFS "10";print}' OFS=, myFile

# 5  
Old 12-07-2010
Thanks a lot. I am getting correct data.
However title for new columns is coming as key1,10. I expect it as key, value. Is there a way to get correct column title too.
# 6  
Old 12-07-2010
awk -F, 'BEGIN{print "call_id,key, value,conn_id,result,ani,"}{NF+=2;for(i=NF-2;i>=2;i--)$(i+2)=$i;$2="key1";$3="10";print}' OFS=,  inputFile

# 7  
Old 12-07-2010
key, value

in a file called "in3" and give a try with what i suggest in post #2 and see if the generated output fits your needs

Previous Thread | Next Thread
Test Your Knowledge in Computers #66
Difficulty: Medium
IT and cybersecurity risk management is based on the intersection of these three areas, criticality, vulnerability, and threat.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding new column in the middle

Hi , I need to add few new columns in existing file .Any help would be great ex: existing file name,typ,add,dept New file(com1,sal are new) name,com1,type,sal,add,dept Thanks, mohan (1 Reply)
Discussion started by: mohan705
1 Replies

2. UNIX for Dummies Questions & Answers

Printing all the values in the middle of two columns

Hi, I have a tab delimited text file with three columns: Input: 1 25734 25737 1 32719 32724 1 59339 59342 1 59512 59513 1 621740 621745 For each row of the text file I want to print out all the values between the second and third columns, including them. The... (3 Replies)
Discussion started by: evelibertine
3 Replies

3. UNIX for Dummies Questions & Answers

Perl - adding columns to file

I have a file in which I need to add more columns to based on a key in the first file: File1 key1,abc,123, key2,def,456, key3,ghi,789, File2 key2,zyx,111,qqq, key3,yuu,222,www, key1,pui,333,eee, key4,xxx,999,rrr, I would like to create the following output: Output (1 Reply)
Discussion started by: WongSifu
1 Replies

4. UNIX for Dummies Questions & Answers

Trim and pad a field in the middle of unix file

Hello, I have a file with several lines and I need to trim and pad with spaces the data that are between position 6 and 15 included. Data in position 1 to 5 and after 15 could be anything, and should stay as they are. For instance, the following records in a file (underscore = space)... (4 Replies)
Discussion started by: BSF
4 Replies

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

6. Shell Programming and Scripting

Suggestions for adding columns to text file

Good afternoon to everyone, I have some input and output from various widgets that I am trying to get to play nicely together. Basically I would like to stay out of excel and be able to automate the entire process. I have read some posts here about how to use awk, nawk, etc, to do similar... (9 Replies)
Discussion started by: LMHmedchem
9 Replies

7. Shell Programming and Scripting

Adding Text To Middle Of File

Hi I am trying to write a bash script to add a line of text to the middle of a file. The way I worked it out was to calculate the number of lines and divide by 2. The file I am using is called newfile and it just contains lines of data. The new file I have created I have called midfile and... (7 Replies)
Discussion started by: BundBash
7 Replies

8. UNIX for Dummies Questions & Answers

Adding lines and columns to a file

Hi everybody, I've got two simples file1 like: aaa aaa aaa bbb bbb bbb ccc ccc ccc and file2 like: 111 111 111 222 222 222 333 333 333 I need to: 1) add a line say "new line" as the first line of the file 2)add a column from file2 (say column3) to file1; the new column should... (14 Replies)
Discussion started by: zajtat
14 Replies

9. Shell Programming and Scripting

Need Help for Adding Three new columns in existing file from fatching data from file

not required this time (36 Replies)
Discussion started by: Sandeep_Malik
36 Replies

10. UNIX for Dummies Questions & Answers

Adding columns to a file

I want to select the first column from a daily file called foo.csv. The result is written to file foo.txt. Currently the following script is used for that: cut -d, -f 1 foo.csv > foo.txt A typical result would yield : A12 A45 B11 B67 What needs to happen in addition is that two columns... (5 Replies)
Discussion started by: figaro
5 Replies

Featured Tech Videos