I have a simple question – how do you re-write the following text with awk or similar command:
from
text1|number1;number2
text2|number3;number4
into
text1|number1
text1|number2
text2|number3
text2|number4
It can be unlimited number of numbers but always one text.
If there is varying number of number arguments – then I can’t set whether it is $2, $3 or $infinity. How to be with it? Some line can have 1, 2, and some 40 numbers. Maybe awk is not the best choice here and python can suit more. Any suggestions are welcome?
>Solution :
Using awk (actually GNU Awk 5.1.0, API: 3.0 (GNU MPFR 4.1.0, GNU MP 6.2.1)):
awk 'BEGIN{FS=OFS="|"}{split($2, a, ";"); for(i=1;i<=length(a);i++){print $1,a[i]}}' yourfile
This is doing the following:
- Set the Field Separator (FS) and Output Field Separator (OFS) to a pipe:
BEGIN{FS=OFS="|"} - Split the second field (the semicolon delimited list of numbers) to an array named
a:split($2, a, ";") - Loop through the array printing out the first column and the array element to a new line for each iteration:
for(i=1;i<=length(a);++i){print $1,a[i]}
Best part is you can tell everyone you solved this using a[i]. I’ll see myself out.