Visit The New, Modern Unix Linux Community


Problem with while loop reading every line of a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with while loop reading every line of a text file
# 1  
Problem with while loop reading every line of a text file

Hello,

I'm using RHEL 5.1 with bash. How to handle "read" inside while loop reading every line? Please see below:

Code:
#  cat /tmp/passwd_sample
CARRJ12:qVSn4ja4mFA72,..:20021:125:JULIAN CARR:/home/everyone:/bin/bash
HERCOT01:NK/3j2ZB4ZC7Q:20022:125:TOM HERCOCK:/home/everyone:/bin/bash
NIRMALA1:35ltVGpjHq.aM:20025:125:NIRMALA KESAVAN:/home/everyone:/bin/bash
KENNEC07:GR1AlScQIfLeI:20023:125:CARL KENNEALLY:/home/everyone:/bin/bash
STANH001:VTPzaDxM1f3ps:20024:125:OWEN STANHOPE:/home/everyone:/bin/bash
#

The below works as expected:

Code:
# cat /tmp/passwd_sample |while read line
> do
> echo "${line}"
> done
CARRJ12:qVSn4ja4mFA72,..:20021:125:JULIAN CARR:/home/everyone:/bin/bash
HERCOT01:NK/3j2ZB4ZC7Q:20022:125:TOM HERCOCK:/home/everyone:/bin/bash
NIRMALA1:35ltVGpjHq.aM:20025:125:NIRMALA KESAVAN:/home/everyone:/bin/bash
KENNEC07:GR1AlScQIfLeI:20023:125:CARL KENNEALLY:/home/everyone:/bin/bash
STANH001:VTPzaDxM1f3ps:20024:125:OWEN STANHOPE:/home/everyone:/bin/bash
#

But when I use "read" it outputs without any interaction (as read is supposed to wait for our input keys) and skips few lines as shown below:

Code:
# cat /tmp/passwd_sample |while read line
> do
> echo "${line}"
> echo "Press any key to continue ...."
> read any_key
> done
CARRJ12:qVSn4ja4mFA72,..:20021:125:JULIAN CARR:/home/everyone:/bin/bash
Press any key to continue ....
NIRMALA1:35ltVGpjHq.aM:20025:125:NIRMALA KESAVAN:/home/everyone:/bin/bash
Press any key to continue ....
STANH001:VTPzaDxM1f3ps:20024:125:OWEN STANHOPE:/home/everyone:/bin/bash
Press any key to continue ....
#

Please advise on what I'm doing wrong and how to fix it, thanks!!
# 2  
both of these read operations work on the same stdin. you need to assign to a different fd and use that for one of them. the easiest way to do this is to ditch the useless use of cat (uuoc award).

Code:
while read line <&9; do
  echo "$line"
  echo "press enter key"
  read
done 9<passwd_sample

Now we redirect passwd_sample into fd 9 for the entire while block, yet only the first read operation operates on fd 9. in this way, we'll keep our position in the file with each iteration and read line-by-line but not interfere with the read that wants input from the terminal.

if you're using bash, you can use read -s -N 1 for grabbing a single key. otherwise you'll need to press enter rather than "any" key.
These 3 Users Gave Thanks to neutronscott For This Post:
# 3  
RedHat

Thanks neutronscott for the solution & explanation Smilie It worked.

As read exists 2 times it displays alternate lines & remaining lines were inputted as "press any keys". It's clear now Smilie

On a different note, read -s -N 1 is NOT working on bash 3.2 (comes with RHEL 5) but it's working fine on bash 4.2 (RHEL 6).

Thanks again!!
# 4  
Yeah, bash 3.2 didn't have -N only -n. It's almost the same. Try it and see if it has the correct effect.
# 5  
Quote:
Originally Posted by neutronscott
Yeah, bash 3.2 didn't have -N only -n. It's almost the same. Try it and see if it has the correct effect.
Yep, I tried -n after posting here and it worked.

Thanks again for all the help & guidance!

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #78
Difficulty: Easy
Linux was inspired by MINIX
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading line by line from live log file using while loop and considering only those lines start from

Hi, I want to read a live log file line by line and considering those line which start from time stamp; Below code I am using, which read line but throws an exception when comparing line that does not contain error code tail -F /logs/COMMON-ERROR.log | while read myline; do... (2 Replies)
Discussion started by: ketanraut
2 Replies

2. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

3. Shell Programming and Scripting

While loop is not reading next line in the file when IF condition is used.

Hi Guys I am new to scripting.Please forgive for asking basic questions. I want to write a script to check whether the logs are getting updated in last 15 mins. cat server 192.168.1.6 192.168.1.7 cat list 192.168.1.7 /logs/logpath1 192.168.1.7 /logs/logpath2 192.168.1.6... (4 Replies)
Discussion started by: vdurai
4 Replies

4. 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

5. Shell Programming and Scripting

reading line by line from a text file

Hi, I have a text file something like this: 10.10.10.1, ldap, cn=users,dc=example,dc=com ..... ... and many more lines ... ... now i want to read each individual line from the file and assign it to a variable example: the script should read 10.10.10.1 and assign it to a variable say... (3 Replies)
Discussion started by: sunrexstar
3 Replies

6. 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

7. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

8. Shell Programming and Scripting

Reading data from a specific line in a text file

Hello, I have a problem which is giving me headache for days, can some please help. Please see code and text fiel below. Please see text in red for the problem I am facing # Program gets an input x from user while read line ; do echo... (4 Replies)
Discussion started by: jermaine4ever
4 Replies

9. Shell Programming and Scripting

Reading data from a specific line in a text file

hello, I have got the following problem that I am hoping someone can help with please. 1. I have got the following text file (below) , the columns data are 'Test Day', 'Board', 'Betting Number'. TEXT FILE ============================================ 1 3 02-01-27-28-29-30 0 1... (1 Reply)
Discussion started by: jermaine4ever
1 Replies

10. Shell Programming and Scripting

reading text file line by line

Ok. before anyone mentions it, I did search for this but I'm not sure if I am looking for the right thing. Now, onto my issue. I have been keeping vmstats output in running text files. So I have a file that looks like this: vmstat 2 5 2005.09.19 kthr memory page ... (6 Replies)
Discussion started by: MizzGail
6 Replies

Featured Tech Videos