用'X'替换一行中两个字符串之间的所有字符 [英] Replace all characters between two strings in a line by 'X'
问题描述
我想用相同数量的 X
字符更改(通过 bash 脚本使用 SED 命令)两个给定字符串之间的所有字符.如果说,第一个字符串是 %26Name%3d
而最后一个字符串是 %26
,那么这两个字符串之间的任何内容我都想用相同数量的 X
字符.我正在读取文件.该文件大约为 1MB.
I want to change (via bash script using SED command) all the characters in between two given strings with an equal number of X
characters. If say, the first string is %26Name%3d
and the last sting is %26
, then anything in between these two strings I want to replace with an equal number of X
characters. I am reading a file in place. The file is roughly 1MB.
所以
SOMETHING%26Name%3dTHISTEXTNEEDSTOBE%26ELSE
到
SOMETHING%26Name%3dXXXXXXXXXXXXXXXXX%26ELSE
OR 如果起始字符串是 %26Last%3d
并且结束字符串是 %26
OR if start string is %26Last%3d
and ending string is %26
something%26Last%3d%2%2%4%2%4%3%5%%2%2%2%26else
到
something%26Last%3dXXXXXXXXXXXXXXXXXXXXX%26else
我在我的 bash 脚本中使用 sed
命令在下面尝试过,但效果不佳.
I tried below using the sed
command in my bash script but didn't worked perfectly.
file=mylog.log
myAnd=\\%26
myEqual=\\%3d
startList=Name\|Last
end_str="\%26"
search_str="$myAnd""(""$startList"")""$myEqual"
sed -i -E ':a; s/('"$search_str"'X*)[^X](.*'"$end_str"')/\1X\2/; ta' "$file"
PS startList=Name\|Last
以上声明以及 myAnd
和 myEqual
.
PS startList=Name\|Last
above declaration and also of myAnd
and myEqual
.
示例:
SOME%26Name%3dTHISTEXTNEEDSTOBE%26Last%3dDUTTA
to
SOME%26Name%3dXXXXXXXXXXXXXXXXX%26Last%3dXXXXX
推荐答案
这是一个 sed 解决方案:
Here is a sed solution:
sed -e :a -e 's/\(%26Name%3d[X]*\)[^X]\(.*%26\)/\1X\2/;ta'
这篇关于用'X'替换一行中两个字符串之间的所有字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!