jhoss
jhoss

Reputation: 487

Put a line separator in the output of a command

how can I put a line of separation in the output of the command:

pacman -Ss linux

i get this

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

I want to get

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

Upvotes: 0

Views: 149

Answers (1)

gmargari
gmargari

Reputation: 171

I have not used pacman, but if you want to print -- before each line that doesn't start with a tab, this awk could do the trick:

echo -e '1\n\t2\n3\n\t4\n\t5\n6\n\t7' | awk '{if (NR > 1 && $0 !~ "^\t") print "--"; print $0}'

Result:

1
    2
--
3
    4
    5
--
6
    7

Explanation:

if ((NR > 1) && ($0 !~ "^\t")) print "--": if (row number is greater than 1) and (line does not start with a tab) then print --

print $0: print the whole line


Similarly, if you want to print -- after every two lines, this should do:

echo -e '1\n\t2\n3\n\t4\n\t5\n6\n\t7' | awk '{if (NR > 1 && NR % 2 == 1) print "--"; print $0}'

Result:

1
    2
--
3
    4
--
    5
6
--
    7

Upvotes: 1

Related Questions