Home Man
Search
Today's Posts
Register

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.

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

Login to Reply

 
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

The Following User Says Thank You to Skrynesaver For This Useful 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.
The Following User Says Thank You to aigles For This Useful 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

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

Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help to join separate lines in a single one from xml file Ophiuchus Shell Programming and Scripting 8 02-25-2018 12:55 AM
Single grep to multiple strings with separate output per string nms Shell Programming and Scripting 9 09-15-2017 03:42 AM
Reading tokens kristinu Programming 1 05-20-2014 04: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 04:15 PM
+: more tokens expected senormarquez Shell Programming and Scripting 4 06-22-2010 10:24 AM
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
: + : more tokens expected Nomaad Shell Programming and Scripting 3 04-17-2008 02:49 PM


All times are GMT -4. The time now is 09:41 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password