tags:

views:

60

answers:

4

In a file :

"name": "test","Address": "UK" "currency": "£" no:121212 ,
"name": "test1","Address": "UK" "currency": "£" no:12123212 ,
"name": "test2","Address": "UK" "currency": "£" no:121223212 ,
"name": "test3","Address": "UK" "currency": "£" no:121223212 ,
"name": "test4","Address": "UK" "currency": "£" no:121223212 ,

I want replace all the no into *

"name": "test","Address": "UK" "currency": "£" no:***** ,
"name": "test1","Address": "UK" "currency": "£" no:***** ,
"name": "test2","Address": "UK" "currency": "£" no:***** ,
"name": "test3","Address": "UK" "currency": "£" no:***** ,
"name": "test4","Address": "UK" "currency": "£" no:***** ,

and want append back into file

+3  A: 
$ perl -pe 's/no:\d+/no:*****/' < input_file > output_file
davorg
This will replace the `no:` along with digits
eugene y
I edited to fix that just as you were commenting.
davorg
+1  A: 
cat input | perl -lne 's/^(.+)no:(\d+)(.*)/print"$1no:","*" x length($2),"$3"/e' > output
oraz
+3  A: 

This one-liner should do it:

 perl -i.bak -pe 's/(?<=no:)\d+/****/' filename
eugene y
This creates an extra temp file, which Tree said in a comment that he didn't want to do.
brian d foy
A: 

I might be tempted to use File::Map to change the file in place. If I do that, however, I have to replace the digits one-for-one since this won't move the other characters in the file:

use File::Map qw(map_file);

map_file my $map, 'test.txt', '+<';
$map =~ s/(?<=no:)(\d+)(?=\s*,$)/ '*' x length $1 /meg;
brian d foy