Yannis
Yannis

Reputation: 6157

Find by regex and replace match to lowercase in Bash

I would like to replace all contents of a file that match a given regex to their lowercase equivalent. Like:

grep -o '[^ ]*[A-Z][^ ]*.png' file-21-05-2013.sql* | awk '{print tolower($0)}'

The line above finds all strings in the given file that have at least one uppercase character and prints the lowercase equivalent.

I would like to replace the output of the grep command with the output of the whole command above

Does that make sense?

Upvotes: 2

Views: 2885

Answers (2)

Sidharth C. Nadhan
Sidharth C. Nadhan

Reputation: 2253

sed -nr '/^.*\b(\w*[A-Z]\w*\.png).*$/{s//\1/;s/.*/\L\0/;p}' file

Upvotes: 1

Martin Mrazik
Martin Mrazik

Reputation: 386

If you are using a GNU system then GNU sed has the following extension:

\L
Turn the replacement to lowercase until a \U or \E is found, 

The following command should do what you need:

sed  "s/\([^ ]*[A-Z][^ ]*.png\)/\L\1/g" file-21-05-2013.sql*

Upvotes: 2

Related Questions