Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Text transform with awk

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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:

  1. Set the Field Separator (FS) and Output Field Separator (OFS) to a pipe: BEGIN{FS=OFS="|"}
  2. Split the second field (the semicolon delimited list of numbers) to an array named a: split($2, a, ";")
  3. 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading