[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: quick scripting question - finding occurrence in many lines



On Monday 06 November 2006 05:14, Andrew Sackville-West wrote:
> On Sun, Nov 05, 2006 at 10:08:12AM -0800, Steve Lamb wrote:
> > Andrew Sackville-West wrote:
> > > On Mon, Nov 06, 2006 at 01:10:08AM +1100, John O'Hagan wrote:
> > >> Or the whole thing could even be done with (I think!):
> > >>
> > >> #tr -d '\n' < IN | tr ' ' '\n' | grep -B1 Processor | grep -v
> > >> 'Processor\|--'

[...]

>
> so that part strips all the newlines. the next tr replaces all spaces
> with newlines so that each word is now on an individual line, then
> grep -B1 Processor returns the line before any occurence of
> Processor. not sure what the second grep does with that \--
> in it, but I assume it is supposed to eliminate any "Processor" result
> when the input includes 'Processor Processor'.
>

The first grep -B1 returns two lines per result, "Processor" and the number we 
are after; also, grep -B separates each result with a "--". The second 
grep -v is just to remove the unwanted lines. 

E.g., if IN contains:

junk info 18 Pro
cessor 
ggjgh 34 
Processor dgjhj 
19 Processor yytyr
fee 45 Processor tt 5
6 Proce
ssor rgrge

Then (after the newline business) grep -B1 Processor gives:

18
Processor
--
34
Processor
--
19
Processor
--
45
Processor
--
56
Processor

Grep -v 'Processor\|--' gives just the numbers we want.

Regards,

John



Reply to: