3

I want to extract username from email. Example: johndoe@gmail.com The domain will always be @gmail.com, but the length of username can be different But I must use substring in linux bash (no grep, sed, cut, and others) like:

str="johndoe@gmail.com"
echo ${str:position:length}

OR maybe using loop like:

length=`expr length "$str"`
for ((i=0; i<$length; i++))
do
    echo ${str:$i:length}
done

The problem is, what should i write in the position and length. How do the code know if I it needs to extract all letters before '@'. Any idea? Thanks!

Cyrus
  • 84,225
  • 14
  • 89
  • 153
ilovecode
  • 35
  • 4
  • For what it's worth, `expr` is basically superseded if you are writing Bash scripts. There are places in POSIX `sh` where you might still need it, but Bash has built-in functionality which replaces basically all of `expr`. – tripleee May 25 '21 at 06:01

1 Answers1

10

Instead of worrying about the position, remove everything from the @ onward.

$ str="johndoe@gmail.com"
$ echo "${str%@*}"
johndoe

Sidenote: Normally it's best to use printf '%s\n' to safely print unknown data, but I'm assuming these are valid Gmail usernames, which are already safe, so echo is fine. The format is something like [a-z0-9][a-z0-9.]{4,28}[a-z0-9] (regex).

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • I think @ is a valid character in the name portion of an email address so I think you need to remove from the last @ onwards. – Jerry Jeremiah May 25 '21 at 03:34
  • @Jerry This actually does remove from the last `@` onwards, but it's irrelevant for Gmail since `@` isn't valid in the username. Reference: [Create a username - Gmail Help](https://support.google.com/mail/answer/9211434?hl=en#) *"Usernames can contain letters (a-z), numbers (0-9), and periods (.)."* – wjandrea May 25 '21 at 03:37
  • I didn't notice that it removed from the last @ onwards so I shouldn't have mentioned it at all. I guess it doesn't really matter anyway because https://en.wikipedia.org/wiki/Email_address#Local-part says that @ can be in the "local-part" of an email address if it is in a portion that is a quoted string and I figure the odds of anyone using that are minuscule (non-existent?) – Jerry Jeremiah May 25 '21 at 03:44
  • 1
    @JerryJeremiah There are many extremely hairy corner cases in what the RFC actually permits. Whether the OP's use case requires strict RFC conformance or not is unclear, but most real systems tend to ignore or reject a good deal of the dark corners which are technically permitted. – tripleee May 25 '21 at 05:58
  • Maybe also point out why [quoting is important.](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – tripleee May 25 '21 at 05:59