For example, this is the md5sum command outputs:
35c9581c469c366e286be708a9d9f939 input.txt
This is the command line I'm running:
md5sum input.txt | sed -f script.sed
I want to make the output of md5sum look like this:
35c9xxxxxxxxxxxxxxxxxxxxxxxxxxxx input.txt
Currently, my .sed script contains this, which redacts everything except the first 4 chars:
s/./x/5g
I need to get it to stop doing this when it sees the space "
". Any thoughts?
Best Answer
If (md5sum) hashes; then this would do the job (in a shell that supports brace-expansion)
else for any character length until first character space seen; then:
Using
sed
, and as a general solution::a
is a sed label we nameda
s/
substitute below matches^
is start of line anchor(
opens a group match.
matches a single character.{4}
matches 4 characters (or just....
) followed byx{0,}
0-or-morex
characters (orx*
))
end of group match and back-reference is\1
[^x ]
matches any character except charactersx
,^
negate the match if it's first character immediately after open[
in[^...]
);/
with below\1
was back-reference to parts matched above within(...)
, so we return back the first part again to its place but evetime extrax
s added into it.x
; remember at above we didn't capture[^x ]
within a group-match(...)
? these are the characters we are replacing them withx
now one by one./
end of substitution;
start a new commandta
jump to labela
if substitution was successful; if not, means we replaced all characters intox
and now none left to replace and end thesed
command.