in reply to Re: Modify a txt file
in thread Modify a txt file
Hey Dave, Thanks for the advice. Although I am using your code, I am not getting the correct output...
Code I am using:
#!/usr/bin/perl use warnings; use strict; if($#ARGV<0){ die "Usage: $0 <*.txt>\n"; } open(IN,$ARGV[0]) ; $/ = "\n\n"; while ( <IN> ) { if( my( $number ) = m/(\d+)/ ) { s/\b\p{Alpha}+\b/$number/g; } else { warn "Malformed record in input line $.\n$_\nContinuing.\n"; } print; }
Input:
0 ASDF ASEE ASEE 13 DERG DREG 28 QWER QWER 42 WERT WERT WERT 55 QWEASD QWEASD QWEASD QWEASD
Getting this output:
0 0 0 0 13 0 0 28 0 0 42 0 0 0 55 0 0 0 0
Desired Output:
0 0 0 0 13 13 13 28 28 28 42 42 42 42 55 55 55 55 55
I see that it is in this line s/\b\p{Alpha}+\b/$number/g; where the substitution is being made. Is the code referring back to the original 0 in the top left hand column perhaps?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Modify a txt file
by GrandFather (Saint) on Oct 20, 2011 at 07:34 UTC | |
|
Re^3: Modify a txt file
by davido (Cardinal) on Oct 20, 2011 at 06:56 UTC |