Shell – cleaning output of a script so it’s descending, gives package names and cleanly exists

bts-searchbugsdebianshell-scriptzsh

I filed Is there a way to find patches which need testing from packages you have? and I was given a script which works –

for source in $(dpkg-query --show -f '${source:Package}\n' | sort -u); do bts select source:${source} tag:patch; done

now while the command works – the output is confusing, these few lines are the output –

[$] for source in $(dpkg-query --show -f '${source:Package}\n' | sort -u); do bts select source:${source} tag:patch; done          

781691
725728
805989
677570
772688
823072

Now it would be nice if there was a way to –

a. Gives the listing in descending manner so newer/exciting patches can be known/understood/tried out first than earlier ones.

b. Should list the package it belongs to –

for example 823072 belongs to acpi-support which has a patch or 805989 which is accountsservice . Having package names would be lot nicer.

c. Lastly, each time after the listing is complete (or is it? ) It should exit to command-prompt. If for some reason, networking connection down or not finding any bugs, it should bail out with a statement stating whatever the reason might be.

Is it possible ? I am on zsh and use oh-my-zsh so everything is operated from ~/.oh-my-zsh

Update – I have done something similar for listing local files but do not know how the above can be achieved –

┌─[shirish@debian] - [~] - [6266]
└─[$] alias ll

ll='ls -lt --color=auto --time-style=long-iso'

Update 2 – I tried just sort for now, have no idea about sed

I saw http://www.thegeekstuff.com/2013/04/sort-files/ as well as

http://www.tecmint.com/sort-command-linux/

and specifically saw –

The following sort command sorts lines in test file in reverse order
and removes duplicate lines from sorted output.

$ sort -r -u test 5 4 2 1

I tried to use the same in the script shared but it gives a different output altogether –

┌─[shirish@debian] - [~] - [6273]
└─[$] for source in $(dpkg-query --show -f '${source:Package}\n' | sort -r -u); do bts select source:${source} tag:patch; done       
538069
831900
779566
622933
691638
776663
419233

AFAI understand it, sort has been used to make sure that bug numbers do not repeat and are unique (means the listing is only once), don't really know why reverse is not working when it should work as the example shared. Maybe something to do with zsh or I am not writing it correctly 🙁 .

Looking up sed in the meanwhile to see if I can understand something.

Update 3 – this doesn't seem to work in zsh for some reason, just using the sed bits atm –

┌─[shirish@debian] - [~] - [6312]
└─[$] cat find-interesting-patches.sh

for source in $(dpkg-query --show -f '${source:Package}\n' | sort -u) do bts select source:${source} tag:patch | sed "s/^/${source} /g" done

and –

┌─[shirish@debian] - [~] - [6313]
└─[$] ll -h find-interesting-patches.sh

-rwxr-xr-x 1 shirish shirish 141 2016-11-23 23:56 find-interesting-patches.sh

so execute permissions are there

Trying to run it I get –

┌─[shirish@debian] - [~] - [6315]
└─[$] ./find-interesting-patches.sh

./find-interesting-patches.sh: 1: ./find-interesting-patches.sh: Syntax error: "|" unexpected

any ideas what could be wrong here ?

Best Answer

Starting with

for source in $(dpkg-query --show -f '${source:Package}\n' | sort -u); do bts select source:${source} tag:patch; done

sorting the output involves another use of sort, to sort the overall output numerically in reverse order:

for source in $(dpkg-query --show -f '${source:Package}\n' | sort -u); do bts select source:${source} tag:patch; done \
| sort -n -r

To display the (source) package name along with the bug number, the simplest option is to add ${source} (the source package) in the right place, processing bts's output each time it's run:

for source in $(dpkg-query --show -f '${source:Package}\n' | sort -u)
do bts select source:${source} tag:patch | sed "s/^/${source} /g"
done

This outputs lines of the form

linux 845422

so we need to change the final sort to sort on the second field:

for source in $(dpkg-query --show -f '${source:Package}\n' | sort -u)
do bts select source:${source} tag:patch | sed "s/^/${source} /g"
done | sort -k2,2n -r

On a single line that's

for source in $(dpkg-query --show -f '${source:Package}\n' | sort -u); do bts select source:${source} tag:patch | sed "s/^/${source} /g"; done | sort -k2,2n -r
Related Question