How to add the single quotation marks for string (Alphabet) values?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-3
down vote

favorite












In file data is like below



Sample input:



123,xyz,456,abc


We need add single quotation for all string's only. First need check either number or string, For suppose first one is number not require to add the single quotation after comma next string need add the single quotation marks like below output.



123,'xyz',456,'abc'









share|improve this question























  • What command do you use to get this output? Simple echo or different tools?
    – Egor Vasilyev
    Oct 3 '17 at 7:57










  • Your question is unclear. You should edit it to include all relevant details. See How to Ask.
    – Anthony Geoghegan
    Oct 3 '17 at 8:31











  • Are your numbers only positive decimal integer numbers without leading +/- sign nor surrounding spaces?
    – Stéphane Chazelas
    Oct 3 '17 at 8:32










  • What qualifies as a string, what qualifies as a number? What do you want to happen for something that qualifies as neither? For instance, what should be the output for an input like -1,0x2a,,a1,1e6, 2 ,foo,x-y, z,++?
    – Stéphane Chazelas
    Oct 3 '17 at 14:39















up vote
-3
down vote

favorite












In file data is like below



Sample input:



123,xyz,456,abc


We need add single quotation for all string's only. First need check either number or string, For suppose first one is number not require to add the single quotation after comma next string need add the single quotation marks like below output.



123,'xyz',456,'abc'









share|improve this question























  • What command do you use to get this output? Simple echo or different tools?
    – Egor Vasilyev
    Oct 3 '17 at 7:57










  • Your question is unclear. You should edit it to include all relevant details. See How to Ask.
    – Anthony Geoghegan
    Oct 3 '17 at 8:31











  • Are your numbers only positive decimal integer numbers without leading +/- sign nor surrounding spaces?
    – Stéphane Chazelas
    Oct 3 '17 at 8:32










  • What qualifies as a string, what qualifies as a number? What do you want to happen for something that qualifies as neither? For instance, what should be the output for an input like -1,0x2a,,a1,1e6, 2 ,foo,x-y, z,++?
    – Stéphane Chazelas
    Oct 3 '17 at 14:39













up vote
-3
down vote

favorite









up vote
-3
down vote

favorite











In file data is like below



Sample input:



123,xyz,456,abc


We need add single quotation for all string's only. First need check either number or string, For suppose first one is number not require to add the single quotation after comma next string need add the single quotation marks like below output.



123,'xyz',456,'abc'









share|improve this question















In file data is like below



Sample input:



123,xyz,456,abc


We need add single quotation for all string's only. First need check either number or string, For suppose first one is number not require to add the single quotation after comma next string need add the single quotation marks like below output.



123,'xyz',456,'abc'






text-processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 3 '17 at 14:28









αғsнιη

15.7k92563




15.7k92563










asked Oct 3 '17 at 7:56









jai

41




41











  • What command do you use to get this output? Simple echo or different tools?
    – Egor Vasilyev
    Oct 3 '17 at 7:57










  • Your question is unclear. You should edit it to include all relevant details. See How to Ask.
    – Anthony Geoghegan
    Oct 3 '17 at 8:31











  • Are your numbers only positive decimal integer numbers without leading +/- sign nor surrounding spaces?
    – Stéphane Chazelas
    Oct 3 '17 at 8:32










  • What qualifies as a string, what qualifies as a number? What do you want to happen for something that qualifies as neither? For instance, what should be the output for an input like -1,0x2a,,a1,1e6, 2 ,foo,x-y, z,++?
    – Stéphane Chazelas
    Oct 3 '17 at 14:39

















  • What command do you use to get this output? Simple echo or different tools?
    – Egor Vasilyev
    Oct 3 '17 at 7:57










  • Your question is unclear. You should edit it to include all relevant details. See How to Ask.
    – Anthony Geoghegan
    Oct 3 '17 at 8:31











  • Are your numbers only positive decimal integer numbers without leading +/- sign nor surrounding spaces?
    – Stéphane Chazelas
    Oct 3 '17 at 8:32










  • What qualifies as a string, what qualifies as a number? What do you want to happen for something that qualifies as neither? For instance, what should be the output for an input like -1,0x2a,,a1,1e6, 2 ,foo,x-y, z,++?
    – Stéphane Chazelas
    Oct 3 '17 at 14:39
















