Join 2 separate strings into one with alternate tokens.

Tags
cut, join, shell scripts, string between delimeters, string manipulation

 
Thread Tools Search this Thread
# 1  
Old 07-05-2011
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?
# 2  
Old 07-05-2011
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

This User Gave Thanks to Skrynesaver For This Post:
karthik023 (07-06-2011)
# 3  
Old 07-05-2011
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 07-05-2011
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.
This User Gave Thanks to aigles For This Post:
sikku (07-05-2011)
# 5  
Old 07-05-2011
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?
# 6  
Old 07-05-2011
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

This User Gave Thanks to ctsgnb For This Post:
sikku (07-05-2011)
# 7  
Old 07-05-2011
Or..
Code:
str3=$(echo "$str1 $str2" | sed 's/\(.*\)|\(.*\)|\(.*\) \(.*\)|\(.*\)|\(.*\)/\1:\4 \2:\5 \3:\6/')


|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Help to join separate lines in a single one from xml file Ophiuchus Shell Programming and Scripting 8 02-25-2018 01:55 AM
Single grep to multiple strings with separate output per string nms Shell Programming and Scripting 9 09-15-2017 04:42 AM
C++ getline, parse and take first tokens by condition yifangt Programming 18 09-19-2014 08:51 PM
Reading tokens kristinu Programming 1 05-20-2014 05:19 PM
How to use the the join command to join multiple files by a common column evelibertine UNIX for Dummies Questions & Answers 5 07-05-2012 05:15 PM
Need tokens in shell script AB10 Shell Programming and Scripting 3 10-01-2010 07:48 AM
+: more tokens expected senormarquez Shell Programming and Scripting 4 06-22-2010 11:24 AM
Join 2 files with multiple columns: awk/grep/join? InfoSeeker UNIX for Dummies Questions & Answers 3 12-01-2009 08:45 PM
Replacing tokens abhinav192 Shell Programming and Scripting 3 11-25-2009 12:42 PM
sql,multiple join,outer join issue robbiezr Programming 0 06-06-2009 12:26 AM
selecting tokens from a string... c_d Shell Programming and Scripting 1 01-15-2009 05:04 AM
How to join two strings together Jenny.palmy UNIX for Dummies Questions & Answers 1 05-23-2008 03:36 AM
: + : more tokens expected Nomaad Shell Programming and Scripting 3 04-17-2008 03:49 PM
reverse tokens with sed markc Shell Programming and Scripting 1 02-22-2008 01:55 AM
tokens in unix ? seaten UNIX for Dummies Questions & Answers 6 05-09-2005 05:57 AM