Why doesn't unix read the last line while reading?

My script

export IFS=":" cat hello | while read abcd; do echo $a,$b,$c,$d done 

My hello file

 1:1:1:1 2:2:2:2 3:3:3:3 

My conclusion

 1,1,1,1 2,2,2,2 

If I put an empty string after 3:3:3:3 in hello , then the output will be

 1,1,1,1 2,2,2,2 3,3,3,3 

Does anyone know how I can fix this problem, so I don't need to put an empty line at the end of hello ?

+7
linux unix bash shell
source share
5 answers

What happens is the read command fails when the input does not end with a new line. Since there is no newline at the end of your file, read fails, so the last iteration of the while skipped.

If you do not want / cannot make sure that your input file has a new line at the end, you can group your cat with echo to create the appearance of an input terminated by a new line, for example:

 { cat hello; echo; } | while read abcd; do echo $a,$b,$c,$d done 

or like this:

 (cat hello; echo) | while read abcd; do echo $a,$b,$c,$d done 
+11
source share

There is a not-so-known, nasty hack to add a missing final new line to a text stream if and only if it is missing:

 sed '$a\' 

See this discussion on unix.stackexchange, for example.

+4
source share

This is an extensive reading for the question, why?

If you do not want to insert a new line at the end, you can do this:

 while IFS=: read -rabcd || [ -n "$a" ]; do echo $a,$b,$c,$d; done < file 

Or using grep :

 while IFS=: read -rabcd; do echo $a,$b,$c,$d; done < <(grep "" filee) 

Note:

+3
source share

This is most likely due to the absence of a newline after the last line in your input file.

Run this command to find out:

 cat -vte hello 

And look what happened.

+1
source share

The last line is probably missing a newline ( \n ). You can prove this by printing out the remaining values โ€‹โ€‹of $a , etc., for example,

 while IFS=: read abcd; do echo $a,$b,$c,$d; done < hello echo $a,$b,$c,$d 

Correct the file. In addition, you must install IFS specifically for read , as shown above. And there is no need for a cat file, just use input redirection, as shown in the figure.

+1
source share

All Articles