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
# 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 10: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 03: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 .
The Following User Says Thank You to ctsgnb For This Useful 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

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

Edited: string3 to string4

Last edited by sikku; 07-06-2011 at 06: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)

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:07 PM.

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