Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Removing commas from CSV file Post 303009413 by preema on Thursday 14th of December 2017 06:56:02 AM
Old 12-14-2017
Removing commas from CSV file

Hi
I'm creating a sh script to generate a csv file. The CSV contains the values from a sql table.
The content looks this:

Code:
 a,b,c,c2,c3,,,,,,,,,,,d,e

I have some code that can separate the fields using the comma as delimiter, but some values actually contain commas, such as c,c2,c3,,,,,,,,,

This field needs to be taken as one field and the trailing commas after c3 must be removed.
Can anybody please help in doing this ?

Thanks in advance!!

Last edited by rbatte1; 12-14-2017 at 09:55 AM.. Reason: Added CODE/ICODE tags
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

removing commas from text file

Dear all I have a file which looks like this xxxxxxxxxxxxxx,xxx,xxxxxxxxxx xxxxxxxxxxxxxx,xxx,xxxxxxxxxx etc basically 14 characters then a comma, three characters, then a comma then 10 characters. We are uploading this file to our mainframe and they want the commas removed, so it... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

2. HP-UX

Padding zeros after removing commas in file

Hi Gurus, There is a ASCII file in which a comma is used as a seperator for the amount field when the amount exceed seven digits: e.g. 0001300,000. Now, this comma needs to be removed from this field, after padding leading zeros (to maintain the ASCII positions) e.g. 00001300000.... (1 Reply)
Discussion started by: pranag21
1 Replies

3. Shell Programming and Scripting

replacing commas with tilde in csv file.

hello all, i have a comma delimited file. i want to replace the commas in the file with the tilde symbol using sed. how can i do this? thanks. (4 Replies)
Discussion started by: femig
4 Replies

4. Shell Programming and Scripting

Removing commas within semicolon in a flat file

Hi , Im relatively new to unix and have to process a comma serparated flat file . I recieve some of the fields in double quotes and i want to remove it .. INPUT ==== filed1,field2,field3,"fie,ld4" OUTPUT ===== field1,field2,field3,"field4" can anyone tell me how to achieve... (10 Replies)
Discussion started by: r_t_1601
10 Replies

5. Shell Programming and Scripting

Removing commas within semicolon in a flat file

i am recieving a flat file ( comma seperated ) with comma in between double quotes in any of the source fields . i need to remove the comma in double quotes and process the file thereafter fields in file ========= col1,col2,col3,col4 input can be any of the followng... (31 Replies)
Discussion started by: r_t_1601
31 Replies

6. Shell Programming and Scripting

Remove duplicate commas after exporting excel file to csv

Hello everyone I'm new here and this is my first post so first of all I want to say that this is a great forum and I have managed to found most of my answers in these forums : ) So with that I ask you my first question: I have an excel file which I saved as a csv. However the excel file... (3 Replies)
Discussion started by: Spunkerspawn
3 Replies

7. Shell Programming and Scripting

Adding Extra Commas to a CSV file

Trying in this forum. Not sure if it is permitted.... but in need of help. Please find the requirements in the below link. https://www.unix.com/unix-dummies-questions-answers/191503-add-extra-commas-csv-file-2.html#post302665179 Thanks in Advance. (1 Reply)
Discussion started by: chillblue
1 Replies

8. UNIX for Dummies Questions & Answers

To Add extra commas to a CSV file using 2 files...

Hi , Based on my previous requirement the code works fine for comma as delimiter. Now my Req is widened up a bit .. There will be two set of files .. one with comma as delimiter and other with semi-colon ; as delimiter. Second Sample file. With Double Quotes (Semi-Colon... (1 Reply)
Discussion started by: chillblue
1 Replies

9. UNIX for Dummies Questions & Answers

To Add extra commas to a CSV file.

Hi All, I got this requirement to process a complex CSV file. Eg File. 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 Line 1: Name:,XYz,,... (12 Replies)
Discussion started by: chillblue
12 Replies

10. Shell Programming and Scripting

How to remove unwanted commas from a .csv file?

how to remove unwanted commas from a .csv file Input file format "Server1","server-PRI-Windows","PRI-VC01","Microsoft Windows Server 2012, (64-bit)","Powered On","1,696.12","server-GEN-SFCHT2-VMS-R013,server-GEN-SFCHT2-VMS-R031,server-GEN-SFCHT2-VMS-R023"... (5 Replies)
Discussion started by: ranjancom2000
5 Replies
FGETCSV(3)								 1								FGETCSV(3)

fgetcsv - Gets line from file pointer and parse for CSV fields

SYNOPSIS
array fgetcsv (resource $handle, [int $length], [string $delimiter = ","], [string $enclosure = '"'], [string $escape = " DESCRIPTION
Similar to fgets(3) except that fgetcsv(3) parses the line it reads for fields in CSV format and returns an array containing the fields read. PARAMETERS
o $handle - A valid file pointer to a file successfully opened by fopen(3), popen(3), or fsockopen(3). o $length - Must be greater than the longest line (in characters) to be found in the CSV file (allowing for trailing line-end characters). It became optional in PHP 5. Omitting this parameter (or setting it to 0 in PHP 5.1.0 and later) the maximum line length is not limited, which is slightly slower. o $delimiter - The optional $delimiter parameter sets the field delimiter (one character only). o $enclosure - The optional $enclosure parameter sets the field enclosure character (one character only). o $escape - The optional $escape parameter sets the escape character (one character only). RETURN VALUES
Returns an indexed array containing the fields read. Note A blank line in a CSV file will be returned as an array comprising a single null field, and will not be treated as an error. Note If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem. fgetcsv(3) returns NULL if an invalid $handle is supplied or FALSE on other errors, including end of file. CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 5.3.0 | | | | | | | The $escape parameter was added | | | | | 5.1.0 | | | | | | | The $length is now optional. Default is 0, mean- | | | ing no length limit. | | | | | 4.3.5 | | | | | | | fgetcsv(3) is now binary safe | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 Read and print the entire contents of a CSV file <?php $row = 1; if (($handle = fopen("test.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); echo "<p> $num fields in line $row: <br /></p> "; $row++; for ($c=0; $c < $num; $c++) { echo $data[$c] . "<br /> "; } } fclose($handle); } ?> NOTES
Note Locale setting is taken into account by this function. If $LANG is e.g. en_US.UTF-8, files in one-byte encoding are read wrong by this function. SEE ALSO
str_getcsv(3), explode(3), file(3), pack(3), fputcsv(3). PHP Documentation Group FGETCSV(3)
All times are GMT -4. The time now is 05:13 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy