|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
[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
|
||||
|
||||
|
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 | ||
|
|
#4
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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 | ||
|
![]() |
| Tags |
| adding commas, csv |
| Thread Tools | Search this Thread |
| 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 |
|
|