• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Shell Script Help Needed

 
jaiser roney
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do it in various way. On option is to use the split function of awk command. Please go through the awk ref
 
Anand Hariharan
Rancher
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,

 
jaiser roney
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic