Enclose String in single quote


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Enclose String in single quote
# 1  
Old 02-26-2019
Enclose String in single quote

I have a comma separated file which contains data like;

File header:
Code:
ID_WVR,SAK_WVR_SVC,DSC_WVR_WVC,SAK_PROCEDURE,CODES,CDE_PROC_MOD ,CDE_PROC_MOD_2 ,CDE_PROC_MOD_3

File Detail:
Code:
AMR,5100,Total Services,305,D0120,,,
AMR,5101,Periodic Services,40702,H2011,U1,,
AMR,5112,Day Services,30616,T2020,U1,HQ,
AMR,5112,Day Services,30616,T2020,U6,UA,In home day services moved under day services on 3/30/15
AMR,5378,Supported Services,40876,T2032,U1,REMOVE,Max rate sheet: end dated 2006

I want to enclose all those strings within single quotes which contain alphanumeric characters. I do not want to enclose data which contain numbers only. For example;

Code:
'AMR',5100,'Total Services',305,'D0120',,,
'AMR',5112,'Day Services',30616,'T2020','U6','UA','In home day services moved under day services on 3/30/15'

I wrote below command which is working for 1st row but it do not work when I am adding more fields, like 2nd, 3rd etc. rows have more alphanumeric data.

Code:
2s/\([A-Z]*\)\([,0-9]*\)\([A-Za-z ]*\)\([,0-9]*\)\([A-Z0-9]*\)\([,.*]\)/'\1'\2'\3'\4'\5'\6/

I am looking for a command which recognize the alphanumeric values and enclose the string with single quote, recursively.

Thanks,
Rohit
# 2  
Old 02-26-2019
When you show us a command that you've used to try to get something to work, we generally expect to see a command name somewhere. All that you showed us is a something that might be an ed, ex, or sed substitute command. Obviously, a shell can't interpret an editor substitute command without knowing what utility you want to use to interpret that command.

I generally find awk easier to work with when I'm dealing with a varying number of fields and there is no reason to believe that the same fields in all lines will meet the same criteria. I'm also not sure why you bothered to show us a file header when it doesn't appear anywhere in your sample input files nor in your desired output. But, if one had a file named file that contained:
Code:
ID_WVR,SAK_WVR_SVC,DSC_WVR_WVC,SAK_PROCEDURE,CODES,CDE_PROC_MOD ,CDE_PROC_MOD_2 ,CDE_PROC_MOD_3
AMR,5100,Total Services,305,D0120,,,
AMR,5101,Periodic Services,40702,H2011,U1,,
AMR,5112,Day Services,30616,T2020,U1,HQ,
AMR,5112,Day Services,30616,T2020,U6,UA,In home day services moved under day services on 3/30/15
AMR,5378,Supported Services,40876,T2032,U1,REMOVE,Max rate sheet: end dated 2006

and one had a script similar to:
Code:
#!/bin/ksh
awk -v sq="'" '
BEGIN {	FS = OFS = ","
}
{	for(i = 1; i <= NF; i++)
		printf("%s%s", ($i ~ /^[0-9]*$/) ? $i : sq $i sq,
		    (i == NF) ? ORS : OFS)
}' file

one might get output similar to:
Code:
'ID_WVR','SAK_WVR_SVC','DSC_WVR_WVC','SAK_PROCEDURE','CODES','CDE_PROC_MOD ','CDE_PROC_MOD_2 ','CDE_PROC_MOD_3'
'AMR',5100,'Total Services',305,'D0120',,,
'AMR',5101,'Periodic Services',40702,'H2011','U1',,
'AMR',5112,'Day Services',30616,'T2020','U1','HQ',
'AMR',5112,'Day Services',30616,'T2020','U6','UA','In home day services moved under day services on 3/30/15'
'AMR',5378,'Supported Services',40876,'T2032','U1','REMOVE','Max rate sheet: end dated 2006'

if one ran that script in the directory containing that file.

Is that what you were trying to do?

And, of course, since you didn't bother telling us what operating system and shell you're using, one should note that if you want to try this on a Solaris/SunOS system, you'll need to change awk in that script to /usr/xpg4/bin/awk or nawk. This was tested using a Korn shell, but should work with any shell that is based on Bourne shell syntax. (Who knows, it might even work with csh or one of its derivatives.)
# 3  
Old 02-26-2019
I really apologize for not giving complete detail about command, OS, Shell etc.

I tried this solution and it worked as expected. I need to do some work around on my file because all rows are not similar.

Thank you for your help.
# 4  
Old 02-27-2019
Code:
sed -r 's/[^,]+/\x27&\x27/g;s/\x27([0-9]+)\x27/\1/g'

This User Gave Thanks to nezabudka For This Post:
# 5  
Old 02-27-2019
Quote:
Originally Posted by nezabudka
Code:
sed -r 's/[^,]+/\x27&\x27/g;s/\x27([0-9]+)\x27/\1/g'

I am not 100% sure if every sed understands this form of character-encoding. Here is a solution which may look a bit longer but will work with every sed:

