How to grep a line followed by a newline from output in terminal

70 Views Asked by At

I have the following output on my terminal

* These packages depend on app-portage/gentoolkit-0.6.5:

 * These packages depend on app-portage/getuto-1.11:
sys-apps/portage-3.0.63 (!build ? app-portage/getuto)

 * These packages depend on app-portage/portage-utils-0.96.1:
app-admin/perl-cleaner-2.31 (!pkgcore ? app-portage/portage-utils)

How do I only grep lines such as gentoolkit that have no lines following it?

What I have tried : some command | xargs grep -eF ".*\n\n"
But this returns File or Directory not found.

What I am trying to achieve?
In gentoo linux, eix-installed -a | xargs equery depends gives the dependency for all packages. I want to find the ones that have no dependencies listed.

2

There are 2 best solutions below

2
jhnc On BEST ANSWER

Although @oguz-ismail's answer would work with the sample output provided, it seems that actual equery output becomes garbled when it is fed into a pipe.

This convoluted commandline seems to work around the problems:

</dev/null script -B /dev/null -f -q -c '
    eix-installed -a | xargs equery depends
' |
tr -d '\r' |
grep -v '^---' |
awk '!/\n/' RS=
7
David C. Rankin On

I think you would need something a bit longer. Something that stores lines ending in ':' and then checks, in some way, that the line that follows is empty (with awk simply checking NF works). You also need a trigger to indicate no output is waiting. Setting the variable holding the saved line to empty works.

There are many ways to do this, but you can translate the above into:

awk '/:$/ {last=$0; next} NF==0 && length(last) { print last } { last="" } END { if (length(last)) print }'

Example Use/Output

Using a heredoc to feed data (you can just pipe to it as you are doing), and making sure you catch a final line of interest in the END rule, you can do:

$ awk '/:$/ {last=$0; next} NF==0 && length(last) { print last } { last="" } END { if (length(last)) print }' << eof
>  * These packages depend on app-portage/gentoolkit-0.6.5:
>
>  * These packages depend on app-portage/getuto-1.11:
> sys-apps/portage-3.0.63 (!build ? app-portage/getuto)
>
>  * These packages depend on app-portage/portage-utils-0.96.1:
> app-admin/perl-cleaner-2.31 (!pkgcore ? app-portage/portage-utils)
>
>  * These packages depend on app-portage/cockatoolkit-0.6.5:
> eof
 * These packages depend on app-portage/gentoolkit-0.6.5:
 * These packages depend on app-portage/cockatoolkit-0.6.5:

("cockatoolkit" was just made up for the example...)

Look things over and let me know if you have problems.