$ cat ip.txt test1,test2,test3 test4,test5 test6,test7,test8,test9,test10 test11,test12,test13,test14 test15 $ perl -F, -ane 'print "$F[0]"; print ",".join(";",@F[1..$#F]) if($#F > 0)' ip.txt test1,test2;test3 test4,test5 test6,test7;test8;test9;test10 test11,test12;test13;test14 test15
Another way:
perl -F'/(,)/,$_,2' -ane '$F[2] =~ s/,/;/g; print @F'
/(,)/,$_,2 split $_ (the input line) into two based on , Since (,) is used, it captures the separator as well resulting in three elements as explained below $F[0] gets first field, $F[1] will get , if present $F[2] gets remaining fields if present
Yet another way, emulating sed 's/,/;/2g'
perl -pe '$c=0; s/,/++$c<2 ? $& : ";"/ge' ip.txt
- initialize counter for every line
- when substituting, check counter value as needed
- the
e modifier allows Perl code in replacement section