Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

bash string manipulation

Posted on 2013-11-01
10
Medium Priority
?
475 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

719 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