How to pause a while loop while reading from a file


 
Thread Tools Search this Thread
Operating Systems AIX How to pause a while loop while reading from a file
# 1  
Old 03-01-2010
How to pause a while loop while reading from a file

Hi,
I am building a script to grep for a string in all the files from a folder and display the results.
I am reading the files one by one by placing the names in other file using while loop
my code is as below
Code:
     while read inp
     do
          chk=`grep -c "$str" $pth/$inp`
          if [ $chk -ge 1 ]; then
               tot=`expr $tot + $chk`
               mwl=`expr $mwl + 1`
               pg=`expr $pg + $chk`
               tput bold
              echo " \n $inp        --------- STRING(S) FOUND ----------- \n "
               tput sgr0
               grep -n "$str" $pth/$inp
          else
               mwol=`expr $mwol + 1`
          fi
     done<tmp

but the results are scrolled down when i run the script, i want to pause the loop for a key stroke before going to second page based on number of lines printed captured in the variable "pg".

Unix gurus please help me in this regard.

Thanks and regards,
Somasekhar Gajjala.
# 2  
Old 03-01-2010
If you want to see all the output generated by your script,you use the following command.

Code:
sh scriptname |less

# 3  
Old 03-01-2010
You can either call your script in a pipeline with some formatting command ('more' or 'pg' are the standard commands for this, 'less' will work the same if it is installed), as vivekraj already mentioned.

You could also "build in" this mechanism into your script:

Code:
...
while .... ; do
     ...
     <some long output generated here>
     .....
done | more

Still, it is more "unixish" to use the former and not this method, as it will be more versatile: suppose you would want to work on the output with yet another script you could do that with the former method by exchanging "more" with some other program, say some "script2.sh". This would not be possible if you incorporate the "more" into your script.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sequential Reading from two file in a loop

Hello All, I have two files with me file1.txt and file2.txt file1.txt has: 333 222 111 file2.txt has ccc bbb aaa ccc is related to 333 only, bbb is related to 222 only and aaa is related to 111 only. I have to get the values from each of the file and pass them in the URL... (3 Replies)
Discussion started by: ankur328
3 Replies

2. Shell Programming and Scripting

Removing \r and \n during reading file through while loop

Hi, I am writing in a file through cat command. This file will contain the path of file along with filename. e.g. /home/user/folder1/folder2/filename.txt There might be very large number of this path in same file like say 140 when I try to run while command: while read -r file do //command... (8 Replies)
Discussion started by: Pulkit Lall
8 Replies

3. Shell Programming and Scripting

While loop reading file with multiple conditions

Hi Am trying to print the PIDs of process in a file and trying to grep any PID from that file I set the if condition as $value != "PID" and $value != "-" Assign that number to a variable Am confused since am using while loop to read the line from file and again if condition to check those... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

4. Shell Programming and Scripting

Need help with perl script with a while loop reading file

Good morning, I appreciate any assistance that I can get from the monks out there. I am able to get this to work for me so that I can do a hostname lookup if I only specify one hostname in the script. What I want to do is have a file with hostnames and do lookups for each name in the file. Here is... (1 Reply)
Discussion started by: brianjb
1 Replies

5. Shell Programming and Scripting

Loop is not reading tabs from the file

Hi, I am on HP-UX and K shell. When I am using while/for loop for reading a file. It is working fine but not reading tabs: Suppose, if the line is: ; ;COMP; ; ; ; then loop is reading as ; ;COMP; ;... (5 Replies)
Discussion started by: ezee
5 Replies

6. Shell Programming and Scripting

How to get the modified value of variable outside the while loop reading from a file

Hi Friends , Sorry if this is a repeated question , The input file contains 5 lines , so the the values of the variables i and count should b i=5; count=15 but the variables are not updating , the value of variables showing i=0 and count =0 only.:mad: can any1 help me please. (11 Replies)
Discussion started by: babusek
11 Replies

7. Shell Programming and Scripting

Not access variable outside loop when a reading a file

I am writing a shell script using the korn shell. It seems that I am only able to use local variables within a while loop that is reading a file. (I can't access a variable outside a previously used while loop.) It's been a while since I wrote shell scripts. Here is a sample cat file.txt... (4 Replies)
Discussion started by: ricardo.ludwig
4 Replies

8. UNIX for Dummies Questions & Answers

How to make a loop base on reading a file?

To make it clearer: I have a file, List.txt List.txt contains: (these are actually splitted files w/c I got from ls command and dump them to the List.txt file) SAMPLEa SAMPLEb SAMPLEc SAMPLEd SAMPLEe SAMPLEf . . . . . And I want to rename these files to have a .dat extension.... (3 Replies)
Discussion started by: JohnBalayo
3 Replies

9. HP-UX

How to make a loop base on reading a file?

Need help on making a loop script base on what is inside a file... File to read: List.txt List.txt contains below w/c are file name as well: SAMPLEa SAMPLEb SAMPLEc SAMPLEd SAMPLEe SAMPLEf . . . Want to make a loop that will manipulate those that are inside the file.txt w/c are... (3 Replies)
Discussion started by: JohnBalayo
3 Replies
Login or Register to Ask a Question