How to Read the entire file using while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Read the entire file using while loop
# 1  
Old 05-22-2009
How to Read the entire file using while loop

Guys,
I am trying to read the whole file using while loop but when i run the ssh part of the script it reads only the first line and exit after that.

There are in total 134 lines in the file, but when the output is redirected, it does only for one line and comes to command prompt.
pls help..

Code:
FILENAME=/tmp/Parser_list_beauty
while read line
do
PARSER_NAME=`echo $line | awk ' { print $1; } '`
HOST_NAME=`echo $line | awk ' { print $2; } '`
echo "-------------------------------------------------------" 
echo "Extracting information for $PARSER_NAME on $HOST_NAME" 
echo " " 
ssh $HOST_NAME grep STANDBY /ilx/gtp/pkg/$PARSER_NAME/log/$PARSER_NAME.log | tail -2 
echo "-------------------------------------------------------" 
done < $FILENAME >> /tmp/parser_extract.txt 
 
The o/p file is
cat /tmp/parser_extract.txt
-------------------------------------------------------
Extracting information for ASXITSparser on spoonl1
0522 085504 (gtp) STATUS: Operational timer signaling process from UP to STANDBY.
0522 085506 (gtp) STATUS: Operational timer signaled process to STANDBY; in STANDBY state, ignoring signal.
-------------------------------------------------------

While it should reflect all the 134 lines output. pls advise.

Last edited by sdosanjh; 05-22-2009 at 07:13 AM..
# 2  
Old 05-22-2009
To read a file line by line I should use:
Code:
cat $FILENAME | while read line; do 
   PARSER_NAME=`echo $line | awk ' { print $1; } '`
   HOST_NAME=`echo $line | awk ' { print $2; } '`
   #[...]
done

Regards,

Albert.
# 3  
Old 05-22-2009
Remove this part of the ssh command:

Code:
| tail -2

Regards
# 4  
Old 05-22-2009
Hi Franklin and Albert,

Thanks for the reply, but still hard luck. and if i remove tail -2 ; then it may produce lot of o/p since i am greeping the expression from the log files.

Any other way??? pls suggest.
Actually when i run the code without the following line, it works well but i don't know how to handle this ssh part now...

ssh $HOST_NAME grep STANDBY /ilx/gtp/pkg/$PARSER_NAME/log/$PARSER_NAME.log | tail -2
# 5  
Old 05-22-2009
Quote:
Originally Posted by AlbertGM
To read a file line by line I should use:
Code:
cat $FILENAME | while read line; do 
   PARSER_NAME=`echo $line | awk ' { print $1; } '`
   HOST_NAME=`echo $line | awk ' { print $2; } '`
   #[...]
done

Regards,

Albert.
no need to cat. Also , if using awk, no need to use while read loop
Code:
awk '{
   parsername=$1
   hostname=$2
   #some other code
}' $filename

# 6  
Old 05-22-2009
Hard luck

> awk '{
>> PARSER_NAME=$1
>> HOST_NAME=$2
>> ssh -X $HOST_NAME grep STANDBY /ilx/gtp/pkg/$PARSER_NAME/log/$PARSER_NAME.log >> /tmp/parser_extract.txt
>> echo "--------------------------------------------------------------------"
>> echo " "
>> }' $FILENAME
awk: syntax error near line 4
awk: illegal statement near line 4
# 7  
Old 05-22-2009
my issue is ... while loop reads first line only from the input file and then redirects the o/p to output file and then returns to command prompt ignoring the other lines of the file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

User with read-only to entire filesystem

Is this even possible? Its a request for external auditors to login remotely. (5 Replies)
Discussion started by: psychocandy
5 Replies

2. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix. I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice. Files:... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

3. Shell Programming and Scripting

Bash script read specific value from files of an entire folder

Hello, I heva a problem creating a script that read specifc value from all the files of an entire folder I have a number of email files into a directory and i need to extrect from each file 2 specific values. After that i have to put them into a new file that looks like that: To: value1 ... (1 Reply)
Discussion started by: ahmenty
1 Replies

4. Shell Programming and Scripting

Using awk instead of while loop to read file

Hello, I have a huge file, I am currently using while loop to read and do some calculation on it, but it is taking a lot of time. I want to use AWK to read and do those calculations. Please suggest. currently doing: cat input2 | while read var1 do num=`echo $var1 | awk... (6 Replies)
Discussion started by: anand2308
6 Replies

5. Shell Programming and Scripting

Read file using while loop not reading last line

I have written a script to read the file line by line. It is reading and printing the lines. But it is coming out of loop before reading last line. So I am not able to print last line. How do I solve it. (6 Replies)
Discussion started by: dgmm
6 Replies

6. UNIX for Dummies Questions & Answers

How to read entire output from a file?

Hello- I am trying to view a file which is quite large. However, whenever I do 'cat (file name)' it shows me just the half.. I am using Putty to access my server. Also, is it possible to edit a file from a unix system on a 'Gedit for Windows" text editor? Thanks (7 Replies)
Discussion started by: DallasT
7 Replies

7. Shell Programming and Scripting

Problem reading file in while/read loop

I know I should be able to see a way of doing this easily, but my brain just won't engage. I have a script working on an embedded device that checks to see if an item is in a blacklist before performing some actions. At the moment the code reads thus.... while read BLACKLIST ; do ... (7 Replies)
Discussion started by: Bashingaway
7 Replies

8. UNIX for Dummies Questions & Answers

How to read a file in unix using do....done loop

Hi , can some give me idea about how to use do...done while loop in UNIX to read the contents of a file.. (2 Replies)
Discussion started by: sreenusola
2 Replies

9. UNIX for Advanced & Expert Users

Read the entire output fired by ps command

Actually I want to display the entire output fired by ps command. My output gets trucated after 80 chars. Thus, i am not able to see the entire command running when i give a ps -eaf .... Does anyone know how do i display the entire output fired by ps command .. (i.e the command along with... (5 Replies)
Discussion started by: vinithepoo
5 Replies

10. Shell Programming and Scripting

Read from a file and use the strings in a loop

Hello all, I need some help to create a script which contain a few strings on every line, and use those strings in a loop to fire some commands. for exmaple the file looks like tom dave bill andy paul I want to read one line at a time and use it in loop like command tom command dave... (3 Replies)
Discussion started by: xboxer21
3 Replies
Login or Register to Ask a Question