Read file using while loop not reading last line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read file using while loop not reading last line
# 1  
Old 06-16-2011
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.

Quote:
while read line
do

let line_count="line_count + 1"
echo "line count $line_count : $line"

done < abc.txt
# 2  
Old 06-16-2011
it should read

post your file here
# 3  
Old 06-16-2011
It's old bag/feature. "While read" doesn't work when the last line of a file doesn't end with newline char. It reads the line but returns false (non-zero) value.

You can use this:

Code:
DONE=false
until $DONE; do
    read s || DONE=true
    # you code
done < FILE

# 4  
Old 06-16-2011
Quote:
Originally Posted by yazu
It's old bag/feature. "While read" doesn't work when the last line of a file doesn't end with newline char. It reads the line but returns false (non-zero) value.

You can use this:

Code:
DONE=false
until $DONE; do
    read s || DONE=true
    # you code
done < FILE

i tried in solaris. It is working fine without new line character

Code:
bash-3.00$ while read line; do echo $line; done < /tmp/myfile
bc
de
fg
bash-3.00$ cat /tmp/myfile
bc
de
fg

# 5  
Old 06-16-2011
Without the last newline your prompt should be like so:
Code:
% echo -n 'one
tho' > myfile
% cat myfile
one
tho%

# 6  
Old 06-16-2011
yes.. yazu, you are correct Smilie

thanks for the info
# 7  
Old 06-16-2011
Code:
awk '{print NR" : "$0}' abc.txt

Dependin whether wanting to number empty line as well or not

Code:
cat -n abc.txt

Code:
cat -b abc.txt

...Ooops

Last edited by ctsgnb; 06-16-2011 at 05:44 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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: # 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... (4 Replies)
Discussion started by: reddyr
4 Replies

2. Shell Programming and Scripting

Do While Loop + Read From File + assign line to a variable

Hello, I am using below code for reading from a file and assigning the values to a variable , but it is loosing the value after the loop , please suggest to retain the value of the variable after the loop , while IFS=: read -r line do set $dsc=$line echo 'printing line variable ' $line... (1 Reply)
Discussion started by: ParthThakkar
1 Replies

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

4. Shell Programming and Scripting

Reading line in while loop

Hello Team, i have to read line by line in a while loop, and the input file has:. # The script will start cppunit test application to run unit tests. -LD_LIBRARY_PATH=$CPPUNIT_HOME/lib:\ +LD_LIBRARY_PATH=$VOBTAG/SS_UnitTest/lib:\ $VOBTAG/SS_BFD/BFDSCLI/build:\ ... (7 Replies)
Discussion started by: chandana hs
7 Replies

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

6. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 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. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

9. Shell Programming and Scripting

while loop not reading last line

hi all, i have a while loop which i am using to read lines into an array: k=0 exec 10<file while read LINE <&10; do ARRAY=$LINE ((k++)) done exec 10>&- echo ${ARRAY} for some reason when i display the array it is not showing the last row in the file. any help appreciated. (1 Reply)
Discussion started by: npatwardhan
1 Replies

10. Shell Programming and Scripting

Reading from a specific line in a loop

Hello All, Request you to let me know how to do the below urgently.. Requirement File A Contains: for i in file A DEV1 DEV5 STG1 STG5 File B Contains: for j in file B DEV1 DEV5 STG1 STG5 (3 Replies)
Discussion started by: kaushikraman
3 Replies
Login or Register to Ask a Question