What command do you use to get this output? Simple echo or different tools?
– Egor Vasilyev
Oct 3 '17 at 7:57




What command do you use to get this output? Simple echo or different tools?
– Egor Vasilyev
Oct 3 '17 at 7:57












Your question is unclear. You should edit it to include all relevant details. See How to Ask.
– Anthony Geoghegan
Oct 3 '17 at 8:31





Your question is unclear. You should edit it to include all relevant details. See How to Ask.
– Anthony Geoghegan
Oct 3 '17 at 8:31













Are your numbers only positive decimal integer numbers without leading +/- sign nor surrounding spaces?
– Stéphane Chazelas
Oct 3 '17 at 8:32




Are your numbers only positive decimal integer numbers without leading +/- sign nor surrounding spaces?
– Stéphane Chazelas
Oct 3 '17 at 8:32












What qualifies as a string, what qualifies as a number? What do you want to happen for something that qualifies as neither? For instance, what should be the output for an input like -1,0x2a,,a1,1e6, 2 ,foo,x-y, z,++?
– Stéphane Chazelas
Oct 3 '17 at 14:39





What qualifies as a string, what qualifies as a number? What do you want to happen for something that qualifies as neither? For instance, what should be the output for an input like -1,0x2a,,a1,1e6, 2 ,foo,x-y, z,++?
– Stéphane Chazelas
Oct 3 '17 at 14:39











2 Answers
2






active

oldest

votes

















up vote
2
down vote













echo ,123,xyz,456,,abc2 |
sed "s/[^,]*[^,0-9][^,]*/'&'/g;s/^,/'',/;s/,$/,''/;:1
s/,,/,'',/g;t1"
'',123,'xyz',456,'','abc2'


would enclose in quotes any comma-separated field that is not made exclusively of one or more decimal digits (that is empty fields like for lines that start (/^,/) or end (/,$/) with , or contain ,, and fields containing at least one non-digit (/[^,]*[^,0-9][^,]*/)).



If you want to recognise arbitrary decimal floating point numbers like -2, 1.23, 1e-2, that would become more complicated and you may want to resort to awk:



awk -F, -v OFS=, -v q="'" '

