0

last row of file is getting truncated

This is my code:

#!/bin/bash
filetouse=$1
while read -r STUDENTID FIRST LAST C1 C2 C3 
do
    totalSum=$(expr $C1 + $C2 + $C3)
    average=$(expr $totalSum / 3)
    printf '%d [%d] %s, %s\n' "$average" "$STUDENTID" "$LAST" "$FIRST"
done < "$filetouse" | sort -k 3,3n -k 4,4n -k 2,2g

expected output =

71 [299226663] Camp, Laney
80 [434401929] Camp, Skyler
81 [199144454] Camp, Tracey
93 [928441032] Forester, Chris
82 [928441032] Forester, Jess
92 [888111818] Forney, JC
82 [123456789] Johnson, Lee
99 [290010111] Lee, Terry
91 [999999999] Smith, Jaime 

actual output =

71 [299226663] Camp, Laney
80 [434401929] Camp, Skyler
81 [199144454] Camp, Tracey
82 [928441032] Forester, Jess
92 [888111818] Forney, JC
82 [123456789] Johnson, Lee
99 [290010111] Lee, Terry
91 [999999999] Smith, Jaime 

I noticed from the code that

82 [928441032] Forester, Jess

is missing. It also happens that its the last row of the file. But I'm not sure why the last line is truncated.

File I'm using = testing.sh

123456789 Lee Johnson 72 85 90
999999999 Jaime Smith 90 92 91
888111818 JC Forney 100 81 97
290010111 Terry Lee 100 99 100
199144454 Tracey Camp 77 84 84
299226663 Laney Camp 70 74 71
434401929 Skyler Camp 78 81 82
928441032 Jess Forester 85 80 82
928441032 Chris Forester 97 94 89
ellsusan
  • 91
  • 1
  • 1
  • 6
  • Check if your input file has `DOS CRLF` endings and remove them, it might mess up the bash loop – Inian Sep 09 '16 at 06:11
  • 2
    You're probably missing the line ending (newline) after the last line. It's Chris Forester, not Jess Forester, who's missing. See also [Shell script read missing last line](http://stackoverflow.com/questions/12916352/shell-script-read-missing-last-line/). I think this is probably a duplicate of that; if the last newline is missing, it will be confirmed. – Jonathan Leffler Sep 09 '16 at 06:16
  • please check your $IFS environment variable. – gzh Sep 09 '16 at 06:30
  • `while read -r STUDENTID FIRST LAST C1 C2 C3 || test -n "$C3"` will allow you to read the last line in the absence of a POSIX line-ending. (e.g. the `'\n'`) – David C. Rankin Sep 09 '16 at 07:43
  • @DavidC.Rankin thanks, that worked! – ellsusan Sep 09 '16 at 18:22
  • Funny how them old-timers have a trick or two left up their sleeves `;p` – David C. Rankin Sep 10 '16 at 06:03

2 Answers2

0

I run your script with no modification, got this:

#./script.sh input.txt 
71 [299226663] Camp, Laney
80 [434401929] Camp, Skyler
81 [199144454] Camp, Tracey
82 [123456789] Johnson, Lee
82 [928441032] Forester, Jess
91 [999999999] Smith, Jaime
92 [888111818] Forney, JC
93 [928441032] Forester, Chris
99 [290010111] Lee, Terry

It does show the one with 93.

elha2en
  • 192
  • 8
0

I tried your input file and script, I got correct result.

I also tried with changing input file format: 1) "DOS CRLF" will make script show error message. 2) a new line at end also make script show error message.

you'd better use -x option to show what happened when run script on your PC.

Plese run "echo $IFS" to show whether bash process newline correctly.

gzh
  • 3,507
  • 2
  • 19
  • 23