awk -F"|" '{n=split($2,a,","); for(i=1;i<=n;++i){print $1"|"a}}' test.dat
The awk code performs 3 steps for every line that it reads from "test.dat":
Step 1:
Split the line on the "|" character, since -F"|" has been specified. After splitting, the variables $1 and $2 are set to the two values. Each line will have only two values since there is exactly one "|" per line.
Step 2:
Use the "split" function on the value of $2 from Step 1. Use the comma "," as separator here. After splitting, set the values to the array "a". Set the value of "n" to the size of the array "a".
Step 3:
Run the "for" loop from value of i = 1 to "n" that was determined in Step 2. For each iteration, print the value of $1 from Step 1, the pipe character "|" and the value of a[i]. "a" was determined in Step 2 and i is the iterator value.
Once you understand these 3 steps, you can apply that knowledge to a couple of lines read.
---------------------------------------------------
Line 1 => Read "Field1|Field2"
---------------------------------------------------
Step 1:
After splitting on "|" character, value of $1 = Field1 and that of $2 = Field2
Step 2:
After splitting $2 = Field2 on the "," character, the array "a" has only one element. a[1] = Field2 and n = 1.
Step 3:
Loop from i=1 to n i.e. 1. Print $1 then "|" then a[1] i.e. print "Field1|Field2"
---------------------------------------------------
Line 2 => Read "123|345,567,789"
---------------------------------------------------
Step 1:
After splitting on "|" character, value of $1 = 123 and that of $2 = 345,567,789
Step 2:
After splitting $2 = 345,567,789 on the "," character, the array "a" has 3 elements.
a[1] = 345
a[2] = 567
a[3] = 789
n = 3
Step 3:
Loop from i=1 to n i.e. 3.
i = 1 => Print $1 then "|" then a[1] i.e. print "123|345"
i = 2 => Print $1 then "|" then a[2] i.e. print "123|567"
i = 3 => Print $1 then "|" then a[3] i.e. print "123|789"
And so on...
Since -F forces awk to split the line anyway, the "split" function could be avoided like so:
awk -F'[|,]' '{for(i=2;i<=NF;++i){print $1"|"$i}}' test.dat
A test run follows:
$
$ cat test.dat
Field1|Field2
123|345,567,789
234|563,560
345|975,098,985,397,984
456|736
$
$ awk -F'[|,]' '{for(i=2;i<=NF;++i){print $1"|"$i}}' test.dat
Field1|Field2
123|345
123|567
123|789
234|563
234|560
345|975
345|098
345|985
345|397
345|984
456|736
$
$
The OFS variable could also be used, as others have shown.