如何自动计数每个命令打印的行数?
示例:
$ echo xxx
xxx
1
$ ls -1
xxx
yyy
zzz
3
$ > t0.txt
0
etc.即如何正确地将| wc -l添加到.bashrc中?
发布于 2022-02-16 03:19:25
使用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输出:
xxx
yyy
zzz
3cat <(echo) -在头部添加了空行,因为如果stdin为零行,上面的代码块将不能正常工作。cat选项的n命令将行号添加到头部tac命令反转stdinwhile循环中,打印反向第一行号和stdintac和tail -n +2打印预期的输出echo xxx、> t0.txt等代替D22。https://unix.stackexchange.com/questions/690804
复制相似问题