Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit text format with shell script

I am trying to make a script for text editing. In this case I have a text file named text.csv, which reads:

first;48548a;48954a,48594B
second;58757a;5875b
third;58756a;58576b;5867d;56894d;45864a

I want to make text format to like this:

first;48548a
first;48954a
first;48594B
second;58757a
second;5875b
third;58756a
third;58576b
third;5867d
third;56894d
third;45864a

What is command should I use to make this happen?

like image 649
adhown Avatar asked Dec 26 '22 20:12

adhown


1 Answers

I'd do this in awk.

Assuming your first line should have a ; instead of a ,:

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

Untested.

like image 61
ghoti Avatar answered Jan 08 '23 05:01

ghoti