Concatenating two mutiline variables in a bash

Hi All,
I am having a situation where am capturing results in two variables from an xml file. However, I am looking to print those two variables with pipe in between them and these variable are multi-line.
This is how my 1st variable looks like:

20181225010
20190224010
20190224010
20190303010
20190304010
20190306010
20190319010
20190320010
20190321010

This is my 2nd variable

906676175
907339851
907453926
907453962
907687907
909465451
909587811
909276884
913772782

Expected output:

20181225010|906676175
20190224010|907339851
20190224010|907453926
20190303010|907453962
20190304010|907687907
20190306010|909465451
20190319010|909587811
20190320010|909276884
20190321010|913772782

Any assistance would be greatly appreciated.

Hello svks1985,

Could you please try following.

awk 'FNR==NR{a[FNR]=$0;next} {print a[FNR]"|"$0}' <(echo "$var1")  <(echo "$var2")

2nd solution: With paste .

paste -d'|' <(echo "$var1") <(echo "$var2")

Thanks,
R. Singh

1 Like

Thanks much RavinderSingh13.
1st solution is printing the output like this

|20181225010
|20190224010
|20190224010
|20190303010
|20190304010
|20190306010
|20190319010
|20190320010
|20190321010
|858485461
|881854750
|881854752
|886257846
|887682847
|886981196
|894784176
|895564612
|894060226

However paste option is working just fine.

Try also

ARR1=( $VAR1 )
ARR2=( $VAR2 )
for IX in ${!ARR1[@]}; do echo "${ARR1[IX]}|${ARR2[IX]}"; done
20181225010|906676175
20190224010|907339851
20190224010|907453926
20190303010|907453962
20190304010|907687907
20190306010|909465451
20190319010|909587811
20190320010|909276884
20190321010|913772782

Be aware that RavinderSingh13's 2nd proposal requires a recent shell offering "process substitution".

1 Like

Thanks RudiC
Your solution works just fine.
However, I would greatly appreciate if you can provide some explanation around it. To be specific, I am looking for

for IX in ${!ARR1[@]}

man bash :

and

2 Likes

Your assistance is truly appreciated. Thanks much!

1 Like

Also try:

printf "%s\n" "$var1" "$var2" | pr -2ts'|'
1 Like

Try also

awk -vV1="$VAR1" -vV2="$VAR2" 'BEGIN {n = split (V1, T1); split (V2, T2); for (i=1; i<=n; i++) printf "%s|%s\n", T1, T2}'