Solved

Convert awk statements to variables

Posted on 2008-10-07
4
1,054 Views
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:

./testme.sh: line 35: username1:mypassword1:database_name1: No such file or directory
./testme.sh: line 36: username1:mypassword1:database_name1: No such file or directory
./testme.sh: 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...
 
username1:mypassword1:database_name1
username2:mypassword2:database_name2
 
 
Script to read in datafile and extract variables...
 
#!/bin/sh
#
site_list=/home/mysql/sites/mysql.accts
n=1
 
#
# Gather info about each site (one at a time) & extract variables
#
for site in $(awk '{print $1}' < "$site_list" )
do
 
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"
 
done

Open in new window

0
Comment
Question by:Michael Worsham
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 4

Expert Comment

by:cflong
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

0
 
LVL 4

Accepted Solution

by:
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

0
 
LVL 14

Expert Comment

by:agriesser
ID: 22660011
Does that meet your requirements?
#!/bin/sh
 
site_list=/home/mysql/sites/mysql.accts
 
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

0
 
LVL 29

Author Closing Comment

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

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

726 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