Linux – Replace all characters until specific character seen

linuxsedtext processing

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)

sed -e's/./x/'{5..32} infile

else for any character length until first character space seen; then:

Using sed, and as a general solution:

sed -E ':a s/^(.{4}x{0,})[^x ]/\1x/;ta' infile
  • :a is a sed label we named a
  • s/ substitute below matches
    • ^ is start of line anchor
    • ( opens a group match
      • . matches a single character
      • .{4} matches 4 characters (or just ....) followed by
      • x{0,} 0-or-more x characters (or x*)
    • ) end of group match and back-reference is \1
    • [^x ] matches any character except characters x, (in character class the ^ 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 extra xs added into it.
    • then a x; remember at above we didn't capture [^x ] within a group-match (...)? these are the characters we are replacing them with x now one by one.
  • / end of substitution
  • ; start a new command
  • ta jump to label a if substitution was successful; if not, means we replaced all characters into x and now none left to replace and end the sed command.
Related Question