用等号*替换3个或更多数字


Answers:


3

您可以使用perl

$ echo "abc-1234-45" | perl -pe 's/(\d{3,})/"*" x length($1)/eg'
abc-****-45

/e标志导致perl在替换之前将右侧评估为表达式。


谢谢。这是不可能的,sed或者awk-我的意思是在替换之前将字符串作为表达式求值?

@RaghavenderKavi据我所知,sedawk不能。
cuonglm 2014年

0

GNU AWK

echo 'abc-1234-45' | 
awk --re-interval -v RS='[[:digit:]]{3,}' '{gsub(/[[:digit:]]/, "*", RT);
  printf "%s%s", $0, RT}'
abc-****-45

或者,如果您愿意研究Python

echo 'abc-1234-45' |  python -c 'import re; import sys; print re.sub(r"\d{3,}",\
lambda x: len(x.group())*"*", sys.stdin.readline().strip())'
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.