string4=$(paste -d: <(echo $string1 | sed 's/\|/ /g' | xargs -n1) <(echo $string2 | sed 's/\|/ /g' | xargs -n1) | xargs)
I have a new situation now:
string1="abc|def|hij"
string2="1212,2134,1245"
string3="123:1,134:2,145:3"
string4="abc:1212 def:2134 hij:1245"
string5="123:abc:1 134:def:2 145:hij:3"
final_string=string4+string5
Instead of pipes now I have commas in the other strings except the first string.
How would you change the scripts accordingly??
I am confused with your last post especially with string3 :
Is it already built ? or are you requesting to build that string3 from some other string (and which) ?
$ string1="abc|def|hij"
$ string2="1212,2134,1245"
$ echo $string1 $string2 | tr ',|' ' ' | xargs -n1 | pr -2 -t -s: | paste -sd, -
abc:1212,def:2134,hij:1245
string3 is already built.
string1="abc|def|hij"
string3="123:1|134:2|145:3"
I need this from string1 and string3
string5="123:abc:1 134:def:2 145:hij:3"
Thanks for the first script...
---------- Post updated at 06:23 AM ---------- Previous update was at 06:12 AM ----------
Ho about this? I changed your own code:
$ echo "$string2 $string1" | tr ' ,|' '\n\n\n' | pr -2ts: | sed 's/^\([^:]*\):\([^:]*\)/\2:\1/' | xargs
abc:1231 def:1322 hij:1443
You are currently uselessly switching things with the sed statement which force you to give string2 before string1 :
The following would be simpler :
$ echo $string1 $string2 | tr ' ,|' '\n\n\n' | pr -2ts: | xargs
abc:1212 def:2134 hij:1245
# echo "$string1 $string3" | tr ' ,|' '\n\n\n' | pr -2ts: | sed 's/^\([^:]*\):\([^:]*\)/\2:\1/' | xargs
123:abc:1 134:def:2 145:hij:3
echo $string1 $string2 | tr ' ,|' '\n\n\n' | pr -2 -t -s: | xargs
That did the trick... So I was doing something which was not required at all...
How about the other script? I am not able to find how to do it.
# echo "$string1 $string3" | tr ' ,|' '\n\n\n' | pr -2ts: | sed 's/^\([^:]*\):\([^:]*\)/\2:\1/' | xargs
123:abc:1 134:def:2 145:hij:3
?
Thanks for the help..
Basically what does this code do?
sed 's/^\([^:]*\):\([^:]*\)/\2:\1/'
Can you please explain?
You might find this link useful, but I've not tried it yet:
You are using this reg exp [^:]* only twice, which means only two tokens are considered while swapping?
Is this the reason the third token does not get affected?
Yes it is