Convert awk statements to variables

Posted on 2008-10-07
Last Modified: 2013-12-21
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

Question by:Michael Worsham
  • 2

Expert Comment

ID: 22659937
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


Accepted Solution

cflong earned 500 total points
ID: 22659995
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

LVL 14

Expert Comment

ID: 22660011
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

LVL 29

Author Closing Comment

by:Michael Worsham
ID: 31503859
You are a god! Thanks!

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Powershell Timeout - Exchange 3 73
Linux script delete files 3 47
Connect to CentOS (in a local VMWare VM) using Putty 7 62
SSL/TLS - openssl troubleshooting 3 39
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question