Join 2 separate strings into one with alternate tokens.

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

 
Thread Tools Search this Thread
# 15  
Old 07-05-2011
The latest one works in the command line but not in the script.
Code:
echo "$string1 $string2"  | tr ' |' '\n\n' | pr -2 -t -s: | sed 's/^\([^:]*\):\([^:]*\)/\2:\1/' | xargs

the following one works in scripts, for now I am using this one itself.
Code:
paste -d: <((echo $string2 | sed 's/\|/ /g' | xargs -n1) | cut -f1 -d:) <(echo $string1 |
 sed 's/\|/ /g' | xargs -n1)  <((echo $string2 | sed 's/\|/ /g' | xargs -n1) | cut -f2 -d: ) | xargs

Is there any method to incorporate the first one into the script file?

---------- Post updated at 09:19 AM ---------- Previous update was at 08:43 AM ----------

I have new question:

I have a string:

s1="123:1,124:2,126:4"

I want

s2="123 124 126"

What should I use?

---

Got it:

Code:
$echo $s1 | tr ',' '\n' | cut -f1 -d: | xargs
123 124 126


Last edited by sikku; 07-05-2011 at 11:33 AM..
# 16  
Old 07-06-2011
Could you please explain the purpose of $ENV (in this context).
is ENV variable used to pull the environment variables (declared in the shell) into the script?
If that is the case ...the following didn't work...could you please correct it.
Code:
#!/bin/ksh
# this script is used to concatenate 2 strings after removing the delimiter
string1="abc|def|hij"; echo $string1;
string2="12|13|14"; echo $string2;
string3=$(perl -e '@f=split( /\|/, $string1);@s=split(/\|/, $string2);for (0..$#f){print "$f[$_]:$s[$_] "}')
echo $string3

thanks,
perl rookie

Last edited by Franklin52; 07-06-2011 at 04:07 AM.. Reason: Please use code tags for code and data samples, thank you
# 17  
Old 07-06-2011
@Sikku :
Code:
$echo $s1 | tr ',' '\n' | cut -f1 -d: | xargs
123 124 126

could be shorten (and save some pipe as well) with:
Code:
echo $s1 | sed 's/:[^,]*//g;s/,/ /g'

---------- Post updated at 10:38 AM ---------- Previous update was at 09:23 AM ----------

Code:
echo "$string1 $string2" | tr ' |' '\n\n' | pr -2 -t -s: | sed 's/^\([^:]*\):\([^:]*\)/\2:\1/' | xargs

Works in command line but does not work in your script ???
Could you please show the content of your script, it sounds weird that you could not pass that command from within your script .
This User Gave Thanks to ctsgnb For This Post:
sikku (07-06-2011)
# 18  
Old 07-06-2011
@ctsgnb:

This is the content of my script

Code:
#!/bin/ksh

string1="abc|def|hij"
string2="123:1|134:2|145:3"

#string3="`paste -d: <((echo $string2 | sed 's/\|/ /g' | xargs -n1) | cut -f1 -d:) <(echo $string1 | \
#         sed 's/\|/ /g' | xargs -n1)  <((echo $string2 | sed 's/\|/ /g' | xargs -n1) | cut -f2 -d: ) | xargs`"

string3="`echo "$string1 $string2" | tr ' |' '\n\n' | pr -2 -t -s: | sed 's/^\([^:]*\):\([^:]*\)/\2:\1/' | xargs`"
echo $string3

I get this error: (list is the script name)
Code:
$ list
./list[9]: : cannot execute
./list[9]: 123:1|134:2|145:3 | tr ' |' '\n\n' | pr -2 -t -s: | sed 's/^\([^:]*\):\([^:]*\)/\2:\1/' | xargs: cannot execute

# 19  
Old 07-06-2011
Try this :
Code:
string3=$(echo "$string1 $string2" | tr ' |' '\n\n' | pr -2 -t -s: | sed 's/^\([^:]*\):\([^:]*\)/\2:\1/' | xargs )
echo $string3

This User Gave Thanks to ctsgnb For This Post:
sikku (07-06-2011)
# 20  
Old 07-06-2011
In fact my script should be like this:

Code:
#!/bin/ksh

string1="abc|def|hij"
string2="1212|2134|1245"
string3="123:1|134:2|145:3"

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

#string4="$string3 `paste -d: <((echo $string3 | sed 's/\|/ /g' | xargs -n1) | cut -f1 -d:) <(echo $string1 | \
#         sed 's/\|/ /g' | xargs -n1)  <((echo $string2 | sed 's/\|/ /g' | xargs -n1) | cut -f2 -d: ) | xargs`"

string4="$string4 `echo "$string1 $string3" | tr ' |' '\n\n' | pr -2 -t -s: | sed 's/^\([^:]*\):\([^:]*\)/\2:\1/' | xargs`"
echo $string3

I want to add string3 to string4 and then append the last result to string4 again.

---------- Post updated at 04:46 AM ---------- Previous update was at 04:43 AM ----------

Quote:
Originally Posted by ctsgnb
Try this :
Code:
string3=$(echo "$string1 $string2" | tr ' |' '\n\n' | pr -2 -t -s: | sed 's/^\([^:]*\):\([^:]*\)/\2:\1/' | xargs )
echo $string3

Ok. Its working now. Thanks. Smilie

Edited: string3 to string4

Last edited by sikku; 07-06-2011 at 07:05 AM..
# 21  
Old 07-06-2011
Code:
string4=$(paste -d: <(echo $string1 | sed 's/\|/ /g' | xargs -n1) <(echo $string2 | sed 's/\|/ /g' | xargs -n1) | xargs)


|
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