Convert column to quote and comma separated row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert column to quote and comma separated row
# 1  
Old 10-30-2014
Convert column to quote and comma separated row

Hi,

I have a list of tables in a file.txt
Code:
C_CLAIM
 C_HLD
 C_PROVIDER

I want the output to be

'C_CLAIM','C_HLD','C_PROVIDER'

Currently I'm usin awk and getting output which is almost correct but still has minor defects

awk -vORS="','" '{ print $1 }' file.txt


The output of above command is

C_CLAIM','C_HLD','C_PROVIDER','


If you notice the first ' is missing and there is an extra ,' at the end.
Also a new line needs to be added at the end

I have other ways to complete this but all require multiple lines and creating temp files.
Any cleaner method will be appreciated.

Last edited by Corona688; 10-30-2014 at 01:57 PM..
# 2  
Old 10-30-2014
Hello Wahi80,

Following may help you in same.

Code:
awk -vs1="'" '{S=S?S OFS s1 $0 s1:s1 $0 s1} END{print S}' OFS=,  Input_file

Output will be as follows.

Code:
'C_CLAIM','C_HLD','C_PROVIDER'

Thanks,
R. Singh
These 2 Users Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 10-30-2014
You can do without saving string

Code:
awk '{printf("%s'\''%s'\''",NR==1?"":OFS,$1)}END{print ""}' OFS=',' infile

---------- Post updated at 10:31 PM ---------- Previous update was at 10:25 PM ----------

Using Octal value
Code:
$ awk '{printf("%s\047%s\047",NR==1?"":OFS,$1)}END{print ""}' OFS=','  infile
'C_CLAIM','C_HLD','C_PROVIDER'

Using Hex value
Code:
$ awk '{printf("%s\x27%s\x27",NR==1?"":OFS,$1)}END{print ""}' OFS=','  infile
'C_CLAIM','C_HLD','C_PROVIDER'

Octal value I will recommend
# 4  
Old 10-30-2014
Try:
Code:
awk '{printf fmt,$1}' fmt="'%s'\n" file | paste -sd, -

# 5  
Old 10-30-2014
If you want to do it all in one invocation of awk and don't want to worry about the encoding of the single quote character, try:
Code:
awk -v q="'" '
NR!=1 {	printf("%s,", p) }
{	p = q $1 q }
END {	print p }' file.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert fixed value fields to comma separated values

Hi All, Hope you are doing Great!!!. Today i have came up with a problem to say exactly it was for performance improvement. I have written code in perl as a solution for this to cut in specific range, but it is taking time to run for files thousands of lines so i am expecting a sed... (9 Replies)
Discussion started by: mad man
9 Replies

2. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

3. Shell Programming and Scripting

Insert single quote on every word separated by comma

Hello, I have a text file as:-ABC BCD CDF DEF EFGI need to convert as 'ABC', 'BCD', 'CDF', 'DEF', 'EFG' using a unix command anybody can help me out on this. Regards, Jas Please wrap all code, files, input & output/errors in CODE tags. It makes them easier to read and preserves... (12 Replies)
Discussion started by: jassi10781
12 Replies

4. Shell Programming and Scripting

Convert comma separated file to fix length

Hi, I am converting a comma separated file to fixed field lenght and I am using that: COLUMNS="25 24 67 26 39 63 20 34 35 14 397" ( cat $indir/input_file.dat | \ $AWK -v columns="$COLUMNS" ' BEGIN { FS=","; OFS=""; split(columns, arr, " "); } { for(i=1; i<=NF;... (5 Replies)
Discussion started by: apenkov
5 Replies

5. UNIX for Dummies Questions & Answers

[solved] Comma separated values to space separated

Hi, I have a large number of files which are written as csv (comma-separated values). Does anyone know of simple sed/awk command do achieve this? Thanks! ---------- Post updated at 10:59 AM ---------- Previous update was at 10:54 AM ---------- Guess I asked this too soon. Found the... (0 Replies)
Discussion started by: lost.identity
0 Replies

6. Shell Programming and Scripting

convert row to column with respect of first column.

Input file A.txt :- C2062 -117.6 -118.5 -117.5 C5145 0 0 0 C5696 0 0 0 Output file B.txt C2062 X -117.6 C2062 Y -118.5 C2062 Z -117.5... (4 Replies)
Discussion started by: asavaliya
4 Replies

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

8. Shell Programming and Scripting

Deleting a column in multiple files that are comma separated

Hi, I have a directory that contains say 100 files named sequencially like input_1.25_50_C1.txt input_1.25_50_C2.txt input_1.25_50_C3.txt input_1.25_50_C4.txt .. .. .. input_1.25_50_C100.txt an example of the content in each of the file is: "NAME" "MEM.SHIP" "cgd1_10" "cgd1_10"... (9 Replies)
Discussion started by: Lucky Ali
9 Replies

9. Shell Programming and Scripting

Converting Column values to comma delimted single Row

I have a requirement in which i have to read a file which has multiple columns seperated by a pipe "|" from this i have to read each column values seperately and create a comma seperated row for the column and write to another file. eg: Input file: ColA ColB 1 2 2 x 3 y... (5 Replies)
Discussion started by: nvuradi
5 Replies

10. UNIX Desktop Questions & Answers

Unix Comma Separated to Excel Column

I would like to copy 2 parts of a csv file from Unix to an XL sheet. However to save time I do not want to format the column ever time I cut and paste into XL(Text2Column). I've used awk -F, '{Print $1, $2....}'. Is there a script or code that can automatically format the csv for XL columns? ... (3 Replies)
Discussion started by: ravzter
3 Replies
Login or Register to Ask a Question