How to automatically count number of lines printed by each command?
Examples:
$ echo xxx
xxx
1
$ ls -1
xxx
yyy
zzz
3
$ > t0.txt
0
etc.
I.e. how to correctly add | wc -l into .bashrc?
Combination of many commands, using Bash:
ls -1 \
| cat <(echo) - \
| cat -n \
| tac \
| cat -n \
| while read i n line; do
if [[ $i == 1 ]]; then
echo $((n - 1))
fi
echo $line
done \
| tac \
| tail -n +2
Output:
xxx
yyy
zzz
3
cat <(echo) - adds an empty line to the head because if stdin is zero lines, the code block above will not work correctlycat command with n option adds line number to headtac command reverses stdinwhile loop, print reversed first line number and stdintac again and tail -n +2 to print the expected outputls -1 with echo xxx, > t0.txt, etc.
| wc -l. So I am thinking to automate it.| wc -lto everything because you want to see the output too. I'm responding to your "how do I do this automatically for everything". I don't think it's possiblelswithls | cat) and also know to step aside gracefully if you fired up a full screen editor such asvi