匹配两个符号之间的字符串并替换

匹配两个符号之间的字符串并替换

我有一个包含许多链接的文件,如下所示:

http://example.com/mall/mall/detail3.jsp?proID=2114280
http://example.com/mall/member/member/bookshelves/add.jsp?proID=3354136&productName=something
http://example.com/mall/bestbookTW//mall/detail3.jsp?proID=3435839&c=532166&id=954325

我希望它变成这样:

http://example.com/mall/mall/detail3.jsp?proID=FUZZ
http://example.com/mall/member/member/bookshelves/add.jsp?proID=FUZZ&productName=FUZZ
http://example.com/mall/bestbookTW//mall/detail3.jsp?proID=FUZZ&c=FUZZ&id=FUZZ

我怎样才能用 sed 或 grep 或 awk 来做到这一点?

答案1

也试试

awk 'gsub("=[^&]*", "=FUZZ")' file 
http://example.com/mall/mall/detail3.jsp?proID=FUZZ
http://example.com/mall/member/member/bookshelves/add.jsp?proID=FUZZ&productName=FUZZ
http://example.com/mall/bestbookTW//mall/detail3.jsp?proID=FUZZ&c=FUZZ&id=FUZZ

答案2

一种perl方法:

$ perl -pe 's/=(.+?)(&|$)/=FUZZ$2/g' file 
http://example.com/mall/mall/detail3.jsp?proID=FUZZ
http://example.com/mall/member/member/bookshelves/add.jsp?proID=FUZZ&productName=FUZZ
http://example.com/mall/bestbookTW//mall/detail3.jsp?proID=FUZZ&c=FUZZ&id=FUZZ

这个想法是将 a 和 后面的所有字符串替换为 ,=直到下一个&或行尾 ( $) FUZZ

要修改原始文件而不是打印到标准输出,请使用-i

perl -i -pe 's/=(.+?)(&|$)/=FUZZ$2/g' file 

或者,使用sed

$ sed -E 's/=([^&]*)/=FUZZ/g' file 
http://example.com/mall/mall/detail3.jsp?proID=FUZZ
http://example.com/mall/member/member/bookshelves/add.jsp?proID=FUZZ&productName=FUZZ
http://example.com/mall/bestbookTW//mall/detail3.jsp?proID=FUZZ&c=FUZZ&id=FUZZ

再次使用-i编辑原始文件:

sed -i -E 's/=([^&]*)/=FUZZ/g' file 

您也可以使用相同的方法perl

perl -i -lpe 's/=([^&]*)/=FUZZ/g' file 

相关内容