Convert awk statements to variables

I am trying to read in a simple colon delimited datafile (i.e. mysql.accts), parse through it with awk to extract out the variables for the MySQL account username, password and database name.

My awk skills are a bit rusty, but when I kick off the script, I am encountering this error message:

./ line 35: username1:mypassword1:database_name1: No such file or directory
./ line 36: username1:mypassword1:database_name1: No such file or directory
./ line 37: username1:mypassword1:database_name1: No such file or directory

I have attached portions of the code via snippets for reference needs.

mysql.sites datafile...
Script to read in datafile and extract variables...
# Gather info about each site (one at a time) & extract variables
for site in $(awk '{print $1}' < "$site_list" )
DBUSER=$(awk -F: '{print $1}' < "$site" )
DBPASS=$(awk -F: '{print $2}' < "$site" )
DBNAME=$(awk -F: '{print $3}' < "$site" )
echo "$DBUSER -- $DBPASS -- $DBNAME"
   let "n += 1"

Open in new window

LVL 29
Michael WorshamInfrastructure / Solutions ArchitectAsked:
Who is Participating?
cflongConnect With a Mentor Commented:
In addition, the reason for the errors is that when you direct stdin with <, shell epects a filename.  Since you have the text in a variable, you can echo that and pipe it to awk
DBUSER=$(echo ${site} | awk -F: '{print $1}' )
DBPASS=$(echo ${site} | awk -F: '{print $2}' )
DBNAME=$(echo ${site} | awk -F: '{print $3}' )

Open in new window

It looks like you're only passing in the first collumn instead of the entire line?  Try building your site variable with collumn $0 (all collumns)
for site in $(awk '{print $0}' < "$site_list" )

Open in new window

Does that meet your requirements?
while read LINE; do
  DBUSER=$(echo $LINE | awk -F: '{ print $1 }')
  DBPASS=$(echo $LINE | awk -F: '{ print $2 }')
  DBNAME=$(echo $LINE | awk -F: '{ print $3 }')
  echo $DBUSER -- $DBPASS -- $DBNAME
done < $site_list

Open in new window

Michael WorshamInfrastructure / Solutions ArchitectAuthor Commented:
You are a god! Thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.