Add a field separator (comma) inside a line of a CSV file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Add a field separator (comma) inside a line of a CSV file
# 1  
Old 06-11-2014
Add a field separator (comma) inside a line of a CSV file

Hi... I can't find my little red AWK book and it's been a long while since I've awk'd. But I need to take a CSV file and convert the first word of the fifth field to its own field by replacing a space with a comma.

This is for importing a spreadsheet of issues into JIRA...

Example:

a line in the input file:
Code:
Field 1,Field 2 is here,Field 3 is a bit more verbose,Field 4,Fifth field,Field 6

I'd like to get out (the comma between 'Fifth' and 'field'):
Code:
Field 1,Field 2 is here,Field 3 is a bit more verbose,Field 4,Fifth,field,Field 6

In essence, I need to find the 4th comma then print everything up to the end of the word that follows the 4th comma, insert a comma, and then print the rest of the line.

Thanks...
Moderator's Comments:
Mod Comment What was described as "a line" was two lines in both samples. I hope I did the right thing by joining both of those sets of lines and adding CODE tags.

Last edited by Don Cragun; 06-11-2014 at 06:58 PM.. Reason: Add CODE tags.
# 2  
Old 06-11-2014
Try:
Code:
awk -F, '
{sub(/ /, ",", $5)}
1' file

Ignore this suggestion, it forgot to set OFS.

Last edited by Don Cragun; 06-11-2014 at 08:00 PM.. Reason: Note shortcomings...
# 3  
Old 06-11-2014
Like that?

Code:
awk 'gsub(" ", ",", $5)' FS=, OFS=, file


