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
    #15  
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
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..
Sponsored Links
    #16  
Old Unix and Linux 07-06-2011   -   Original Discussion by sikku
karthik023's Unix or Linux Image
karthik023 karthik023 is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 21 June 2012, 2:19 PM EDT
Posts: 2
Thanks: 7
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #17  
Old Unix and Linux 07-06-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
@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 Unix and Linux 07-06-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
@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

Sponsored Links
    #19  
Old Unix and Linux 07-06-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
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)
Sponsored Links
    #20  
Old Unix and Linux 07-06-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
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 View Post
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. Linux

Edited: string3 to string4

Last edited by sikku; 07-06-2011 at 06:05 AM..
Sponsored Links
    #21  
Old Unix and Linux 07-06-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:
string4=$(paste -d: <(echo $string1 | sed 's/\|/ /g' | xargs -n1) <(echo $string2 | sed 's/\|/ /g' | xargs -n1) | xargs)

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