Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get lines either side of a grep 2

Status
Not open for further replies.

stackdump

Technical User
Sep 21, 2004
278
GB
I want to grep a file (the output of lmstat -a) and see the matching line plus the next few.

The first part is easy; lmstat -a | grep -i xyz

but how can I get the next few lines? or maybe the subsequent lines up until the occurrence of a different pattern?

In regex this would be something like

lmstat -a | grep -ie 'users of xyz.*users'

but grep wont do singleline mode.
 
I'm not familiar with lmstat output (can you provide a sample output file?), but perhaps you can use

grep -ip xyz

for this (paragraph containing xyz - case insensitive).

The default para separator is empty line, but you can specify another parasep. See grep man page for more info.


HTH,

p5wizard
 
You may try something like this:
lmstat -a | awk '/pattern1/,/pattern2/'

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks guys, but I'm still having problems...

This is a fragment of "lmstat -a".

Code:
Users of 44500:  (Total of 4 licenses issued;  Total of 1 license in use)

  "44500" v1.000, vendor: accslmd
  floating license

    geoff sol1 wsacdc.dom:0.0 (v4.400) (gemini/7200 790), start Thu 1/12 16:29

Users of 33301:  (Total of 6 licenses issued;  Total of 0 licenses in use)

Users of 88800:  (Total of 6 licenses issued;  Total of 2 licenses in use)

  "88800" v1.000, vendor: accslmd
  floating license

    jodie sol4 wsacwb.dom:0.0 (v4.400) (gemini/7200 685), start Tue 1/10 13:14
    geoff sol1 wsacdc.dom:0.0 (v4.400) (gemini/7200 566), start Thu 1/12 13:05

Users of 44700:  (Total of 6 licenses issued;  Total of 1 license in use)

  "44700" v1.000, vendor: accslmd
  floating license

    geoff sol1 wsacdc.dom:0.0 (v4.400) (gemini/7200 490), start Thu 1/12 16:19

Users of 140:  (Total of 6 licenses issued;  Total of 0 licenses in use)

Users of 141:  (Total of 4 licenses issued;  Total of 0 licenses in use)

Users of 142:  (Total of 6 licenses issued;  Total of 0 licenses in use)

Users of 142 means users of productName 142. If the "users" is >0 then underneath, it lists the users.

I want to get a list of products in use; productName User

So this would be;

44500 geoff
88800 jodie geoff
44700 geoff
 
A starting point:
lmstat -a | awk '
$1=="Users" && $11>0{
u=$3;n=$11;printf "%s",u
for(i=0;i<4;++i)getline
for(i=0;i<n;++i){getline;printf " %s",$1}
printf "\n"}
' > /path/to/output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 

Thanks,

that'll get me most of the way there.
 
Not exactly how you want the output, and awk is better suited for this - see PHV's solution, but grep -p and sed can produce this:

Code:
lmstat -a | grep -p"Users of " vendor | sed '/floating/d; /^$/d; s/^ *//; s/ .*$//; s/"//g'
44500
geoff
88800
jodie
geoff
44700
geoff

HTH,

p5wizard
 

Also a neat solution!

Thanks dudes.

Have a star.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top