Reputation: 3
I have an csv file like:
1;2,3,4
5;2,3
etc
I need to get file like:
1;12
1;13
1;14
5;52
5;53
Can i do that without deep programming, maybe something like awk or something. I can do this thing on perl or python, but ш think there is a simpler way.
Upvotes: 0
Views: 62
Reputation: 157990
You can use awk
:
awk -F'[;,]' '{for(i=2;i<=NF;i++)printf "%s;%s%s\n",$1,$1,$i}' a.txt
Explanation
-F';|,'
Split line by ,
or ;
{for(i=2;i<NF;i++)printf "%s;%s%s\n",$1,$1,$i}
Iterate though columns and produce output as desired.Upvotes: 1
Reputation: 195069
how about:
awk -F";" '{sub(/;/,FS $1);gsub(/,/,ORS $1";"$1)}7' file
test with your data:
kent$ echo "1;2,3,4
5;2,3"|awk -F";" '{sub(/;/,FS $1);gsub(/,/,ORS $1";"$1)}7'
1;12
1;13
1;14
5;52
5;53
or:
awk -F";" 'sub(/;/,FS $1)+gsub(/,/,ORS $1";"$1)' file
Upvotes: 1
Reputation: 75488
awk -F '[;,]' '{ for (i = 2; i <= NF; ++i) print $1 ";" $1 $i }' file
Output:
1;12
1;13
1;14
5;52
5;53
Upvotes: 2
Reputation: 289725
This is a way:
$ awk 'BEGIN{FS=OFS=";"}{n=split($2, a, ","); for (i=1; i<=n; i++) print $1, $1a[i]}' file
1;12
1;13
1;14
5;52
5;53
BEGIN{FS=OFS=";"}
set input and output field separator as ;
.{n=split($2, a, ",")
slice the second field based on comma. The pieces are stored in the array a[]
.for (i=1; i<=n; i++) print $1, $1a[i]}
loop through the fields in a[]
printing them together with the first field on the format FIRST_FIELD;FIRST_FIELD + a[i]Upvotes: 2