Hi Friends,
I am trying to scan line by line using awk and pull the values and pass it in variables and then will use the variables but doesn't work.
Please see below for details.
#more dbtest.sh
----------------------------------
#!/bin/bash
. $HOME/.bash_profile
while read line
do
export DBNAME=`cat dblist.txt | awk '{print $1}'`
export USERNAME=`cat dblist.txt | awk '{print $2}'`
export PASSWORD=`cat dblist.txt | awk '{print $3}'`
echo "$DBNAME , $USERNAME , $PASSWORD"
done < dblist.txt
#more dblist.txt
test system managertest
dev system managerdev
Required output
---------------------
test , system , managertest
dev , system , managerdev
Current output:
-----------------
[oracle@test01.com] sh dbtest.sh
test
dev , system
system , managertest
managerdev
test
dev , system
system , managertest
managerdev
Whats the wong in the script , Kindly help.
Regards,
DB
Yoda
2
You don't need awk
to do this. Simply read values into variables in while
loop:
while read DBNAME USERNAME PASSWORD
do
echo "$DBNAME , $USERNAME , $PASSWORD"
done < dblist.txt
---------- Post updated at 21:54 ---------- Previous update was at 21:29 ----------
BTW the problem with your script is that you are reading lines in variable: line
Later you are using cat
which is wrong:
while read line
do
export DBNAME=`cat dblist.txt | awk '{print $1}'`
export USERNAME=`cat dblist.txt | awk '{print $2}'`
export PASSWORD=`cat dblist.txt | awk '{print $3}'`
echo "$DBNAME , $USERNAME , $PASSWORD"
done < dblist.txt
You should replace it with echo "$line"
Hi bipinajith,
Thanks a lot .
so based on the seperator i have to pass the value in after while read right?
since i am using tab seperated in my file i am using
"while read DBNAME USERNAME PASSWORD"
in case if i user comma seperated in my file the i have to use
"while read DBNAME,USERNAME,PASSWORD"
Is it Right?
Regards,
DB
Yoda
4
No, that is not correct.
If your file has comma separated entries, then use IFS
while IFS=, read DBNAME USERNAME PASSWORD
Hi bipinajith,
#more dblist.txt
test;system;managertest
dev;system;managerdev
#more dbtest.sh
---------------------
while read DBNAME;USERNAME;PASSWORD
do
echo "${USERNAME}/${PASSWORD}@${DBNAME}"
echo "
done < dblist.txt
While executing getting error:
---------------------------------
#sh dbtest.sh
dbtest.sh: line 1: USERNAME: command not found
dbtest.sh: line 1: PASSWORD: command not found
Yoda
6
I repeat, use IFS
while IFS=";" read DBNAME USERNAME PASSWORD
do
echo "${USERNAME}/${PASSWORD}@${DBNAME}"
done < dblist.txt
Hi bipinajith,
Excellent . Thanks a lot.
Regards,
DB