help with grep (or alternative)

Hi,

I am trying to produce a script which at some point needs to grep a command’s output in order to retrieve some value.

Example:


my-key   : my-data
other-key: other-data

Where I am interested in getting the value “my-data”.

So far I’ve been able to deal with that kind of input, but I recently discovered that input can come differently:


my-key   : my data1
           my-data2
           my-data3
other-key: other-data

Where I am interested in getting “my-data1” “my-data2” and “my-data3”.

Is there a way of telling grep to get those values for me? If not, what about awk or even sed?

And just in case, this is NOT homework. In fact, I finished University 15 years ago (I am currently 37 years old).

What I am trying to produce is a simple script that finds all packages that (optionally/mandatory) require a given package by parsing pacman’s (pacman -Qi) output.

Thank you in advance.

Put another stage in the filter chain that joins up those continued lines into one long line.

What you’re trying to do might already be supplied by ‘rpm --provides’ commands. Look at the man page for rpm and see if it helps.

query-options
        --changelog] -c,--configfiles] -d,--docfiles] --dump]
        --filesbypkg] -i,--info] --last] -l,--list]
        --provides] --qf,--queryformat QUERYFMT]
        -R,--requires] --scripts] -s,--state]
        --triggers,--triggerscripts]

Why invent the wheel again?

The problem is this is not for openSUSE (neither any other rpm-based distro) but for Arch, which uses pacman. Thanks anyways.

Can you provide one example? I imagine I need some sed magic (and I suck at sed).

Hi Tio I’ve just tried this not sure what you’re after but this caused a few headaches. Far from an awk guru or sed but this seems to work. But you still have some rubbish in opt deps.

pacman -Qi | awk 'BEGIN{RS="Optional Deps";FS="Required By"}{print $1}'

What this does from my crude understanding RS = set the record selector to “Optional Deps” and set the FS = field marker to “Required By” and print field 1 .

Thank you FM, I’ll try it as soon as I get home.

There still is a few niggles to iron out i.e the first record for me is wrong it includes too much and ends with depends on so will need trimming(Maybe you can remove those lines, something like line doesn’t begin with alpha or line begins with whitespace in regexp). Then the other problem is you’ll still end up with things like.

  : gamin: a fam replacement to make thunar monitor changes to
                 files on the fly
                 xfce4-panel: for trash applet 

Not to mention I presume eventually you’ll want to collect the app name to. But certainly think awk maybe the better tool for what you wish for.

Thanks FM.

I finally made it work, Here’s the result, if you are interested:


#!/bin/sh

if  -z "$1" ]; then
	echo "Usage: `basename "$0"` packagename"
	exit 1
fi

for i in `pacman -Q | awk '{print $1}'`; do
	for j in `pacman -Qi $i | awk 'NF>=4 && $1=="Optional" {prefix=$4}; NF>=1 && $1!="Optional" && $1!="Required" && $1!="None" {prefix=prefix" "$1}; NF>=4 && $1=="Required" {print prefix}'`; do
		 "$j" = "$1" || "$j" = "$1:" ]] && echo $i
	done
done

Those bash purists will have a fit… not really a criticism but I suspect the awk purists will hang you for looping in a loop.

Now for example with very little testing, now that I think I can see what you wish to achieve why not, something similar to…

pacman -Qi | awk 'BEGIN{RS="Req";ORS="
"}/Optional.*gamin/' | awk 'BEGIN{RS="

";ORS="
";FS="
"}/gamin. /{print $1}'

Just pure awk apologies for the superfluous use of pipes one day I’ll actually work out that bit. It runs a lot faster I’ve done no troubleshooting beyond getting it to this basic bit. I can’t remember why the “Optional.*SEARCHTERM” I know what it does(Should be search and with) and I know removing it causes unexpected results, and doesn’t just return Opt - search term.

Any way perhaps it’ll help.