odd problem in read lines from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting odd problem in read lines from file
# 1  
Old 09-01-2011
odd problem in read lines from file

Hi,

I wrote a small program to read lines from a file and count the lines. The program is as below:

Code:
filename=$1
count=0    
cat $filename | while read -r line
do  
    printf "%5d:%s\n" $count "$line"
    count=$((count + 1))
done
echo " $count "

After I run the program, the result is below:
Code:
   0:This
    1:is
    2:test
    3:
    4:!
    5:
 0 

I don't understand why the count is still 0 after the read lines loop. Can someone give any advices? Thanks!

Last edited by Franklin52; 09-01-2011 at 01:24 PM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 09-01-2011
# 3  
Old 09-01-2011
Hi yazu,

Thanks. The information is very helpful.
# 4  
Old 09-01-2011
This is a useless use of cat. In this case the useless cat doesn't just waste CPU time, it also prevents variables from being modified because the things after the pipe execute in a subshell. Remove the useless cat and it should work:
Code:
filename=$1
count=0    
while read -r line
do  
    printf "%5d:%s\n" $count "$line"
    count=$((count + 1))
done < $filename
echo " $count "

# 5  
Old 09-01-2011
Hi Corona688:

Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find to delete lines with pattern and even or odd number

In the below directory I am trying to delete all lines with a .bam extention that have the pattern IonCode_ followed by an even number. I am also trying to delete all lines with a .fastq extention that have the pattern IonCode_ followed by an odd number. I was going to use find but can see all... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. UNIX for Dummies Questions & Answers

Replace character in odd or even lines

Hello, I'm here again asking for your precious help. I'm writing some code to convert csv files to html. I want to highlight header and also I want to have rows with alternate colors. So far this is my work###Let's format first line only with some color cat $fileIN".tmp1" | sed '1... (7 Replies)
Discussion started by: emare
7 Replies

3. Shell Programming and Scripting

Sum product of even/odd lines

Hi, I have a text file like this 6.0000E-02 0.00000E+00 0.0000 0.00000E+00 0.0000 7.0000E-02 5.00000E-10 1.0000 5.00000E-10 1.0000 8.0000E-02 3.00000E-09 0.4082 3.00000E-09 0.4082 9.0000E-02 3.50000E-09 0.3780 3.50000E-09 0.3780 1.0000E-01 1.00000E-09... (2 Replies)
Discussion started by: f_o_555
2 Replies

4. Shell Programming and Scripting

Insert at the beginning of odd lines

Hello people, I am trying with sed to insert some text at the beginning of each odd line of a file but no luck. Can you please help. Awk is also suitable but I am not very familiar with it. Thank you in advance for any help. (7 Replies)
Discussion started by: drbiloukos
7 Replies

5. Shell Programming and Scripting

How to search for pattern in odd lines?

Hi friends, I am looking for sed command/script that would search for a given fixed pattern on odd lines and then if it matches, prints the matching pattern and the next line. For example, in the example below, i am looking for pattern 0 and 1011 on odd lines. ########## start of example file... (10 Replies)
Discussion started by: kaaliakahn
10 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

print ODD lines

i want to print ODD lines like first ,third then fifth and so on 234,567,ABC,KJL 234,565,ABD,KJL 234,568,ABE,KJL 234,560,ABF,KJL 234,563,ABG,KJL 234,562,ABH,KJL O/P will be like 234,567,ABC,KJL ----->first liine 234,568,ABE,KJL ----->third line 234,563,ABG,KJL ----->fifth line... (6 Replies)
Discussion started by: aaysa123
6 Replies

8. UNIX for Dummies Questions & Answers

odd ftp problem

we migrated from a system with HPUX B.11.0 to HPUX 11.23 i64 the new server is the same IP as the old server. the old server is renumbered to something else to avoid conflict. on the old system, we had a user named "ftp" . There is one person who daily will ftp files to the host using this... (15 Replies)
Discussion started by: LisaS
15 Replies

9. Linux

Odd WLAN Device Problem

I'm trying to help convert my boss over to Linux. He has an HP/Compaq PC on which he installed Fedora Core 4 on. It's got a Linksys wireless card in it and we're using the NDIS wrapper to load the Windows driver for the chipset on that card. 'iwconfig' sees the card. We can configure the card. ... (1 Reply)
Discussion started by: deckard
1 Replies

10. Linux

odd telnet problem

Hey, I've got a RH9 box running telnet-server 0.17-25. Now i don't know what the problem is and i've been reading all night trying to find somthing like it. I am able to open a telnet session on the box using localhost and 10.10.10.6(machines address) but if i try to do it from another... (7 Replies)
Discussion started by: byblyk
7 Replies
Login or Register to Ask a Question