in reply to Replacing nth occurrence of string
#!/usr/bin/perl -w use strict; my $delimiter = ';'; my $nth = 3; my $regex = '^(' . ".*$delimiter"x$nth . ')'; while (<DATA>) { s/$regex/$1$delimiter/ && print; } __DATA__ col1;col2;col3;col4 aaa;bbb;ccc;ddd AAA;BBB;CCC;DDD
Regards,col1;col2;col3;;col4 aaa;bbb;ccc;;ddd AAA;BBB;CCC;;DDD
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Replacing nth occurrence of string
by halley (Prior) on Sep 23, 2005 at 15:24 UTC |