Last edited by Don Cragun; 06-11-2014 at 07:09 PM.. Reason: Change QUOTE tags to CODE tags.
This User Gave Thanks to Aia For This Post:
# 4  
Old 06-11-2014
(thanks Mod for adding the code tags -- it wasn't "code" per say so it didn't dawn on me. I'll be better, promise!)

---------- Post updated at 03:11 PM ---------- Previous update was at 03:02 PM ----------

Quote:
Originally Posted by Aia
Like that?
it's close, but it replaces all subsequent spaces with $5 as well...
# 5  
Old 06-11-2014
Quote:
Originally Posted by Tawpie
(thanks Mod for adding the code tags -- it wasn't "code" per say so it didn't dawn on me. I'll be better, promise!)

---------- Post updated at 03:11 PM ---------- Previous update was at 03:02 PM ----------



it's close, but it replaces all subsequent spaces with $5 as well...
Take the g out of gsub
Code:
awk 'sub(" ", ",", $5)' FS=, OFS=, file

# 6  
Old 06-11-2014
Quote:
Originally Posted by Tawpie
(thanks Mod for adding the code tags -- it wasn't "code" per say so it didn't dawn on me. I'll be better, promise!)
Please use CODE tags for sample input, output, and code. The CODE tags preserve the differences between spaces and tabs and keep multiple adjacent spaces distinct. Without the CODE tags, long input lines can be split and whitespace characters can be coalesced into single spaces.

If your input contains lines where the 5th field does not contain any spaces (and you still want to add a comma to create an empty new 6th field before the original 6th field), you could try:
Code:
awk -F, 'sub(/ |$/, ",", $5)' OFS=, file

With the input:
Code:
Field 1,Field 2 is here,Field 3 is a bit more verbose,Field 4,Fifth field,Field 6
Field 1,Field 2 is here,Field 3 is a bit more verbose,Field 4,Fifth field more,Field 6
Field 1,Field 2 is here,Field 3 is a bit more verbose,Field 4,Fifth,Field 6
Field 1,Field 2 is here,Field 3 is a bit more verbose,Field 4,,Field 6

it produces the output:
Code:
Field 1,Field 2 is here,Field 3 is a bit more verbose,Field 4,Fifth,field,Field 6
Field 1,Field 2 is here,Field 3 is a bit more verbose,Field 4,Fifth,field more,Field 6
Field 1,Field 2 is here,Field 3 is a bit more verbose,Field 4,Fifth,,Field 6
Field 1,Field 2 is here,Field 3 is a bit more verbose,Field 4,,,Field 6

This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 06-11-2014
I thought that would work too but it doesn't do anything without being global.

I'm really looking at the 8th field but didn't want to type that much. Here's real input and output.

Code:
1,Improvement,2014-05-20,5/20/14,2,CREATED,creatorUN,assigneeUN,aPL5 v1 RC22 > Treatments > View Report,The text for longer treatments is being cut off on the right margin., ,aPL5 v1.2.0 RC22 Event History Log Text

I want to insert the comma between the first aPL5 and the space that follows the 5. Without gsub I get

Code:
1,Improvement,2014-05-20,5/20/14,2,CREATED,creatorUN,assigneeUN,aPL5 v1 RC22 > Treatments > View Report,The text for longer treatments is being cut off on the right margin., ,aPL5 v1.2.0 RC22 Event History Log Text


Last edited by Tawpie; 06-11-2014 at 08:14 PM.. Reason: typo in thought...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can awk ignore the field delimiter like comma inside a field?

We have a csv file as mentioned below and the requirement is to change the date format in file as mentioned below. Current file (file.csv) ---------------------- empname,date_of_join,dept,date_of_resignation ram,08/09/2015,sales,21/06/2016 "akash,sahu",08/10/2015,IT,21/07/2016 ... (6 Replies)
Discussion started by: gopal.biswal
6 Replies

2. Shell Programming and Scripting

Use two field separator in the same line and print them

Hi Guys, I have the file --- HOST_NAME,data_coleta,data_carga,CPU_util,CPU_idle,run_queue,memory,MEMORY_SYSTEM,MEMORY_TOTAL,MEMORY_SWAPIN,MEMORY_SWAPOUT,DISK_READ,DISK_WRITE,DISK_IO,NET_IN_PACKET, NET_OUT_PACKET... (4 Replies)
Discussion started by: antoniorajr
4 Replies

3. Shell Programming and Scripting

How can i comma-delimited last field in line?

Awk gurus, Greatly appreciate for any kind of assistance from the expert community Input line: abc,11.22.33.44,xyz,7-8-9-10 pqr,111.222.333.444,wxy,1-2-3 def,22.33.44.55,stu,7-8 used the gsub function below but it changes all of the "-" delimiter: awk 'gsub("-",",")' Desired... (4 Replies)
Discussion started by: ux4me
4 Replies

4. Shell Programming and Scripting

CSV file column separator

Hi, I have a CSV file of 40 columns with "," as delimiter. I want to assign the value of each column to a variable. But some of the columns content inside contains "," so how can i split the columns and assign it to a variable. Regards, ARASU. (1 Reply)
Discussion started by: Arasu123
1 Replies

5. Shell Programming and Scripting

Compare two CSV files and put the difference in third file with line no,field no and diff value.

I am having two csv files i need to compare these files and the output file should have the information of the differences at the field level. For Example, File 1: A,B,C,D,E,F 1,2,3,4,5,6 File 2: A,C,B,D,E,F 1,2,4,5,5,6 out put file: (12 Replies)
Discussion started by: karingulanagara
12 Replies

6. Shell Programming and Scripting

Need Help - comma inside double quote in comma separated csv,

Hello there, I have a comma separated csv , and all the text field is wrapped by double quote. Issue is some text field contain comma as well inside double quote. so it is difficult to process. Input in the csv file is , 1,234,"abc,12,gh","GH234TY",34 I need output like below,... (8 Replies)
Discussion started by: Uttam Maji
8 Replies

7. Shell Programming and Scripting

how to convert comma delimited file to tab separator

Hi all, How can i convert comma delimited .csv file to tab separate using sed command or script. Thanks, Krupa (4 Replies)
Discussion started by: krupasindhu18
4 Replies

8. Shell Programming and Scripting

Removing comma "," in a field value in csv file

Hi, I have csv file with records as below. Now i have remove any comma in the filed value because that creates problem when i feed this file to an application. for example below are two sample records, the second record have a comma in "Salesforce.com, Inc." field, now i have to remove this... (13 Replies)
Discussion started by: anaga
13 Replies

9. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

10. Shell Programming and Scripting

How to add text to a field within a csv file

I have a csv file which has three columns mem no. name surname 1234 John Smith 12345 John Doe I want to change the mem no. to add TF to the mem no. field i.e. mem no. name surname 1234TF John Smith 12345TF John Doe How do you do this for all records in the file? (3 Replies)
Discussion started by: Pablo_beezo
3 Replies
Login or Register to Ask a Question