Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can you split a string in bash script by starting at a certain char and ending at another

i have an email address and i am wanting to split the email in the first letter of the firstname and full last name and put them in a place holder.
eg:
firstname.lastname@email.com

x = f 
y = lastname

I did use try firstCharacter=${email:0:1} to get the first char but am not sure how to get the last name.

Full code here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#!/bin/bash

file="users.csv.1"
Count=0

while IFS=";"; read email birthDate group sharedFolder
do
        echo -e "Email: $email"
        firstCharacter=${email:0:1}
        echo $firstCharacter
        IFS="."
        read -ra
        echo -e "Birth Date: $birthDate"
        echo -e "Group: $group"
        echo -e "shared Folder: $sharedFolder\n"

done < "$file"

>Solution :

One idea using parameter expansion/substitution:

$ email='firstname.lastname@email.com'
$ lastName="${email//@*}"                   # strip off domain (ie, everything from @ to end of string)
$ lastName="${lastName//*.}"                # strip off everything from start of string up to to and including last period; should address email addresses like 'firstname.mi.lastname'
$ typeset -p lastName
declare -- lastName="lastname"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading