1

I'm reposting this question with more context and examples because admittedly my last post was rushed. I'm trying to find ALL MD5 hashes (based off the regex) and simply lowercase them (regardless of format, and regardless of what else is on the line).

I previously posted a similar question related to lowercasing emails and this was the solved answer (maybe this can assist in finding the answer).

sed -e 's/^\([^@]*\)@/\L\1@/' file

The MD5 Regex I use is: [a-f0-9]{32}

I'll provide some examples now (each line is unique - there's NOT a set pattern)

Input data:

fwefwe:few32rfwe:3r2frewg:-::d3ewStack:D077F244DEF8A70E5EA758BD8352FCD8
fwefwe:few33rfwe:3r2frewg:-::dsasaewStack:06D80EB0C50B49A509B49F2424E8C805
fwefwe:few34rfwe:3r2f3213ef::2d3ewStack:F1BDF5ED1D7AD7EDE4E3809BD35644B0
fwefwe:few35rf32re4frewgre3frewg:-::d3ewStack:DDE2C7AD63AD86D6A18DE781205D194F

Output data:

fwefwe:few32rfwe:3r2frewg:-::d3ewStack:d077f244def8a70e5ea758bd8352fcd8
fwefwe:few33rfwe:3r2frewg:-::dsasaewStack:06d80eb0c50b49a509b49f2424e8c805
fwefwe:few34rfwe:3r2f3213ef::2d3ewStack:f1bdf5ed1d7ad7ede4e3809bd35644b0
fwefwe:few35rf32re4frewgre3frewg:-::d3ewStack:dde2c7ad63ad86d6a18de781205d194f
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • I don't think this is easy to do in `sed`. Do you have to use that? – Barmar Jan 20 '20 at 08:13
  • `sed` has the `y` command for doing translations, but I don't think you can restrict it to just the part of the line that matches the regexp. – Barmar Jan 20 '20 at 08:14
  • No not necessarily, I just thought it'd be easiest – StackStackAndStack Jan 20 '20 at 08:15
  • 1
    Your [previous question](https://stackoverflow.com/questions/59817589/lowercase-md5-hash) was closed as duplicate to [this one about lowercasing strings](https://stackoverflow.com/questions/2264428/how-to-convert-a-string-to-lower-case-in-bash) - why does that one not help? – VLAZ Jan 20 '20 at 08:15
  • Because I'm not lowercasing a singular string. It's a regex, which that question doesn't provide an answer too. – StackStackAndStack Jan 20 '20 at 08:16
  • @StackStackAndStack Your example above about lowercasing an email uses a regexp. Why can't you use the same method? – Barmar Jan 20 '20 at 08:17
  • @StackStackAndStack the accepted answer has a `sed` snippet that searches and replaces by regex. – VLAZ Jan 20 '20 at 08:17
  • I'm not trying to lowercase all of the data. Only the MD5 hashes. And I don't know how to incorporate the Regex into a command – StackStackAndStack Jan 20 '20 at 08:17
  • Do you want to match `[A-F0-9]{32}` at the end of a string? Or at the end of the string after `:`? – Wiktor Stribiżew Jan 20 '20 at 08:19
  • @StackStackAndStack It seems like you don't really understand how the email replacement is working, or it would be obvious how to do the same thing here. You need to learn how things work, not just copy them without understanding. – Barmar Jan 20 '20 at 08:22
  • @WiktorStribiżew [A-F0-9]{32} can appear anywhere on each line, there isnt a strict format. I'm just looking to lowercase it – StackStackAndStack Jan 20 '20 at 08:25
  • Then you already have an answer, you just need to use `-E`, not `-e`. `sed -E 's/[A-F0-9]{32}/\L&/g'` – Wiktor Stribiżew Jan 20 '20 at 08:27
  • Correct solution, thankyou! Maybe add it as an official comment, and I can Mark as Solved – StackStackAndStack Jan 20 '20 at 08:28
  • But do not edit the question for solution announcement please. – Yunnosch Jul 09 '22 at 12:09

2 Answers2

3

You may use

sed -E 's/[A-F0-9]{32}/\L&/g' file

If you also want to modify the existing file add -i option:

sed -i -E 's/[A-F0-9]{32}/\L&/g' file

The point is that you need to use an -E option to enable POSIX ERE regex syntax and write {...} quantifier without escaping. POSIX BRE equivalent will look like sed -i 's/[A-F0-9]\{32\}/\L&/g' file.

Also, I added g flag to modify all match occurrences on a line.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
1

You do it the same way as you did when lowercasing the email. Use the regexp to match part of the line, then use a back-reference in the replacement to pick that up.

sed -e 's/[a-fA-F0-9]\{32\}/\L&/' file

In the replacement, & is replaced with whatever matched the regexp, and \L lowercases it.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    Psst, you were missing the escaping of `{` and `}`. `sed` is weird that way `sed -e 's/[a-fA-F0-9]\{32\}/\L&/g'` – VLAZ Jan 20 '20 at 08:31