Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Join 2 separate strings into one with alternate tokens.

Shell Programming and Scripting


Tags
cut, join, string between delimeters, string manipulation

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-05-2011   -   Original Discussion by sikku
sikku's Unix or Linux Image
sikku sikku is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 5 June 2012, 6:30 AM EDT
Posts: 18
Thanks: 9
Thanked 0 Times in 0 Posts
Join 2 separate strings into one with alternate tokens.

Hi,

I have two strings eg:
string1=abc|def|hij
string2=12|13|14

I want a new string with
string3="abc:12 def:13 hij:14"

I am using shell scripting. Is there any method to do this?

I tried using cut command but this wont advance the respective strings.
Can anybody help in this matter?
Sponsored Links
    #2  
Old Unix and Linux 07-05-2011   -   Original Discussion by sikku
Skrynesaver's Unix or Linux Image
Skrynesaver Skrynesaver is offline Forum Advisor  
Grumpy old geek
 
Join Date: Mar 2011
Last Activity: 19 December 2017, 7:31 AM EST
Location: Éire
Posts: 931
Thanks: 25
Thanked 223 Times in 217 Posts
Not a pure shell solution, however the following works


Code:
export string1="abc|def|hij"
export string2="12|13|14"
string3=$(perl -e '@f=split /\|/, $ENV{string1};@s=split /\|/, $ENV{string2};for (0..$#f){print "$f[$_]:$s[$_] "}')
echo $string3

The Following User Says Thank You to Skrynesaver For This Useful Post:
karthik023 (07-06-2011)
Sponsored Links
    #3  
Old Unix and Linux 07-05-2011   -   Original Discussion by sikku
sikku's Unix or Linux Image
sikku sikku is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 5 June 2012, 6:30 AM EDT
Posts: 18
Thanks: 9
Thanked 0 Times in 0 Posts
Thanks.
It runs from the command line, but not from the script file.
Any idea?

I am running this script as
#!/bin/sh
    #4  
Old Unix and Linux 07-05-2011   -   Original Discussion by sikku
aigles's Unix or Linux Image
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Last Activity: 1 December 2014, 9:03 AM EST
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 62 Times in 58 Posts
a pure shell (ksh) solution :

Code:
#!/usr/bin/ksh

string1='abc|def|hij'
string2='12|13|14'
string3=''

oIFS="${IFS}"
IFS='|'

set -A str1 ${string1}
set -A str2 ${string2}

IFS="${oIFS}"

cnt1=${#str1[*]}
cnt2=${#str2[*]}
(( cnt1 > cnt2 )) && cnt=${cnt1} || cnt=${cnt2}

while (( cnt > 0 ))
do
   (( cnt -= 1 ))
   string3="${str1[$cnt]}:${str2[$cnt]} ${string3}"
done

string3="${string3# }"
print "${string3}"

Jean-Pierre.
The Following User Says Thank You to aigles For This Useful Post:
sikku (07-05-2011)
Sponsored Links
    #5  
Old Unix and Linux 07-05-2011   -   Original Discussion by sikku
sikku's Unix or Linux Image
sikku sikku is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 5 June 2012, 6:30 AM EDT
Posts: 18
Thanks: 9
Thanked 0 Times in 0 Posts
It worked fine in ksh. Thanks for the help.
But it gives wrong results for strings of different length.
Any idea to restrict it to the smaller string among the both?
Sponsored Links
    #6  
Old Unix and Linux 07-05-2011   -   Original Discussion by sikku
ctsgnb's Unix or Linux Image
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 3 April 2018, 6:42 AM EDT
Location: France
Posts: 2,967
Thanks: 88
Thanked 640 Times in 610 Posts


Code:
paste -d: <(echo $string1 | sed 's/\|/ /g' | xargs -n1) <(echo $string2 | sed 's/\|/ /g' | xargs -n1) | xargs

---------- Post updated at 12:07 PM ---------- Previous update was at 12:06 PM ----------



Code:
[ctsgnb@shell ~/sand]$ echo $string1
abc|def|hij
[ctsgnb@shell ~/sand]$ echo $string2
12|13|14
[ctsgnb@shell ~/sand]$ paste -d: <(echo $string1 | sed 's/\|/ /g' | xargs -n1) <(echo $string2 | sed 's/\|/ /g' | xargs -n1) | xargs
abc:12 def:13 hij:14
[ctsgnb@shell ~/sand]$

---------- Post updated at 12:11 PM ---------- Previous update was at 12:07 PM ----------



Code:
echo $string1 $string2 | sed 's/\|/ /g' | xargs -n1 | pr -2 -t -s: | xargs



Code:
$ echo $string1 $string2 | sed 's/\|/ /g' | xargs -n1 | pr -2 -t -s: | xargs
abc:12 def:13 hij:14

The Following User Says Thank You to ctsgnb For This Useful Post:
sikku (07-05-2011)
Sponsored Links
    #7  
Old Unix and Linux 07-05-2011   -   Original Discussion by sikku
michaelrozar17's Unix or Linux Image
michaelrozar17 michaelrozar17 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 22 September 2016, 3:57 AM EDT
Location: IN
Posts: 894
Thanks: 15
Thanked 183 Times in 174 Posts
Or..


Code:
str3=$(echo "$str1 $str2" | sed 's/\(.*\)|\(.*\)|\(.*\) \(.*\)|\(.*\)|\(.*\)/\1:\4 \2:\5 \3:\6/')

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Join 2 files with multiple columns: awk/grep/join? InfoSeeker UNIX for Dummies Questions & Answers 3 12-01-2009 07:45 PM
Replacing tokens abhinav192 Shell Programming and Scripting 3 11-25-2009 11:42 AM
sql,multiple join,outer join issue robbiezr Programming 0 06-05-2009 11:26 PM
How to join two strings together Jenny.palmy UNIX for Dummies Questions & Answers 1 05-23-2008 02:36 AM
tokens in unix ? seaten UNIX for Dummies Questions & Answers 6 05-09-2005 04:57 AM



All times are GMT -4. The time now is 02:34 PM.