开发者

how to replace particular field in file

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 appe开发者_StackOverflow中文版nd back into file


This one-liner should do it:

 perl -i.bak -pe 's/(?<=no:)\d+/****/' filename


$ perl -pe 's/no:\d+/no:*****/' < input_file > output_file


cat input | perl -lne 's/^(.+)no:(\d+)(.*)/print"$1no:","*" x length($2),"$3"/e' > output


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