Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to match for example:

The string e1000g0 from the lltconfig -a list , and then to display the two lines after the string e1000g0

so I get only the lines:

       Node   0 du1a      :   00:21:28:14:76:68
       Node   1 du1b      :   00:21:28:59:72:C4  permanent

please advice how the get only the two lines after?

full example from ( lltconfig -a list ):

 lltconfig -a list
 Link 0 (e1000g0):
       Node   0 du1a      :   00:21:28:14:76:68
       Node   1 du1b      :   00:21:28:59:72:C4  permanent


 Link 1 (e1000g1):
       Node   0 du1a      :   00:21:28:14:76:69
       Node   1 du1b      :   00:21:28:59:72:C5  permanent


 Link21 (e1000g2):
       Node   0 du1a      :   00:21:28:14:76:49
       Node   1 du1b      :   00:21:28:59:72:A5  permanent

I also try this ( but its work only for linux and not on solaris -:(

       lltconfig -a list | grep -A 4 "e1000g0"  | grep -v "e1000g0"
       grep: illegal option -- A
       Usage: grep -hblcnsviw pattern file . . .
share|improve this question
    
look for ggrep on Solaris. Possibly in /opt/sfw/bin, /usr/sfw/bin, or /usr/gnu/bin, depending on when or how it may have been installed. –  Tim Kennedy Oct 18 '12 at 16:44

3 Answers 3

up vote 2 down vote accepted

Try doing this :

var='Link 0'
lltconfig -a list |
    awk '/'"$var"'/{l=1;next} /(^$)/{l=0} l==1  {print}'

If you'd like something more general :

grep="pattern" # the string where we begin
max=4          # the number of lines after the matched pattern
awk '/'"$grep"'/{l=1;count=NR;next} l>0 && NR-count < '"$max"+1' {print}'

(tested on Solaris11)

share|improve this answer
1  
not good because its also bring the lines for e1000g1 ( I need only the lines for e1000g0 ) –  yael Oct 18 '12 at 14:43
    
how to export "Link 0" into awk , because some times its Link 1 ... etc ( I need this as parameter , not as static value ) –  yael Oct 18 '12 at 15:03
    
See my edited post. –  sputnick Oct 18 '12 at 15:10
    
great thx allot –  yael Oct 18 '12 at 15:15
    
Added a more generic awk command. –  sputnick Oct 18 '12 at 15:18

Solaris 11 has GNU egrep, which can provide contextual lines using -A or -B.

Or, if you don't have GNU grep/egrep, then the cgrep script at http://www.intuitive.com/wicked/showscript.cgi?036-cgrep.sh provides a contextual grep with a similar function.

share|improve this answer

lltconfig -a list | awk 'BEGIN{n=0}/e1000g0/{n=NR}n&&NR>n&&NR<n+3{print}'

share|improve this answer
    
lltconfig -a list | awk 'BEGIN{n=0}/e1000g0/{n=NR}n&&NR>n&&NR<n+3{print}' awk: syntax error near line 1 awk: bailing out near line 1 –  yael Oct 18 '12 at 15:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.