0

I would like to have the following in a text file

name=missy [email protected]

Is it possible to read that into a text file and be able to call the variables $name and $email

Or what is the best way of doing this?

Thank you for all the help

2
  • I'm not sure I understand your question correctly. Are you trying to read from a text file, or to write into it ? Commented Nov 26, 2014 at 23:18
  • Read from it and use in a bash script Commented Nov 26, 2014 at 23:22

1 Answer 1

1

Sure, this is the first way that comes to mind:

#!bash
# set for loop separator to newline
IFS=$'\n'
# loop through each line in the file
for userline in $(cat email_list.txt); do
    # Cut delimiter is a space for the next two lines
    # echo the line into cut, grab the first field as the user
    user=$(echo "$userline" | cut -d' ' -f1)
    # echo the line into cut, grab the second field as the email
    email=$(echo "$userline" | cut -d' ' -f2)

    # Set the delimiter an =, grab field 2
    user=$(echo "$user" | cut -d'=' -f2)
    # Set the delimiter an =, grab field 2
    email=$(echo "$email" | cut -d'=' -f2)

    echo "Username: ${user}, email address: ${email}"
done

email_list.txt:

name=missy [email protected]
name=joe [email protected]

Output:

Username: missy, email address: [email protected]
Username: joe, email address: [email protected]
Sign up to request clarification or add additional context in comments.

1 Comment

There are many ways to do this. However, it depends on what you are doing with it, and how many records you have in your file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.