Solved

bash string manipulation

Posted on 2013-11-01
10
456 Views
Last Modified: 2013-11-02
I have a variable with value as
var="a,b,c"
I would like to convert that as in a bash script.
var="t1.a=t2.a AND t1.a=t2.b AND t1.c=t2.c"

I tried to split the string into an array iterate through the elements but could not able to generate the desired string.

Can anyone please help.

Thanks
Sharath
0
Comment
Question by:Sharath
  • 5
  • 4
10 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 39617843
give this a shot:

 
var="a,b,c"
arr=$(echo $var | tr "," "\n")
output=""

i=0
for x in $arr
do
    echo "> [$x]"
   if [ "$i" -eq 0 ]
   then
       output="t1."${x}"=t2."${x}" AND t1."${x}"=t2."
   elif [ "$i" -eq 1 ]
   then
       output=${output}${x}
   elif [ "$i" -eq 2 ]
   then
       output=${output}" AND t1."${x}"=t2."${x}

   fi
    i++
done

echo $output
 

Open in new window

0
 
LVL 40

Author Comment

by:Sharath
ID: 39618046
YZlat, Thanks for your post.
your script only works for "a,b,c". What if I have a string like "a,b,c,d,e,f"?
However, I already wrote a script using for loop.
Is there any better way of doing this with sed/awk instead of loop?
0
 
LVL 84

Expert Comment

by:ozo
ID: 39618081
Do you mean
var="t1.a=t2.a AND t1.b=t2.b AND t1.c=t2.c"
?
If not, what is the rule for producing
"t1.a=t2.a AND t1.a=t2.b AND t1.c=t2.c"  from "a,b,c"
and how would that rule apply to "a,b,c,d,e,f"?
0
 
LVL 40

Author Comment

by:Sharath
ID: 39618091
yes. for each value in the string, change it to t1.str1=t2.str1
if var="a,b,c,d,e,f"
then var should be "t1.a=t2.a AND t1.b=t2.b AND t1.c=t2.c AND t1.d=t2.d AND t1.e=t2.e"
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39618102
var="a,b,c,d,e,f"
var=`echo $var | sed -e 's/\([a-z]\)/t1.&=t2.&/g' -e 's/,/ AND /g'`
echo $var
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 40

Author Comment

by:Sharath
ID: 39618413
ozo, simply superb. exactly what I am looking for.

Here is my effort on this. I am able to do this but not very straightforward.
I am still learning shell scripting and want to know if you can provide some pointers in learning sed/awk.  Thank you so much for your post.

var="a,b,c,d,e,f"
var=$(echo $var | tr ',' ' ')
var=($var)
for i in "${var[@]}"; 
do 
	var2=("${var2[@]}" "t1.$i=t2.$i") 
done
var=$(printf '%s,' "${var2[@]}")
var="${var:0:${#var}-1}"
var=${var//,/ AND }
echo $var

Open in new window

0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 500 total points
ID: 39618448
Maybe you're using all those different operations in order to learn them, but they could be simplified to

var="a,b,c,d,e,f"
var2=`IFS=','; for i in $var ; do echo  "t1.$i=t2.$i" ; done`
echo ${var2//[[:space:]]/ AND }

or

var2=`IFS=','; for i in $var ; do echo -n "t1.$i=t2.$i AND " ; done`
echo ${var2% AND }
0
 
LVL 40

Author Comment

by:Sharath
ID: 39618476
I have used all those operations to achieve what I am looking for. I like your posts as you are simplifying all those operations. I am learning sed/awk, in fact the shell scripting.
There are lot of material available online. Will you be able to help me to start in right direction. Much appreciated.
0
 
LVL 84

Expert Comment

by:ozo
ID: 39618488
man sed
and
man awk
are the primary online material I use.
Asking and answering questions here is a good excercise.
The O'Reilly lorises book is a good reference
Question 10 in the awk FAQ lists other resources
http://www.faqs.org/faqs/computer-lang/awk/faq/
0
 
LVL 40

Author Comment

by:Sharath
ID: 39618492
Thanks ozo.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now