I would like to write a script with the following requirements:
- in input, get a list of packages available via
apt
. In there you may have some packages that are automatically installed by other packages in list. - in output, provide the same list without the packages that depends from other packages in list.
In other terms, I want to do in bash what the user Francois G did in this answer
Maybe something like this already exists, but sometimes I like to write scripts to improve my bash-scripting and also for fun.
In my mind, I already designed the script, but I have a technical issue. Let's assume that I have the dependency list in this format (it's the way apt-rdepends
put it):
useless-line-1
useless-line-2
useless-line-3
item-1
fixed-string substring-1-1
fixed-string substring-1-2
fixed-string substring-1-3
item-2
fixed-string substring-2-1
fixed-string substring-2-2
item-3
item-4
fixed-string substring-4-1
fixed-string substring-4-2
fixed-string substring-4-3
fixed-string substring-4-4
I want to extract the paragraph related to item-1
i.e.:
fixed-string substring-1-1
fixed-string substring-1-2
fixed-string substring-1-3
I'm not an awk
expert, but I think that it can suit my purposes. I'm not able to "build" the correct command. Due to the fact that item-2
may be not known, I tried:
# extract text between item-1 and the next line that starts without blank
$ awk '/item-1/,/^[A-Za-z0-9]/' deplist
item-1
but item-1
already fits the condition ^[A-Za-z0-9]
, so it's not good. Moreover, I want to exclude item-1
and item-2
from the output.
Which is the best way to extract that portion of data?
You could do something "stateful" ex.
How this works:
set
p=0
whenever we match a line that stars with anything other than horizontal whitespace (you could use your original slightly more specific^[A-Za-z0-9]
here)set
p=1
if we match the desired^item
print whenever
p==1
Essentially "turn printing on when we match the desired item and turn it off when we match any other item".
You'll need a bit of extra logic to skip the matched line:
Here, we perform the same match but save the result in variable
m
; then setp=1
whenm
is true (this part is identical to what we had before); we then only print when bothp==1
andm==0
i.e. skip the line where the actual match occurs.