0

I have a file output.txt containing a cURL progress output like

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0  490M    0     0    0  208k      0   248k  0:33:43 --:--:--  0:33:43  248k
  0  490M    0     0    0  256k      0   157k  0:53:11  0:00:01  0:53:10  157k
  0  490M    0     0    0  368k      0   140k  0:59:49  0:00:02  0:59:47  140k
  3  490M    0     0    3 19.5M      0   155k  0:53:50  0:02:08  0:51:42  153k
  3  490M    0     0    3 19.6M      0   154k  0:54:05  0:02:09  0:51:56  141k
  4  490M    0     0    4 19.7M      0   154k  0:54:06  0:02:10  0:51:56  120k

I would ONLY like to extract the last line. When I do:

LASTLINE=$(tail -n1 output.txt)

It is apparent that even tho

echo $LASTLINE

shows the last line, it contains everything as evidence by:

echo "${#LASTLINE}"

which shows 709, the count of the number of characters, but the last line should only have 79 characters. I understand this is something about buffers. What is going on?

oguz ismail
  • 1
  • 16
  • 47
  • 69
user321627
  • 2,350
  • 4
  • 20
  • 43
  • What is `${#LASTLINE}` and what do you expect it to be? – Shawn Jul 06 '20 at 03:38
  • (Also, don't use ALLCAPS for your own variables. See [Correct bash and shell script variable capitalization](https://stackoverflow.com/questions/673055/correct-bash-and-shell-script-variable-capitalization) for details) – Shawn Jul 06 '20 at 03:39
  • it should be `4 490M 0 0 4 19.7M 0 154k 0:54:06 0:02:10 0:51:56 120k ` – user321627 Jul 06 '20 at 03:39
  • `${#LASTLINE}` should just be a number. – Shawn Jul 06 '20 at 03:41
  • Unable to duplicate, btw. `foo=$(tail -n1 output.txt) && echo "${#foo}"` gives me 78. – Shawn Jul 06 '20 at 03:47
  • Perhaps there are some trailing or in beetween non printable characters in the file. Check it with `hexdump` or `od`. – M. Nejat Aydin Jul 06 '20 at 03:52
  • Thanks, that totally was the case, copy and pasting it into stackoverflow fixed it so it could not reproduce. thank you! – user321627 Jul 06 '20 at 03:54

1 Answers1

1

You have carriage returns in that file. Get rid of everything preceding and including the last one using sed:

LASTLINE=$(sed '$!d;s/.*\r//' output.txt)
oguz ismail
  • 1
  • 16
  • 47
  • 69