Solved

bash string manipulation

Posted on 2013-11-01
10
463 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 41

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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 41

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
 
LVL 41

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 41

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 41

Author Comment

by:Sharath
ID: 39618492
Thanks ozo.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

685 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