0

As part of learning/experimenting with bash I wrote a script with which you can install android-tools-adb package. apt output is written to log file. Before and after I run apt install, I check number of lines in log file, so I can check lines later for errors:

OUT=~/script_output-$(date +"%Y_%m_%d_%H_%M_%S").log
echo "" > "$OUT"
ln_before=$(wc --lines < "$OUT")
(sudo apt install android-tools-adb --yes) &>> "$OUT"
ln_after=$(wc --lines < "$OUT")
#$ln_after=66

after apt is finished, I can see in log file 87 lines. However, wc --lines returns 66. I understand it's because of lines starting with 'Reading dataase ...' which are updated on the same line in terminal. Gedit recognizes correctly that it has 87 lines. How can I get correct number of lines from log file ? Note this is not full script, just part of one function.

mauek unak
  • 702
  • 2
  • 11
  • 28
  • first attempt to return 87 and second attempt return 66? – Adiii Aug 02 '19 at 15:18
  • You can use `cat -A` to identify the characters; to make sense of the `^-` notation it uses, either substract 64 from the code of the character that follows the `^` or shift two columns to the left in an ascii table (for instance `^K` => 75 - 64 = 11 = VT, Vertical Tab). I'm struggling to find how to count them though, I thought `grep -c` would be appropriate but it counts matching lines, not individual matches. – Aaron Aug 02 '19 at 16:10
  • [This question](https://stackoverflow.com/questions/16679369/count-occurrences-of-a-char-in-a-string-using-bash) most likely has the solution to that problem. Looks like the `awk` accepted solution doesn't do the trick (but could be improved to print the sum of the counts rather than the count per line), and the `tr` + `wc -c` should work just fine – Aaron Aug 02 '19 at 16:15
  • `tr '\r' '\n' < "$OUT" > "$OUT.new"; mv "$OUT.new" "$OUT"` before `ln_after=$(wc --lines < "$OUT")` – Dennis Williamson Aug 02 '19 at 21:50

0 Answers0