Solved

bash string manipulation

Posted on 2013-11-01
10
458 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
linux crontab output 3 67
Remove a folder in Linux 9 91
How to learn Linux? 10 43
Run DOS2UNIX and then execute the command 21 39
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…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.:

910 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

18 Experts available now in Live!

Get 1:1 Help Now