conversion of space separated string to coma separated in bash
up vote
0
down vote
favorite
I am new to shell scripting
I am indenting to convert a string like:
abc def ghi
to
"abc","def","ghi"
This is what I have tried:
testvar= "abc def ghi"
a='"';
res="";
coma=","
for i in $testvar
do
vals=($i//__/ )
if [ -z "$res" ]; then
$res= $res$a$vals$a
else
$res=$res$coma$a$vals$a
fi
done
echo $res
Its giving this error:
$bash -f main.sh
main.sh: line 4: abc def ghi: command not found
What wrong am I doing?
Is there any better way to do this?
bash shell
add a comment |
up vote
0
down vote
favorite
I am new to shell scripting
I am indenting to convert a string like:
abc def ghi
to
"abc","def","ghi"
This is what I have tried:
testvar= "abc def ghi"
a='"';
res="";
coma=","
for i in $testvar
do
vals=($i//__/ )
if [ -z "$res" ]; then
$res= $res$a$vals$a
else
$res=$res$coma$a$vals$a
fi
done
echo $res
Its giving this error:
$bash -f main.sh
main.sh: line 4: abc def ghi: command not found
What wrong am I doing?
Is there any better way to do this?
bash shell
Check out the answer to a similar questions here: stackoverflow.com/questions/918886/…
– Eric Jorgensen
Nov 10 at 15:23
Thanks for the pointer, but I am trying to do a bit diff thing, I have already used the split syntax from the link in vals=($i//__/ )
– Samayra Goyal
Nov 10 at 15:34
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am new to shell scripting
I am indenting to convert a string like:
abc def ghi
to
"abc","def","ghi"
This is what I have tried:
testvar= "abc def ghi"
a='"';
res="";
coma=","
for i in $testvar
do
vals=($i//__/ )
if [ -z "$res" ]; then
$res= $res$a$vals$a
else
$res=$res$coma$a$vals$a
fi
done
echo $res
Its giving this error:
$bash -f main.sh
main.sh: line 4: abc def ghi: command not found
What wrong am I doing?
Is there any better way to do this?
bash shell
I am new to shell scripting
I am indenting to convert a string like:
abc def ghi
to
"abc","def","ghi"
This is what I have tried:
testvar= "abc def ghi"
a='"';
res="";
coma=","
for i in $testvar
do
vals=($i//__/ )
if [ -z "$res" ]; then
$res= $res$a$vals$a
else
$res=$res$coma$a$vals$a
fi
done
echo $res
Its giving this error:
$bash -f main.sh
main.sh: line 4: abc def ghi: command not found
What wrong am I doing?
Is there any better way to do this?
bash shell
bash shell
edited Nov 10 at 15:58
Cyrus
44.2k43375
44.2k43375
asked Nov 10 at 15:21
Samayra Goyal
12
12
Check out the answer to a similar questions here: stackoverflow.com/questions/918886/…
– Eric Jorgensen
Nov 10 at 15:23
Thanks for the pointer, but I am trying to do a bit diff thing, I have already used the split syntax from the link in vals=($i//__/ )
– Samayra Goyal
Nov 10 at 15:34
add a comment |
Check out the answer to a similar questions here: stackoverflow.com/questions/918886/…
– Eric Jorgensen
Nov 10 at 15:23
Thanks for the pointer, but I am trying to do a bit diff thing, I have already used the split syntax from the link in vals=($i//__/ )
– Samayra Goyal
Nov 10 at 15:34
Check out the answer to a similar questions here: stackoverflow.com/questions/918886/…
– Eric Jorgensen
Nov 10 at 15:23
Check out the answer to a similar questions here: stackoverflow.com/questions/918886/…
– Eric Jorgensen
Nov 10 at 15:23
Thanks for the pointer, but I am trying to do a bit diff thing, I have already used the split syntax from the link in vals=($i//__/ )
– Samayra Goyal
Nov 10 at 15:34
Thanks for the pointer, but I am trying to do a bit diff thing, I have already used the split syntax from the link in vals=($i//__/ )
– Samayra Goyal
Nov 10 at 15:34
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
Alternate way by creating an array and using IFS. Loop through every value and add double qoutes around it.
array=($testvar)
declare item
for idx in "$!array[@]"; do
item="$array[$idx]"
array[$idx]=""$item"" # Add double qoute
done
(IFS=, ; echo "$array[*]") # prevents IFS from changing.
add a comment |
up vote
0
down vote
maybe you can use sed
command like below:
(notice that there are multi spaces in def
and ghi
)
$ echo 'abc def ghi' | sed -E 's/s+/,/g'
abc,def,ghi
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Alternate way by creating an array and using IFS. Loop through every value and add double qoutes around it.
array=($testvar)
declare item
for idx in "$!array[@]"; do
item="$array[$idx]"
array[$idx]=""$item"" # Add double qoute
done
(IFS=, ; echo "$array[*]") # prevents IFS from changing.
add a comment |
up vote
0
down vote
Alternate way by creating an array and using IFS. Loop through every value and add double qoutes around it.
array=($testvar)
declare item
for idx in "$!array[@]"; do
item="$array[$idx]"
array[$idx]=""$item"" # Add double qoute
done
(IFS=, ; echo "$array[*]") # prevents IFS from changing.
add a comment |
up vote
0
down vote
up vote
0
down vote
Alternate way by creating an array and using IFS. Loop through every value and add double qoutes around it.
array=($testvar)
declare item
for idx in "$!array[@]"; do
item="$array[$idx]"
array[$idx]=""$item"" # Add double qoute
done
(IFS=, ; echo "$array[*]") # prevents IFS from changing.
Alternate way by creating an array and using IFS. Loop through every value and add double qoutes around it.
array=($testvar)
declare item
for idx in "$!array[@]"; do
item="$array[$idx]"
array[$idx]=""$item"" # Add double qoute
done
(IFS=, ; echo "$array[*]") # prevents IFS from changing.
answered Nov 10 at 16:47
Vivek Akupatni
67729
67729
add a comment |
add a comment |
up vote
0
down vote
maybe you can use sed
command like below:
(notice that there are multi spaces in def
and ghi
)
$ echo 'abc def ghi' | sed -E 's/s+/,/g'
abc,def,ghi
add a comment |
up vote
0
down vote
maybe you can use sed
command like below:
(notice that there are multi spaces in def
and ghi
)
$ echo 'abc def ghi' | sed -E 's/s+/,/g'
abc,def,ghi
add a comment |
up vote
0
down vote
up vote
0
down vote
maybe you can use sed
command like below:
(notice that there are multi spaces in def
and ghi
)
$ echo 'abc def ghi' | sed -E 's/s+/,/g'
abc,def,ghi
maybe you can use sed
command like below:
(notice that there are multi spaces in def
and ghi
)
$ echo 'abc def ghi' | sed -E 's/s+/,/g'
abc,def,ghi
answered Nov 11 at 13:59
GerryLon
444
444
add a comment |
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240366%2fconversion-of-space-separated-string-to-coma-separated-in-bash%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Check out the answer to a similar questions here: stackoverflow.com/questions/918886/…
– Eric Jorgensen
Nov 10 at 15:23
Thanks for the pointer, but I am trying to do a bit diff thing, I have already used the split syntax from the link in vals=($i//__/ )
– Samayra Goyal
Nov 10 at 15:34