Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Add a field separator (comma) inside a line of a CSV file Post 302905474 by Don Cragun on Wednesday 11th of June 2014 06:58:37 PM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
sact(1) 						      General Commands Manual							   sact(1)

NAME
sact - print current SCCS file editing activity SYNOPSIS
file ... DESCRIPTION
The command informs the user of any impending deltas to a named SCCS file. This situation occurs when has been previously executed without a subsequent execution of (see delta(1) and get(1)). If a directory is named on the command line, behaves as though each file in the directory were specified as a named file, except that non-SCCS files (last component of path name does not begin with and unreadable files are silently ignored. If a name of is given, the standard input is read with each line being taken as the name of an SCCS file to be pro- cessed. The output for each named file consists of five fields separated by spaces. Field 1 SID of a delta that currently exists in the SCCS file to which changes will be made to make the new delta. Field 2 SID for the new delta to be created. Field 3 Logname of the user making the delta (i.e., executed a for editing). Field 4 Date when was executed. Field 5 Time when was executed. DIAGNOSTICS
Use sccshelp(1) for explanations. SEE ALSO
delta(1), get(1), sccshelp(1), unget(1). STANDARDS CONFORMANCE
sact(1)
All times are GMT -4. The time now is 10:22 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy