When you say \w+
, I assume you actually mean [a-zA-Z]
since \w
also matches numbers and underscores and it looks like you only want letters. In fact, based on your example, you might only want capital letters. Finally, and again I'm guessing here since you don't explain, it looks like you want to exclude cases where the letters string is not exactly 3 characters long. If so, here's a way of doing what you want in Perl:
$ perl -lne '/^(.*?),[A-Z]{3},\d{2},/; $start=$1; @k=(/,(\w{3},\d{2})/g); print "$start,", join("|",@k)' file
1,123-456,IND,91|UAE,97|USA,01
3,345-678,AUS,61|SLA,94|NZW,64
Alternatively, and assuming you only want to make the matches after the 2nd field, you can do this in awk:
$ awk -F, -v OFS="," '{
for(i=3;i<=NF;i+=2){
if ($i~/^[A-Z]{3}$/ && $(i+1)~/^[0-9]{2}$/){
k ? k=k"|"$i","$(i+1) : k=$i","$(i+1);
}
} print $1,$2,k; k=""}' file
1,123-456,IND,91|UAE,97|USA,01
3,345-678,AUS,61|SLA,94|NZW,64
3,345-678,AUS,61|SLA,94|NZW,64
, right?SA, 27
disappearing in your output?