I have a text file of some special characters:
L$+M)jX];&?+jip=ZjJ#}^yV5;fBQ=$L+tG=#x4kDh('t%BWBG/P|U9M`,pcII*6G<r
I wanted to remove some of the characters with a similar amount of space characters removed. remove this fBQ=$L+tG=#x4kDh('t%BWBG/P|U9M
I want to replace with a similar number of characters.
according to characters counter:
L$+M)jX];&?+jip=ZjJ#}^yV5;fBQ=$L+tG=#x4kDh('t%BWBG/P|U9M`,pcII*6G<r
is 68 characters. I want to replace fBQ=$L+tG=#x4kDh('t%BWBG/P|U9M
with the same 30 characters of blank space.
答案1
You can use sed
for this. It is just a little bit tricky because you have a '
in your pattern. This means you need to use "
for the sed. Also, you have a $
, which will need to be escaped so that sed
doesn't see it as "end of line" and also, since we will be using double quotes, so that the shell doesn't see it as a variable. Finally, because you also have a /
, we will need to use another character in the replacement operator. With all this in mind, this should do what you want:
$ sed "s:fBQ=\$L+tG=#x4kDh('t%BWBG/P|U9M: :" file
L$+M)jX];&?+jip=ZjJ#}^yV5; `,pcII*6G<r
For a more general solution, where you can pass any string in a variable and replace that, you could try something like this:
$ export var="fBQ=\$L+tG=#x4kDh('t%BWBG/P|U9M"
$ perl -pe '$f=quotemeta($ENV{var}); s/$f/" " x length($f)/e' file
L$+M)jX];&?+jip=ZjJ#}^yV5; `,pcII*6G<r
答案2
Is this what you're trying to do - replace a string with blanks?
$ cat str
fBQ=$L+tG=#x4kDh('t%BWBG/P|U9M
$ cat file
L$+M)jX];&?+jip=ZjJ#}^yV5;fBQ=$L+tG=#x4kDh('t%BWBG/P|U9M`,pcII*6G<r
$ awk 'NR==FNR{str=$0; lgth=length(str); next} s=index($0,str){$0=substr($0,1,s-1) sprintf("%*s",lgth,"") substr($0,s+lgth)} 1' str file
L$+M)jX];&?+jip=ZjJ#}^yV5; `,pcII*6G<r
The above will work using any awk in any shell on all UNIX boxes for all characters in the string except newline.