| Author |
Shell Script Help Needed
|
jaiser roney
Greenhorn
Joined: Jul 15, 2009
Posts: 16
|
|
Hi ,
I am new to Shell Script , i am working in a file that starts as #!/bin/sh
In this file I am getting a form parameter $FORM_form_id_numbers which is having value as 1234ab,4578,9843dd,2365 my requirement is i wanted to pass this value as in IN clause as '1234ab','4578','9843dd','2365'
overall my requirement is to convert 1234ab,4578,9843dd,2365 into '1234ab','4578','9843dd','2365'
Please take care that after last element i don''t want any comma
How can i proceed for the same .
Thank you in Advance.
|
 |
Soumyajit Hazra
Ranch Hand
Joined: Jun 26, 2007
Posts: 136
|
|
|
You can do it in various way. On option is to use the split function of awk command. Please go through the awk ref
|
Java Programmer | SCJP 1.5 | SCWCD 1.4
|
 |
Anand Hariharan
Rancher
Joined: Aug 22, 2006
Posts: 252
|
|
Try -
You may need to escape the inner single-quotes with more back-slashes depending upon the shell in which your script will execute.
For example,
|
"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
|
 |
jaiser roney
Greenhorn
Joined: Jul 15, 2009
Posts: 16
|
|
Now escaping the single quotes becomes the biggest challenge for me , i tried various solutions but they didn't work.
Below is the basic information of the environment on which i am working
1) When i type echo $shell , on my environment , it prints /bin/csh
But i am testing after creating a shell file that starts #!/bin/sh ,
When i tried as
#!/bin/sh
MyVAL="foo,bar,tar"
L=`echo $MyVAL |sed -e "s/'/'/g"`
echo $L
I got foo,bar,tar as echo result
When i changed L as L=`echo $MyVAL |sed -e "s/'/\'/g"` I still get foo,bar,tar as echo result
When i changed L as L=`echo $MyVAL |sed -e "s/'/\\'/g"` I still get foo,bar,tar as echo result
When i changed L as L=`echo $MyVAL |sed -e "s/'/\\\'/g"` I still get foo,bar,tar as echo result
When i changed L as L=`echo $MyVAL |sed -e "s/'/\\\\'/g"` I still get foo,bar,tar as echo result
When i changed L as L=`echo $MyVAL |sed -e "s/'/\\\\\'/g"` I still get foo,bar,tar as echo result
It seems that now my primary question is how can i replace comma by single quote , i tried my solutions by googling but din't find any answer
Can anyone please help in this regard.
Thank you in Advance.
|
 |
jaiser roney
Greenhorn
Joined: Jul 15, 2009
Posts: 16
|
|
Please ignore the last post ,
its a type error in L
it should be like
L=`echo $MyVAL |sed -e "s/,/'/g"`
instead of
L=`echo $MyVAL |sed -e "s/'/'/g"`
I would like to thank Anand Hariharan.
Thank you Sir.
|
 |
jaiser roney
Greenhorn
Joined: Jul 15, 2009
Posts: 16
|
|
While typing the below command in the shell
sed -e 's/,/\',\'/g' -e 's/^/\'/' -e 's/$/\'/'
I was struggling typing caret character (^) , so i just copied from here , but if i want to actually type this character , what key combination should i use ?
|
 |
Anand Hariharan
Rancher
Joined: Aug 22, 2006
Posts: 252
|
|
jaiser roney wrote:
While typing the below command in the shell
sed -e 's/,/\',\'/g' -e 's/^/\'/' -e 's/$/\'/'
I was struggling typing caret character (^) , so i just copied from here , but if i want to actually type this character , what key combination should i use ?
Shift 6 on my keyboard (and I am referring to the 6 just above 't' and 'y', not the 6 that might be in a separate number pad).
jaiser roney wrote:
I would like to thank Anand Hariharan.
Thank you Sir.
It was my pleasure. You are most welcome.
sincerely,
- Anand
|
 |
 |
|
|
subject: Shell Script Help Needed
|
|
|