As part of an inventory bash script I am checking SFP modules and adding them to a .csv-file. The format currently looks like this, with varying lengths:

RO01;1000BaseSX SFP;1000BaseSX SFP
RO02;1000BaseSX SFP;1000BaseSX SFP;1000BaseLX SFP

My goal is to give each row after the first its own line together with the first row. So the above line should look like this:

RO01;1000BaseSX SFP
RO01;1000BaseSX SFP
RO02;1000BaseSX SFP
RO02;1000BaseSX SFP
RO02;1000BaseLX SFP

How could I achieve this using text processing?


Possible solution could be with using awk:

awk -F";" '{ for (i = 2; i <= NF; i++) { printf("%s;%s\n", $1, $i); } }' file

With awk -F";" we set FS(field separator) to ;. Then for every row(record) we start from field 2 to the last field (NF): for (i = 2; i <= NF; i++) and we print field 1 and current field ($i).

  • It worked exactly as I wanted, thanks a lot! If it's not troublesome would you care to explain what's happening in this command? – Sebbeleu Jun 22 '15 at 8:34
  • @Sebbeleu - answer edited. – taliezin Jun 22 '15 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.