Sponsored Content
Top Forums Shell Programming and Scripting Problem with while loop reading every line of a text file Post 302949219 by reddyr on Wednesday 8th of July 2015 08:54:21 AM
Old 07-08-2015
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!!
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
getusershell(3C)					   Standard C Library Functions 					  getusershell(3C)

NAME
getusershell, setusershell, endusershell - get legal user shells SYNOPSIS
#include <unistd.h> char *getusershell(void); void setusershell(void); void endusershell(void); DESCRIPTION
The getusershell() function returns a pointer to a legal user shell as defined by the system manager in the file /etc/shells. If /etc/shells does not exist, the following locations of the standard system shells are used in its place: /bin/bash /bin/csh /bin/jsh /bin/ksh /bin/pfcsh /bin/pfksh /bin/pfsh /bin/sh /bin/tcsh /bin/zsh /sbin/jsh /sbin/pfsh /sbin/sh /usr/bin/bash /usr/bin/csh /usr/bin/jsh /usr/bin/ksh /usr/bin/pfcsh /usr/bin/pfksh /usr/bin/pfsh /usr/bin/sh /usr/bin/tcsh /usr/bin/zsh /usr/xpg4/bin/sh The getusershell() function opens the file /etc/shells, if it exists, and returns the next entry in the list of shells. The setusershell() function rewinds the file or the list. The endusershell() function closes the file, frees any memory used by getusershell() and setusershell(), and rewinds the file /etc/shells. RETURN VALUES
The getusershell() function returns a null pointer on EOF. BUGS
All information is contained in memory that may be freed with a call to endusershell(), so it must be copied if it is to be saved. SunOS 5.10 30 Aug 2004 getusershell(3C)
All times are GMT -4. The time now is 09:28 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy