awk issue while reading from file in while do


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk issue while reading from file in while do
# 1  
Old 02-07-2013
awk issue while reading from file in while do

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.

Code:
#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

Code:
#more dblist.txt
test system managertest
dev system managerdev

Required output
---------------------
Code:
test , system , managertest
dev , system , managerdev


Current output:

-----------------
Code:
[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

Last edited by Scrutinizer; 02-08-2013 at 04:18 AM.. Reason: code tags
# 2  
Old 02-07-2013
You don't need awk to do this. Simply read values into variables in while loop:
Code:
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:
Code:
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"
# 3  
Old 02-08-2013
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
# 4  
Old 02-08-2013
Quote:
Originally Posted by narunice
in case if i user comma seperated in my file the i have to use

"while read DBNAME,USERNAME,PASSWORD"

Is it Right?
No, that is not correct.

If your file has comma separated entries, then use IFS
Code:
while IFS=, read DBNAME USERNAME PASSWORD

# 5  
Old 02-08-2013
Hi bipinajith,

Code:
#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:
---------------------------------
Code:
#sh dbtest.sh

dbtest.sh: line 1: USERNAME: command not found
dbtest.sh: line 1: PASSWORD: command not found


Last edited by Scrutinizer; 02-08-2013 at 04:19 AM.. Reason: code tags
# 6  
Old 02-08-2013
I repeat, use IFS
Code:
while IFS=";" read DBNAME USERNAME PASSWORD
do
  echo "${USERNAME}/${PASSWORD}@${DBNAME}"
done < dblist.txt

# 7  
Old 02-08-2013
Hi bipinajith,

Excellent . Thanks a lot.

Regards,
DB
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issues in reading file using 'awk'

Dear all, I am using following function of some script to assign variable "JobNo" some value form file $SAMPLE"_status.log" ( generated using the red color command ) crab ntuplize_crab -status -c $SAMPLE >& $SAMPLE"_status.log" & echo $SAMPLE"_status.log" "=====" jobNo=$(awk... (10 Replies)
Discussion started by: emily
10 Replies

2. Shell Programming and Scripting

Reading data from file using awk

I have a file as below. It contains two data sets separated by >. I want to pipe each data set to another program called psxy. How can I get the different records Have started doing as follows but it only passes the first data set awk 'BEGIN {RS=">"};{print $0}' p.dat cat p.dat... (12 Replies)
Discussion started by: kristinu
12 Replies

3. Shell Programming and Scripting

reading file awk or while

While read line query !!! Folks, I am working on a file which has entries as follows. I am using while read line to generate desired output as follows. filename1: Name : sdt2156157_ID NOS : 4567 NOS : 2348 Name : sdt2156158_ID NOS : 4987 NOS :... (3 Replies)
Discussion started by: dynamax
3 Replies

4. Shell Programming and Scripting

awk file reading doubt

Hi, Using this trivial code, I am trying to insert/paste the single column data of a file into the second column (field 2) of a multi-column text file. awk 'FNR==NR {a=$0; next} {$1=$1 OFS a}1' single-column-file multi-column-file Lets consider the single-column-file as 'f2' and multi-column... (1 Reply)
Discussion started by: royalibrahim
1 Replies

5. Shell Programming and Scripting

awk- reading input file twice

Hello, I've been trying to come up with a solution for the following problem; I have an input file with two columns and I want to print as an output the first column without any changes but for the second column, I want to divide it by its last value. Example input: 1 9 2 10 3 11 4 12 5... (14 Replies)
Discussion started by: acsg
14 Replies

6. Shell Programming and Scripting

UNIX File handling -Issue in reading a file

I have been doing automation of daily check activity for a server, i have been using sqls to retrive the data and while loop for reading the data from the file for several activities. BUT i got a show stopper the below one.. where the data is getting store in $temp_file, but not being read by while... (1 Reply)
Discussion started by: KuldeepSinghTCS
1 Replies

7. Shell Programming and Scripting

Reading a file several times with awk

Hi everyone, I was wondering if it's possible to read a file ("file2" in my example) more than once. In this example I want to print file2 entirely for each lines of file1: awk -F$'\t' '{ print $0 while ((getline < "file2") > 0) { print "\t"$0 } }' file1 It... (4 Replies)
Discussion started by: anthony.cros
4 Replies

8. Shell Programming and Scripting

Using awk to when reading a file to search and output to file

Hi, I am not sure if this will work or not. I am getting a syntax error. I am reading fileA, using an acct number field trying to see if it exists in fileB and output to new file. Can anyone tell me if what I am doing will work or should I attempt it another way? Thanks. exec < "${fileA}... (4 Replies)
Discussion started by: ski
4 Replies

9. UNIX for Dummies Questions & Answers

issue on reading the file and appending date

Hi Am having issue on appending time stamp I know the exact file names in the directory like a.dat b.dat c.dat e.dat f.dat I want to read all these file names and append the timestamp to each files like a.dat.20090604,b.dat.20090604 and move to the different directory. ... (3 Replies)
Discussion started by: bobprabhu
3 Replies

10. UNIX for Advanced & Expert Users

Issue reading csv file

HI All I have csv file containing the data like this Electrical Equipment,ElecEquip "Engineering, Machinery & Equipment",Engineerin Entertainment & Broadcasting,Entertain The first and third record are fine,The issue with second records as it has comma enclosed with in inverted... (1 Reply)
Discussion started by: mohdtausifsh
1 Replies
Login or Register to Ask a Question