Code:
sed 's/\([0-9]*[^0-9,][^0-9,]*[0-9]*\),/\'\1\',/g
     s/\([0-9]*[^0-9,][^0-9,]*[0-9]*\)$/\'\1\',/' /path/to/file

I get the notion from your own script that you want to exclude the first line (header) from the processing. If so, modify the above like this:

Code:
sed '1 !{ s/\([0-9]*[^0-9,][^0-9,]*[0-9]*\),/\'\1\',/g
          s/\([0-9]*[^0-9,][^0-9,]*[0-9]*\)$/\'\1\',/
        }' /path/to/file

Also notice that you may want to not repeat the operation if there are already single quotes in place, i.e. you may want to transform

Code:
foo,bar,999,,       ==> 'foo','bar',999,,

but probably not want this:

Code:
'foo','bar',999,,       ==> ''foo'',''bar'',999,,

and instead leave it unchanged. If this is the case you should change the sed-script this way:

Code:
sed '1 !{ s/\'*\([0-9]*[^0-9,][^0-9,]*[0-9]*\)\'*,/\'\1\',/g
          s/\'*\([0-9]*[^0-9,][^0-9,]*[0-9]*\)\'*$/\'\1\',/
        }' /path/to/file

You may want to trim trailing blanks first, otherwise you might get this (blanks encoded as "<b>"):

Code:
foo,bar,999,,<b><b>     ==> 'foo','bar',999,,'<b><b>'

which is probably not what you want. Therefore a last modification (replace "<b>" and "<t>" with literal blanks/tabs) :

Code:
sed 's/[<b><t>]*$//
     1 !{ s/\'*\([0-9]*[^0-9,][^0-9,]*[0-9]*\)\'*,/\'\1\',/g
          s/\'*\([0-9]*[^0-9,][^0-9,]*[0-9]*\)\'*$/\'\1\',/
        }' /path/to/file

I hope this helps.

bakunin
These 2 Users Gave Thanks to bakunin 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

Need to split string on single quote as delimiter

I have a variable that contains the following string: FPATH=-rw-rw-r-- 1 user1 dba 0 Aug 7 13:14 /app/F11.3/app/cust/exe/filename1.exe' -rw-rw-r-- 1 user1 dba 0 Aug 19 10:09 /app/app/F11.3/app/cust/sql/33211.sql' -rw-r--r-- 1 user1 dba 0 Aug 6 17:20 /app/F11.2/app/01/mrt/file1.mrt' I... (7 Replies)
Discussion started by: mohtashims
7 Replies

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

3. Shell Programming and Scripting

Replace double quotes with a single quote within a double quoted string

Hi Froum. I have tried in vain to find a solution for this problem - I'm trying to replace any double quotes within a quoted string with a single quote, leaving everything else as is. I have the following data: Before: ... (32 Replies)
Discussion started by: pchang
32 Replies

4. Shell Programming and Scripting

How to capture a string enclose by a pattern within a file?

Hi all, My file :test.txt just like this: ........................... From: 333:123<sip:88888888888@bbbb.com To: <sip:123456@aaaaa.com ......................... I want a script to capture the string between sip: & @ Expect output: 88888888888 123456 Please help! (4 Replies)
Discussion started by: Alex Li
4 Replies

5. Shell Programming and Scripting

need to enclose a string in quotes

I have a script which I call and pass a text string to it. This string is then is assigned to a variable in the script. I then call another script and pass that variable to the second script, but when I do, the quotes are lost and the second script gets a total of three variables 'my', 'lovely' and... (3 Replies)
Discussion started by: iskatel
3 Replies

6. Shell Programming and Scripting

single quote replacement

hi all, i have a data in the file which of the formate : 100,102,103 and the required formate is \'100\',\'102\',\'103 Idealy we need to replace , with \',\' Regards arkesh (2 Replies)
Discussion started by: arkeshtk
2 Replies

7. Shell Programming and Scripting

Replace single quote with two single quotes in perl

Hi I want to replace single quote with two single quotes in a perl string. If the string is <It's Simpson's book> It should become <It''s Simpson''s book> (3 Replies)
Discussion started by: DushyantG
3 Replies

8. UNIX for Dummies Questions & Answers

find single quote in a string and replace it

Hi, I have variable inside shell script - from_item. from_item = 40.1'1/16 i have to first find out whether FROM_ITEM contains single quote('). If yes, then that need to be replace with two quotes (''). How to do it inside shell script? Please note that inside shell script........ (4 Replies)
Discussion started by: yogichavan
4 Replies

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

10. Shell Programming and Scripting

single quote

Hi I have a shell script with many lines as below: comment on column dcases.proj_seq_num is dcases_1sq; .... .... I want the above script to be as below: comment on column dcases.proj_seq_num is 'dcases_1sq'; I want to have single quotes like that as above for the entire shell... (2 Replies)
Discussion started by: dreams5617
2 Replies
Login or Register to Ask a Question