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
# 1  
Old 07-16-2015
Insert single quote on every word separated by comma

Hello,

I have a text file as:-
Code:
ABC
BCD
CDF
DEF
EFG

I need to convert as 'ABC', 'BCD', 'CDF', 'DEF', 'EFG' using a unix command

anybody can help me out on this.

Regards,
Jas


Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags.
It makes them easier to read and preserves multiple spaces for indenting or fixed width data.

Last edited by rbatte1; 07-16-2015 at 12:23 PM.. Reason: CODE tags added for file
# 2  
Old 07-16-2015
It depends what you really want to do.

Do you want to convert this to an output file or string as 'ABC', 'BCD', 'CDF', 'DEF', 'EFG'

If so, you could:-
Code:
while read line
do
   my_var="$my_var, '$line'"
done < text_file

my_var="${my_var#, }"                # Trim off leading comma and space

echo "$my_var"


Does this meet your needs? If not, can you explain a bit more about what you do need.



Regards,
Robin

Last edited by rbatte1; 07-16-2015 at 12:34 PM..
# 3  
Old 07-16-2015
it would be good if its in string
# 4  
Old 07-16-2015
Hello jassi,

Following may help you in same, please use code tags as per forum rules too.
Code:
 awk -vs1="'" '{A=A?A OFS s1 $0 s1:s1 $0 s1} END{print A}' OFS=,  Input_file

Output will be as follows.
Code:
 'ABC','BCD','CDF','DEF','EFG'

EDIT: Adding one more solution a little different from above.
Code:
 awk -vs1="'" '{gsub(/^|$/,s1,$0);A=A?A OFS $0:$0} END{print A}' OFS=,  Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 07-16-2015 at 01:03 PM.. Reason: Added a little different solution too now
# 5  
Old 07-16-2015
Does the above (reading the input file text_file) not work for you?

The value is stored in $my_var at the end of these few lines and you can use it as you please.

What is the eventual purpose? Perhaps there is a smarter way to achieve your overall aim that we can help with.



Robin
# 6  
Old 07-16-2015
Try also this sed solution (need EREs!):
Code:
sed -rn 's/^|$/\o047/g;1h;1!H;${x;s/\n/,/gp}' file

# 7  
Old 07-16-2015
Each on new line, based in one of the answers above:
Code:
awk -vs1="'" '{gsub(/^|$/,s1,$0);A=A?A OFS $0:$0} END{print A}' OFS="\n" file


Last edited by Scrutinizer; 07-16-2015 at 03:33 PM.. Reason: code tags
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