Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-26-2012
Registered User
 
Join Date: Jun 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
[Solved] To Add extra commas to a CSV file.

Hi All,

I got this requirement to process a complex CSV file.

Eg File.

Code:
Line 1: Name:,XYz
Line 2: Age:,15
Line 3: Grade:,7
Line 4: 
Line 5: English, Maths, Science,Spanish
Line 6:10,11,13,14

As you can see the maximum column is 4 . The file i need to make is

Code:
Line 1: Name:,XYz,,
Line 2: Age:,15,,
Line 3: Grade:,7,,
Line 4: ,,,,
Line 5: English, Maths, Science,Spanish
Line 6:10,11,13,14

Please notice the extra commas for those lines which is less than 4 ...

This has to be performed in Unix... I have to find out the maximum column length and apply the commas for those lines which does not have them.

Thanks in Advance.

Last edited by Franklin52; 06-26-2012 at 08:37 AM.. Reason: Please use code tags for data and code samples
Sponsored Links
    #2  
Old 06-26-2012
itkamaraj's Avatar
^Kamaraj^
 
Join Date: Apr 2010
Posts: 3,025
Thanks: 33
Thanked 647 Times in 625 Posts

Code:
$ awk -F, -v OFS=, '{for(i=NF;i<=4;i++){$i=$i""}print}' a.txt
Line 1: Name:,XYz,,
Line 2: Age:,15,,
Line 3: Grade:,7,,
Line 4: ,,,
Line 5: English, Maths, Science,Spanish
Line 6:10,11,13,14

Sponsored Links
    #3  
Old 06-26-2012
Moderator
 
Join Date: Feb 2007
Location: The Netherlands
Posts: 7,500
Thanks: 73
Thanked 473 Times in 452 Posts
Or:

Code:
awk -F, 'NF=4' OFS=, file

The Following User Says Thank You to Franklin52 For This Useful Post:
zaxxon (06-26-2012)
    #4  
Old 06-26-2012
Registered User
 
Join Date: Jun 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks a ton for your reply.... itkamaraj and Franklin52.

I observe that the max column (comma) is assumed to be 4... but that is not in my case ..

The number of column is not fixed ... The file can have 3 or even 30 columns .... so the commas has to be placed for the length of values present in Line 5.


Thank you
Sponsored Links
    #5  
Old 06-26-2012
itkamaraj's Avatar
^Kamaraj^
 
Join Date: Apr 2010
Posts: 3,025
Thanks: 33
Thanked 647 Times in 625 Posts

Code:
$ column_size=$(awk -F, 'a<NF{a=NF}END{print a}' a.txt)
$ echo $column_size
7
$ awk -F, -v OFS=, -v c="$column_size" 'NF=c' a.txt 
Line 1: Name:,XYz,,,,,
Line 2: Age:,15,,,,,
Line 3: Grade:,7,,,,,
Line 4: ,,,,,,
Line 5: English, Maths, Science,Spanish,,,
Line 6:10,11,13,14,,,
a,b,c,d,e,f,g

Sponsored Links
    #6  
Old 06-27-2012
Registered User
 
Join Date: Jun 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Hi All,

I want the extra commas to be placed in a file and save it with the commas. The above help helped me to display the content with commas.

Please help me in adding the commas and saving the file. The number of commas is also not fixed ... the length of the max column has to be taken.

Thanks in advance.
Sponsored Links
    #7  
Old 06-27-2012
itkamaraj's Avatar
^Kamaraj^
 
Join Date: Apr 2010
Posts: 3,025
Thanks: 33
Thanked 647 Times in 625 Posts

Code:
$ column_size=$(awk -F, 'a<NF{a=NF}END{print a}' a.txt)
$ awk -F, -v OFS=, -v c="$column_size" 'NF=c' a.txt > output.txt

Sponsored Links
Closed Thread

Tags
adding commas, csv

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
To Add extra commas to a CSV file using 2 files... chillblue UNIX for Dummies Questions & Answers 1 07-05-2012 08:15 AM
Adding Extra Commas to a CSV file chillblue Shell Programming and Scripting 1 07-05-2012 07:59 AM
awk for removing special characters and extra commas shruthidwh UNIX for Dummies Questions & Answers 2 09-19-2011 11:24 AM
shell script to remove extra commas from CSV outp file sreenath1037 Shell Programming and Scripting 1 04-29-2011 02:11 PM
Inserting commas and replacing backslashes with commas kangaroo UNIX for Dummies Questions & Answers 3 11-11-2008 08:32 PM



All times are GMT -4. The time now is 04:34 PM.