How to add the single quotation marks for string (Alphabet) values?
Clash 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'
text-processing
add a comment |Â
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'
text-processing
What command do you use to get this output? Simpleecho
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
add a comment |Â
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'
text-processing
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
text-processing
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? Simpleecho
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
add a comment |Â
What command do you use to get this output? Simpleecho
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
add a comment |Â
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]*'
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
add a comment |Â
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
That turnsabc123,,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,,
inabc123,,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
add a comment |Â
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]*'
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
add a comment |Â
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]*'
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
add a comment |Â
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]*'
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]*'
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
add a comment |Â
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
add a comment |Â
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
That turnsabc123,,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,,
inabc123,,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
add a comment |Â
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
That turnsabc123,,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,,
inabc123,,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
add a comment |Â
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
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
edited Oct 3 '17 at 14:29
answered Oct 3 '17 at 8:31
ñÃÂsýù÷
15.7k92563
15.7k92563
That turnsabc123,,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,,
inabc123,,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
add a comment |Â
That turnsabc123,,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,,
inabc123,,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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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