I have a file with strings like this
1A,1B,1C, 2A,2B,2C,
between the group "1" and the group "2" there is a tab (each string has a different number of elements inside the file); I would need to pair the "A", "B", and "C", and move each pair in a new line; for e.g.
1A-2A
1B-2B
1C-2C
I was looking with AWK or BASH but I can not come out. Any help, thanks.
Perl solution:
perl -F'/\t/' -lne '@c = map [split /,/, $_], @F;
print "$c[0][$_]-$c[1][$_]" for 0 .. $#{ $c[0] }
' < input_file
-n
reads the input line by line;-l
removes newlines from input and appends them to print
;-F
splits each input line on the given regex, here we're using a tab; the @F
array is populated with the results;@c
contains two array references (note that unlike awk, Perl throws away empty trailing fileds by default);$#{ $c[0] }
is the last index of the first array reference.