for (i = 1; i <= NF; i++)
if ($i !~ /^[[:blank:]]*[-+]?([0-9]+.?[0-9]*'


That changes:



abc,,0x20, +2.23E-5 , x y ,-2


to:



'abc','','0x20', +2.23E-5 ,' x y ',-2


If you wanted to get



'abc','','0x20', +2.23E-5 , 'x y' ,-2


instead, you'd do:



awk -F, -v OFS=, -v q="'" '

for (i = 1; i <= NF; i++)
if ($i !~ /^[[:blank:]]*[-+]?([0-9]+.?[0-9]*'





share|improve this answer






















  • not sure if OP had thought of so many corner cases! if empty fields are to be untouched and any field with at least one lowercase alphabet should be quoted, then I think this would work - echo ,123,xyz,456,,abc2 | sed 's/[^,]*[a-z][^,]*/x27&x27/g' ...
    – Sundeep
    Oct 3 '17 at 9:12

















up vote
1
down vote













With sed if your input data in each fields are only digits or alpha:



sed -E "s/[[:alpha:]]+/'&'/g" <<<"123,xyz,456,abc"


Or in complex mode.



sed -E "s/[0-9]*[[:alpha:]-]+[0-9]*/'&'/g" infile


Input:



123,xyz,456,abc
abc123,,a-b,5


Output:



123,'xyz',456,'abc'
'abc123',,'a-b',5





share|improve this answer






















  • That turns abc123,,a-b,5 into 'abc'123,,'a'-'b',5 instead of 'abc123','','a-b',5
    – Stéphane Chazelas
    Oct 3 '17 at 8:46











  • @StéphaneChazelas fixed that, but ,, in abc123,,1-n,5 is null, should be ,'', also?
    – Î±Ò“sнιη
    Oct 3 '17 at 9:09










  • Well, the OP's requirement are not clearly specified. I understood it as anything but numbers should be quoted, but there are plenty of other ways to interpret the question. It might be that the OP's input only contains either all-alpha or all-digit fields and we don't need to worry about empty fields or fields containing characters that are neither alpha nor digits or combination of alpha and digits.
    – Stéphane Chazelas
    Oct 3 '17 at 10:02






  • 1




    Thank you afshin exactly correct what i asked...i got the result...
    – jai
    Oct 3 '17 at 14:09











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f395773%2fhow-to-add-the-single-quotation-marks-for-string-alphabet-values%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













echo ,123,xyz,456,,abc2 |
sed "s/[^,]*[^,0-9][^,]*/'&'/g;s/^,/'',/;s/,$/,''/;:1
s/,,/,'',/g;t1"
'',123,'xyz',456,'','abc2'


would enclose in quotes any comma-separated field that is not made exclusively of one or more decimal digits (that is empty fields like for lines that start (/^,/) or end (/,$/) with , or contain ,, and fields containing at least one non-digit (/[^,]*[^,0-9][^,]*/)).



If you want to recognise arbitrary decimal floating point numbers like -2, 1.23, 1e-2, that would become more complicated and you may want to resort to awk:



awk -F, -v OFS=, -v q="'" '

for (i = 1; i <= NF; i++)
if ($i !~ /^[[:blank:]]*[-+]?([0-9]+.?[0-9]*'


That changes:



abc,,0x20, +2.23E-5 , x y ,-2


to:



'abc','','0x20', +2.23E-5 ,' x y ',-2


If you wanted to get



'abc','','0x20', +2.23E-5 , 'x y' ,-2


instead, you'd do:



awk -F, -v OFS=, -v q="'" '

for (i = 1; i <= NF; i++)
if ($i !~ /^[[:blank:]]*[-+]?([0-9]+.?[0-9]*'





share|improve this answer






















  • not sure if OP had thought of so many corner cases! if empty fields are to be untouched and any field with at least one lowercase alphabet should be quoted, then I think this would work - echo ,123,xyz,456,,abc2 | sed 's/[^,]*[a-z][^,]*/x27&x27/g' ...
    – Sundeep
    Oct 3 '17 at 9:12














up vote
2
down vote













echo ,123,xyz,456,,abc2 |
sed "s/[^,]*[^,0-9][^,]*/'&'/g;s/^,/'',/;s/,$/,''/;:1
s/,,/,'',/g;t1"
'',123,'xyz',456,'','abc2'


would enclose in quotes any comma-separated field that is not made exclusively of one or more decimal digits (that is empty fields like for lines that start (/^,/) or end (/,$/) with , or contain ,, and fields containing at least one non-digit (/[^,]*[^,0-9][^,]*/)).



If you want to recognise arbitrary decimal floating point numbers like -2, 1.23, 1e-2, that would become more complicated and you may want to resort to awk:



awk -F, -v OFS=, -v q="'" '

for (i = 1; i <= NF; i++)
if ($i !~ /^[[:blank:]]*[-+]?([0-9]+.?[0-9]*'


That changes:



abc,,0x20, +2.23E-5 , x y ,-2


to:



'abc','','0x20', +2.23E-5 ,' x y ',-2


If you wanted to get



'abc','','0x20', +2.23E-5 , 'x y' ,-2


instead, you'd do:



awk -F, -v OFS=, -v q="'" '

for (i = 1; i <= NF; i++)
if ($i !~ /^[[:blank:]]*[-+]?([0-9]+.?[0-9]*'





share|improve this answer






















  • not sure if OP had thought of so many corner cases! if empty fields are to be untouched and any field with at least one lowercase alphabet should be quoted, then I think this would work - echo ,123,xyz,456,,abc2 | sed 's/[^,]*[a-z][^,]*/x27&x27/g' ...
    – Sundeep
    Oct 3 '17 at 9:12












up vote
2
down vote










up vote
2
down vote









echo ,123,xyz,456,,abc2 |
sed "s/[^,]*[^,0-9][^,]*/'&'/g;s/^,/'',/;s/,$/,''/;:1
s/,,/,'',/g;t1"
'',123,'xyz',456,'','abc2'


would enclose in quotes any comma-separated field that is not made exclusively of one or more decimal digits (that is empty fields like for lines that start (/^,/) or end (/,$/) with , or contain ,, and fields containing at least one non-digit (/[^,]*[^,0-9][^,]*/)).



If you want to recognise arbitrary decimal floating point numbers like -2, 1.23, 1e-2, that would become more complicated and you may want to resort to awk:



awk -F, -v OFS=, -v q="'" '

for (i = 1; i <= NF; i++)
if ($i !~ /^[[:blank:]]*[-+]?([0-9]+.?[0-9]*'


That changes:



abc,,0x20, +2.23E-5 , x y ,-2


to:



'abc','','0x20', +2.23E-5 ,' x y ',-2


If you wanted to get



'abc','','0x20', +2.23E-5 , 'x y' ,-2


instead, you'd do:



awk -F, -v OFS=, -v q="'" '

for (i = 1; i <= NF; i++)
if ($i !~ /^[[:blank:]]*[-+]?([0-9]+.?[0-9]*'





share|improve this answer














echo ,123,xyz,456,,abc2 |
sed "s/[^,]*[^,0-9][^,]*/'&'/g;s/^,/'',/;s/,$/,''/;:1
s/,,/,'',/g;t1"
'',123,'xyz',456,'','abc2'


would enclose in quotes any comma-separated field that is not made exclusively of one or more decimal digits (that is empty fields like for lines that start (/^,/) or end (/,$/) with , or contain ,, and fields containing at least one non-digit (/[^,]*[^,0-9][^,]*/)).



If you want to recognise arbitrary decimal floating point numbers like -2, 1.23, 1e-2, that would become more complicated and you may want to resort to awk:



awk -F, -v OFS=, -v q="'" '

for (i = 1; i <= NF; i++)
if ($i !~ /^[[:blank:]]*[-+]?([0-9]+.?[0-9]*'


That changes:



abc,,0x20, +2.23E-5 , x y ,-2


to:



'abc','','0x20', +2.23E-5 ,' x y ',-2


If you wanted to get



'abc','','0x20', +2.23E-5 , 'x y' ,-2


instead, you'd do:



awk -F, -v OFS=, -v q="'" '

for (i = 1; i <= NF; i++)
if ($i !~ /^[[:blank:]]*[-+]?([0-9]+.?[0-9]*'






share|improve this answer














share|improve this answer



share|improve this answer








edited Oct 3 '17 at 11:57

























answered Oct 3 '17 at 8:37









Stéphane Chazelas

283k53522859




283k53522859











  • not sure if OP had thought of so many corner cases! if empty fields are to be untouched and any field with at least one lowercase alphabet should be quoted, then I think this would work - echo ,123,xyz,456,,abc2 | sed 's/[^,]*[a-z][^,]*/x27&x27/g' ...
    – Sundeep
    Oct 3 '17 at 9:12
















  • not sure if OP had thought of so many corner cases! if empty fields are to be untouched and any field with at least one lowercase alphabet should be quoted, then I think this would work - echo ,123,xyz,456,,abc2 | sed 's/[^,]*[a-z][^,]*/x27&x27/g' ...
    – Sundeep
    Oct 3 '17 at 9:12















not sure if OP had thought of so many corner cases! if empty fields are to be untouched and any field with at least one lowercase alphabet should be quoted, then I think this would work - echo ,123,xyz,456,,abc2 | sed 's/[^,]*[a-z][^,]*/x27&x27/g' ...
– Sundeep
Oct 3 '17 at 9:12




not sure if OP had thought of so many corner cases! if empty fields are to be untouched and any field with at least one lowercase alphabet should be quoted, then I think this would work - echo ,123,xyz,456,,abc2 | sed 's/[^,]*[a-z][^,]*/x27&x27/g' ...
– Sundeep
Oct 3 '17 at 9:12












up vote
1
down vote













With sed if your input data in each fields are only digits or alpha:



sed -E "s/[[:alpha:]]+/'&'/g" <<<"123,xyz,456,abc"


Or in complex mode.



sed -E "s/[0-9]*[[:alpha:]-]+[0-9]*/'&'/g" infile


Input:



123,xyz,456,abc
abc123,,a-b,5


Output:



123,'xyz',456,'abc'
'abc123',,'a-b',5





share|improve this answer






















  • That turns abc123,,a-b,5 into 'abc'123,,'a'-'b',5 instead of 'abc123','','a-b',5
    – Stéphane Chazelas
    Oct 3 '17 at 8:46











  • @StéphaneChazelas fixed that, but ,, in abc123,,1-n,5 is null, should be ,'', also?
    – Î±Ò“sнιη
    Oct 3 '17 at 9:09










  • Well, the OP's requirement are not clearly specified. I understood it as anything but numbers should be quoted, but there are plenty of other ways to interpret the question. It might be that the OP's input only contains either all-alpha or all-digit fields and we don't need to worry about empty fields or fields containing characters that are neither alpha nor digits or combination of alpha and digits.
    – Stéphane Chazelas
    Oct 3 '17 at 10:02






  • 1




    Thank you afshin exactly correct what i asked...i got the result...
    – jai
    Oct 3 '17 at 14:09















up vote
1
down vote













With sed if your input data in each fields are only digits or alpha:



sed -E "s/[[:alpha:]]+/'&'/g" <<<"123,xyz,456,abc"


Or in complex mode.



sed -E "s/[0-9]*[[:alpha:]-]+[0-9]*/'&'/g" infile


Input:



123,xyz,456,abc
abc123,,a-b,5


Output:



123,'xyz',456,'abc'
'abc123',,'a-b',5





share|improve this answer






















  • That turns abc123,,a-b,5 into 'abc'123,,'a'-'b',5 instead of 'abc123','','a-b',5
    – Stéphane Chazelas
    Oct 3 '17 at 8:46











  • @StéphaneChazelas fixed that, but ,, in abc123,,1-n,5 is null, should be ,'', also?
    – Î±Ò“sнιη
    Oct 3 '17 at 9:09










  • Well, the OP's requirement are not clearly specified. I understood it as anything but numbers should be quoted, but there are plenty of other ways to interpret the question. It might be that the OP's input only contains either all-alpha or all-digit fields and we don't need to worry about empty fields or fields containing characters that are neither alpha nor digits or combination of alpha and digits.
    – Stéphane Chazelas
    Oct 3 '17 at 10:02






  • 1




    Thank you afshin exactly correct what i asked...i got the result...
    – jai
    Oct 3 '17 at 14:09













up vote
1
down vote










up vote
1
down vote









With sed if your input data in each fields are only digits or alpha:



sed -E "s/[[:alpha:]]+/'&'/g" <<<"123,xyz,456,abc"


Or in complex mode.



sed -E "s/[0-9]*[[:alpha:]-]+[0-9]*/'&'/g" infile


Input:



123,xyz,456,abc
abc123,,a-b,5


Output:



123,'xyz',456,'abc'
'abc123',,'a-b',5





share|improve this answer














With sed if your input data in each fields are only digits or alpha:



sed -E "s/[[:alpha:]]+/'&'/g" <<<"123,xyz,456,abc"


Or in complex mode.



sed -E "s/[0-9]*[[:alpha:]-]+[0-9]*/'&'/g" infile


Input:



123,xyz,456,abc
abc123,,a-b,5


Output:



123,'xyz',456,'abc'
'abc123',,'a-b',5






share|improve this answer














share|improve this answer



share|improve this answer








edited Oct 3 '17 at 14:29

























answered Oct 3 '17 at 8:31









αғsнιη

15.7k92563




15.7k92563











  • That turns abc123,,a-b,5 into 'abc'123,,'a'-'b',5 instead of 'abc123','','a-b',5
    – Stéphane Chazelas
    Oct 3 '17 at 8:46











  • @StéphaneChazelas fixed that, but ,, in abc123,,1-n,5 is null, should be ,'', also?
    – Î±Ò“sнιη
    Oct 3 '17 at 9:09










  • Well, the OP's requirement are not clearly specified. I understood it as anything but numbers should be quoted, but there are plenty of other ways to interpret the question. It might be that the OP's input only contains either all-alpha or all-digit fields and we don't need to worry about empty fields or fields containing characters that are neither alpha nor digits or combination of alpha and digits.
    – Stéphane Chazelas
    Oct 3 '17 at 10:02






  • 1




    Thank you afshin exactly correct what i asked...i got the result...
    – jai
    Oct 3 '17 at 14:09

















  • That turns abc123,,a-b,5 into 'abc'123,,'a'-'b',5 instead of 'abc123','','a-b',5
    – Stéphane Chazelas
    Oct 3 '17 at 8:46











  • @StéphaneChazelas fixed that, but ,, in abc123,,1-n,5 is null, should be ,'', also?
    – Î±Ò“sнιη
    Oct 3 '17 at 9:09










  • Well, the OP's requirement are not clearly specified. I understood it as anything but numbers should be quoted, but there are plenty of other ways to interpret the question. It might be that the OP's input only contains either all-alpha or all-digit fields and we don't need to worry about empty fields or fields containing characters that are neither alpha nor digits or combination of alpha and digits.
    – Stéphane Chazelas
    Oct 3 '17 at 10:02






  • 1




    Thank you afshin exactly correct what i asked...i got the result...
    – jai
    Oct 3 '17 at 14:09
















That turns abc123,,a-b,5 into 'abc'123,,'a'-'b',5 instead of 'abc123','','a-b',5
– Stéphane Chazelas
Oct 3 '17 at 8:46





That turns abc123,,a-b,5 into 'abc'123,,'a'-'b',5 instead of 'abc123','','a-b',5
– Stéphane Chazelas
Oct 3 '17 at 8:46













@StéphaneChazelas fixed that, but ,, in abc123,,1-n,5 is null, should be ,'', also?
– Î±Ò“sнιη
Oct 3 '17 at 9:09




@StéphaneChazelas fixed that, but ,, in abc123,,1-n,5 is null, should be ,'', also?
– Î±Ò“sнιη
Oct 3 '17 at 9:09












Well, the OP's requirement are not clearly specified. I understood it as anything but numbers should be quoted, but there are plenty of other ways to interpret the question. It might be that the OP's input only contains either all-alpha or all-digit fields and we don't need to worry about empty fields or fields containing characters that are neither alpha nor digits or combination of alpha and digits.
– Stéphane Chazelas
Oct 3 '17 at 10:02




Well, the OP's requirement are not clearly specified. I understood it as anything but numbers should be quoted, but there are plenty of other ways to interpret the question. It might be that the OP's input only contains either all-alpha or all-digit fields and we don't need to worry about empty fields or fields containing characters that are neither alpha nor digits or combination of alpha and digits.
– Stéphane Chazelas
Oct 3 '17 at 10:02




1




1




Thank you afshin exactly correct what i asked...i got the result...
– jai
Oct 3 '17 at 14:09





Thank you afshin exactly correct what i asked...i got the result...
– jai
Oct 3 '17 at 14:09


















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f395773%2fhow-to-add-the-single-quotation-marks-for-string-alphabet-values%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?