如何在命令的输出中添加分隔线:
pacman -Ss linux
我明白了
community/riscv64-linux-gnu-glibc 2.29-1
GNU C Library RISCV target
community/riscv64-linux-gnu-linux-api-headers 5.0-1
Kernel headers sanitized for use in userspace (riscv64-linux-gnu)
community/rt-tests 1.3-1 (realtime)
A collection of latency testing tools for the linux(-rt) kernel
我想得到
community/riscv64-linux-gnu-glibc 2.29-1
GNU C Library RISCV target
--
community/riscv64-linux-gnu-linux-api-headers 5.0-1
Kernel headers sanitized for use in userspace (riscv64-linux-gnu)
--
community/rt-tests 1.3-1 (realtime)
A collection of latency testing tools for the linux(-rt) kernel
答案 0 :(得分:1)
我还没有使用过pacman,但是如果您想在每行不以制表符开头的行之前打印--
,此awk可以解决问题:
echo -e '1\n\t2\n3\n\t4\n\t5\n6\n\t7' | awk '{if (NR > 1 && $0 !~ "^\t") print "--"; print $0}'
结果:
1
2
--
3
4
5
--
6
7
说明:
if ((NR > 1) && ($0 !~ "^\t")) print "--"
:如果(行号大于1)并且(行不以制表符开头),则打印--
print $0
:打印整行
类似地,如果您想每两行打印--
,则应该这样做:
echo -e '1\n\t2\n3\n\t4\n\t5\n6\n\t7' | awk '{if (NR > 1 && NR % 2 == 1) print "--"; print $0}'
结果:
1
2
--
3
4
--
5
6
--
7