Insert single quote on every word separated by comma


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert single quote on every word separated by comma
# 8  
Old 07-16-2015
Unix sed:
Code:
sed -e ':L1' -e N -e '$!bL1' -e "s/.*/'&'/;s/\n/','/g" file

Read the whole file into the memory buffer, then
insert a ' at the beginning and end of the buffer,
replace all intermediate \n characters by ','.
Variant2, use the hold buffer:
Code:
sed -e '1h;1!H;$!d;g' -e "s/.*/'&'/;s/\n/','/g" file

Variant3, converted RudiC solution to Unix sed, also using the hold buffer:
Code:
sed 's/^/'\''/;s/$/'\''/;1h;1!H;$!d;x;s/\n/,/g' file


Last edited by MadeInGermany; 07-16-2015 at 03:39 PM.. Reason: variant2,3
# 9  
Old 07-16-2015
Code:
sed "s/.*/'&'/" file | paste -sd, -




--
to put it in a variable, use command substitution:
Code:
var=$(sed "s/.*/'&'/" file | paste -sd, -)

This User Gave Thanks to Scrutinizer For This Post:
# 10  
Old 07-16-2015
and the Oscar goes to
Quote:
Originally Posted by Scrutinizer
...
Code:
var=$(sed "s/.*/'&'/" file | paste -sd, -)

Smilie
# 11  
Old 07-16-2015
In pure bash you could try:

Code:
# Change IFS to Support white space on lines
IFS=$'\n'
printf -v var "'%s', " $(< file)
IFS=$' \t\n'

# remove trailing ", "
var="${var%, }"

echo "$var"

For input:

Code:
ABC DEF
GHI
JKL
MNO

Produces 'ABC DEF', 'GHI', 'JKL', 'MNO'



for output 'ABC', 'DEF', 'GHI', 'JKL', 'MNO' (which the thread subject seems to imply)

simply use:
Code:
printf -v var "'%s', " $(< file)
echo "${var%, }"


Last edited by Chubler_XL; 07-16-2015 at 05:29 PM..
# 12  
Old 07-16-2015
Another shell solution:
Code:
var=$( while read L; do printf "%s'%s'" "$sep" "$L"; sep=", "; done < file )

Translating to awk saves the explicit loop:
Code:
var=$( awk '{printf "%s'\''%s'\''",sep,$0; sep=", "}' file )

# 13  
Old 07-16-2015
Another pure bash/ksh93/zsh solution (does not require changing IFS):
Code:
rec=$(<file); var="'${rec//$'\n'/','}'"



--
to print:
Code:
rec=$(<fname); printf "%s\n" "'${rec//$'\n'/','}'"







--
Quote:
Originally Posted by MadeInGermany
and the Oscar goes to

Smilie
Smilie

Last edited by Scrutinizer; 07-16-2015 at 11:28 PM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Insert a single quote in front of a line in vi editor

Hello Gurus, I wanted to put a single quote in every where starting with /oradata, and at the end with .dbf. For example I have one line as below: alter database rename datafile /oradata/test.dbf to /oradata_new/test.dbf I wanted as below alter database rename datafile '/oradata/test.dbf' to... (3 Replies)
Discussion started by: pokhraj_d
3 Replies

3. Shell Programming and Scripting

Replacing all but the first and last double quote in a line with a single quote with awk

From: 1,2,3,4,5,This is a test 6,7,8,9,0,"This, is a test" 1,9,2,8,3,"This is a ""test""" 4,7,3,1,8,"""" To: 1,2,3,4,5,This is a test 6,7,8,9,0,"This; is a test" 1,9,2,8,3,"This is a ''test''" 4,7,3,1,8,"''"Is there an easy syntax I'm overlooking? There will always be an odd number... (5 Replies)
Discussion started by: Michael Stora
5 Replies

4. Shell Programming and Scripting

Convert column to quote and comma separated row

Hi, I have a list of tables in a file.txt 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... (4 Replies)
Discussion started by: wahi80
4 Replies

5. Shell Programming and Scripting

Make multiple lines into single quoted comma separated Linux

Hi, I want to change a file file1.txt: 1234 3456 2345 6789 3456 2333 4444 As, file2.txt in Linux: '1234','3456','2345','6789','3456','2333','4444' Could someone please help me. (Single liner sed, awk will be welcome!) (7 Replies)
Discussion started by: wiweq05
7 Replies

6. UNIX for Dummies Questions & Answers

Insert single quote and commas

Hi All, I have a set of data as below : XS012371378 Raj 23-09-12 SH128238948 Andrew 24-08-12 CH273712399 Walsh 12-10-12 JK7249923893 Nick 10-02-13 JP6383791389 Braslin 30-12-13 I want the first column to be extracted separately. I can get this using awk. awk '{print $1}' file_name ... (3 Replies)
Discussion started by: Nand Kishor
3 Replies

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

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

9. Shell Programming and Scripting

How to insert a single quote to each record

I have a file as: 1 New used 1 used New I need o/p as: '1' 'New' 'used' '1' 'used' 'New' (12 Replies)
Discussion started by: karumudi7
12 Replies

10. Shell Programming and Scripting

Regex in grep to match all lines ending with a double quote (") OR a single quote (')

Hi, I've been trying to write a regex to use in egrep (in a shell script) that'll fetch the names of all the files that match a particular pattern. I expect to match the following line in a file: Name = "abc" The regex I'm using to match the same is: egrep -l '(^) *= *" ** *"$' /PATH_TO_SEARCH... (6 Replies)
Discussion started by: NanJ
6 Replies
Login or Register to Ask a Question