jlope043 has asked for the wisdom of the Perl Monks concerning the following question:
Hello everyone, I am trying to replace whitespaces, from my input file, with commas. The challenge I am currently experiencing is that when a selection of the line has no characters it is not recognizing the selection therefore not adding the extra comma in place of the line. What I want my script to do is, add the comma regardless if there is or isn't characters present in the section of the line. Below is what I currently have, where I'm stating that every 3 whitespaces in my input file per line, replace with a comma. Thank you for your time and help, as always everyone is very helpful on here.
use strict; use warnings; open (NEW, ">", "OUTPUT_COMMA.txt" ) or die "could not open:$!"; open (FILE, "<", "INPUT.txt") or die "could not open:$!"; while (my $line = <FILE>) { $line =~s/\s{3} +/,/g; print NEW $line; } close (FILE); close (NEW)
INPUT File:
12345 DOE,JOHN $50.00 REFUND COMPLETE 12345 DOE,JOHN $25.00 DENIED COMPLETE 12345 DOE,JOHN $75.00 COMPLETE 12345 DOE,JOHN $10.00 REFUND COMPLETE
EXPECTED OUTPUT File:
12345,DOE,JOHN,$50.00,REFUND,COMPLETE 12345,DOE,JOHN,$25.00,DENIED,COMPLETE 12345,DOE,JOHN,$75.00,,COMPLETE 12345,DOE,JOHN,$10.00,REFUND,COMPLETE
CURRENT OUTPUT File:
12345,DOE,JOHN,$50.00,REFUND,COMPLETE 12345,DOE,JOHN,$25.00,DENIED,COMPLETE 12345,DOE,JOHN,$75.00,COMPLETE 12345,DOE,JOHN,$10.00,REFUND,COMPLETE
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Replace Whitespace with Comma
by toolic (Bishop) on Aug 16, 2016 at 19:41 UTC | |
|
Re: Replace Whitespace with Comma
by GrandFather (Saint) on Aug 16, 2016 at 20:44 UTC | |
|
Re: Replace Whitespace with Comma
by Marshall (Canon) on Aug 16, 2016 at 20:46 UTC | |
|
Re: Replace Whitespace with Comma
by choroba (Cardinal) on Aug 16, 2016 at 20:34 UTC | |
|
Re: Replace Whitespace with Comma
by Anonymous Monk on Aug 16, 2016 at 19:46 UTC |