To Add extra commas to a CSV file.

Tags
adding commas, beginners, csv, solved

 

 
Thread Tools Search this Thread
# 1  
Old 06-26-2012
[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 09:37 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 06-26-2012
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

# 3  
Old 06-26-2012
Or:
Code:
awk -F, 'NF=4' OFS=, file

This User Gave Thanks to Franklin52 For This Post:
zaxxon (06-26-2012)
# 4  
Old 06-26-2012
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
# 5  
Old 06-26-2012
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

# 6  
Old 06-27-2012
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.
# 7  
Old 06-27-2012
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

 

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
How to remove unwanted commas from a .csv file? ranjancom2000 Shell Programming and Scripting 5 10-05-2018 01:31 PM
Removing commas from CSV file preema UNIX for Beginners Questions & Answers 2 12-14-2017 09:02 AM
Need to Insert three extra columns in csv file ahmed.vaghar Shell Programming and Scripting 5 09-13-2015 02:57 PM
Shell script that should remove unnecessary commas between double quotes in CSV file mathias23 Shell Programming and Scripting 5 11-14-2014 01:55 PM
How to delete the commas in a .CSV file that are enclosed in a string with double quotes? dhruuv369 Shell Programming and Scripting 8 02-11-2014 07:30 PM
Randomly inserting extra columns into csv file Lokesha Shell Programming and Scripting 8 09-20-2013 02:53 PM
HELP with AWK or SED. Need to replace the commas between double quotes in CSV file shell_boy23 Shell Programming and Scripting 5 07-12-2012 05:54 AM
To Add extra commas to a CSV file using 2 files... chillblue UNIX for Dummies Questions & Answers 1 07-05-2012 09:15 AM
Adding Extra Commas to a CSV file chillblue Shell Programming and Scripting 1 07-05-2012 08:59 AM
Ascii Mode appending extra records to csv file developerpa Shell Programming and Scripting 3 06-12-2012 03:23 AM
awk for removing special characters and extra commas shruthidwh UNIX for Dummies Questions & Answers 2 09-19-2011 12:24 PM
shell script to remove extra commas from CSV outp file sreenath1037 Shell Programming and Scripting 1 04-29-2011 03:11 PM
Remove duplicate commas after exporting excel file to csv Spunkerspawn Shell Programming and Scripting 3 08-12-2010 10:13 AM
manipulating csv to add extra row neil546 Shell Programming and Scripting 3 07-16-2007 08:11 AM
replacing commas with tilde in csv file. femig Shell Programming and Scripting 4 11-13-2006 12:41 PM