Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete character in nth position

Status
Not open for further replies.

viclap

Programmer
Oct 12, 2000
39
0
0
PH
Hi to all,

i have this kind of data:

60 , 70 , 90 , 27,345 , 10
30 , 40 , 100 , 38,900 , 12
41 , 23 , 98 , 21,200 , 09

I've wanted to remove the comma in 4th position to be able to come up with below output using perl:

60 , 70 , 90 , 27345 , 10
30 , 40 , 100 , 38900 , 12
41 , 23 , 98 , 21200 , 09

Hope someone could enlighten me on this. TIA.
 
Hi

Must it be Perl ? For this particular case Sed has a special trick ( as far as I know ) not available in Perl :
Code:
sed 's/,//4' /input/file

Feherke.
[link feherke.github.com/][/url]
 
Hi

The above Sed code's closest Perl equivalent could be this :
Code:
perl -naF'\s*,\s*' -e '[b]splice[/b][navy]@F[/navy][teal],[/teal][purple]3[/purple][teal],[/teal][purple]2[/purple][teal],[/teal][navy]$F[/navy][teal][[/teal][purple]3[/purple][teal]].[/teal][navy]$F[/navy][teal][[/teal][purple]4[/purple][teal]];[/teal][b]print[/b] [b]join[/b][green][i]" , "[/i][/green][teal],[/teal][navy]@F[/navy]' /input/file

But in case your problem can be rephrased as "remove the comma which is not surrounded by spaces", it can be reduced to :
Code:
perl -pe '[b]s[/b][green][i]/(?<!\s),(?!\s)//[/i][/green]' /input/file


Feherke.
[link feherke.github.com/][/url]
 
hi Feherke,

Thanks once again. solution 1 and 2 really works . Great!!!
 
Hi

Feherke said:
The above Sed code's closest Perl equivalent could be this :
Code:
perl -naF'\s*,\s*' -e '[b]splice[/b][navy]@F[/navy][teal],[/teal][purple]3[/purple][teal],[/teal][purple]2[/purple][teal],[/teal][navy]$F[/navy][teal][[/teal][purple]3[/purple][teal]].[/teal][navy]$F[/navy][teal][[/teal][purple]4[/purple][teal]];[/teal][b]print[/b] [b]join[/b][green][i]" , "[/i][/green][teal],[/teal][navy]@F[/navy]' /input/file
Actually that was wrong wording. The Sed code's equivalent whould be something like this :
Code:
perl -pe '[navy]$n[/navy][teal]=[/teal][purple]0[/purple][teal];[/teal][b]s[/b][green][i]/\s*,\s*/$n++==3?"":$&/[/i][/green]ge' /input/file


Feherke.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top