If you have a lot of data, awk
will be faster than a shell. The sed
solutions are fine, but this works too:
$: awk '-F[;:]' '{ printf "%s;%s\n", tolower($1), $2 }' x
[email protected];exaMple
[email protected];eXmaple
[email protected];exAmple
[email protected];exmaplE
example_example.com;Example
example_example.com;eXmaple
[email protected],example;
That defines the -F
ield separators as a list of ;:
and lowercases the first field. I arbitrarily replaced the delimiter with a standardized ;
- if that doesn't work, this might not be the best solution for you. Stick with the sed
.
sprabhakaran beat me to it with a practically identical sed
solution while I was initially typing, lol. :)
sed
can.
$: cat x
[email protected];exaMple
[email protected]:eXmaple
[email protected];example
[email protected]:exmaple
example_EXAMPLE.com;example
example_EXAMPLE.com:exmaple
[email protected],example
$: sed -E '/@.+[;:]/s/^(.*)@(.*)([;:])(.*)/\1@\L\2\E\3\4/' x
[email protected];exaMple
[email protected]:eXmaple
[email protected];exAmple
[email protected]:exmaplE
example_EXAMPLE.com;Example
example_EXAMPLE.com:eXmaple
[email protected],examPle
\L
says to begin lowercasing until \E
(end) or \U
(begin uppercasing).
This skips lines that don't have both @
and [;:]
(either of ;
or :
.)
for small datasets native bash
might be easier.
It might be a lot simpler however to just downcase the whole thing.
$: declare -l line
$: while read line
> do echo "$line"
> done < x
[email protected];example
[email protected]:exmaple
[email protected];example
[email protected]:exmaple
example_example.com;example
example_example.com:exmaple
[email protected],example
declare -l
make a variable always lowercase anything put in it.
Since case-sensitive passwords prevent that, parse the parts separately.
$: while IFS="$IFS:;" read email pass
> do echo "$email [$pass]"
> done < x
[email protected] [exaMple]
[email protected] [eXmaple]
[email protected] [exAmple]
[email protected] [exmaplE]
example_example.com [Example]
example_example.com [eXmaple]
[email protected],example []
As long as the record is properly formatted it works great.
I assume you can check for errors or trust your data.