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.

In the Cisco IOS, you can filter and manipulate the output of commands using various built-in tools. Some of these have clear Linux equivalents. For example, 'exclude' and 'include' provide 'grep -v' and 'grep' behavior.

The "begin" command will output all input text after it matches a RegEx pattern. Is there a Linux equivalent for this type of behavior?

share|improve this question
add comment

migrated from serverfault.com Jan 24 '12 at 14:11

This question came from our site for professional system and network administrators.

4 Answers

up vote 5 down vote accepted

Sure, it's easily done using awk:

$ echo -e 'a\nb\nc\nd' | awk '{if (p) {print;}} /^b$/ { print; p=1;}'
b
c
d

$ echo -e 'a\nb\nc\nd' | awk '{if (p) {print;}} /^b$/ { p=1;}'
c
d

These can also be abbreviated as (thanks to @manatwork):

$ echo -e 'a\nb\nc\nd' | awk '/^b$/{p=1} p'
b
c
d

$ echo -e 'a\nb\nc\nd' | awk 'p; /^b$/{p=1}'
c
d

Rolling it into a convenient shell script is left as an exercise for the user.

share|improve this answer
    
Thanks to default action that can be written shorter as awk '/^b$/{p=1} p or awk 'p; /^b$/{p=1}, depending if you want to include the matching line or not. But thanks to pattern ranges awk '$0=="b",0' is enough. –  manatwork Jan 24 '12 at 14:25
    
The awk approach comes closest...guess i have to learn more about awk! I'd ultimately like to include it as a alias in my bashrc. –  Aquadisco Jan 24 '12 at 21:04
add comment

grep -A1000 search gives 999 lines of output after each matching line.

share|improve this answer
    
+1, but why not just bang on the '9' key (-A99999) a few times? –  Gerald Combs Jan 24 '12 at 0:34
add comment

Use less and then / to search:

petrus@hive:/usr/bin$ ls -l | less
total 535796
-rwxr-xr-x 1 root   root        30244 2011-02-23 14:22 [
lrwxrwxrwx 1 root   root            8 2011-05-26 01:22 2to3 -> 2to3-2.7
-rwxr-xr-x 1 root   root           96 2011-04-11 21:07 2to3-2.7
[...]
-rwxr-xr-x 1 root   root         9588 2011-02-24 09:43 appres
-rwxr-xr-x 1 root   root         6930 2011-05-20 23:53 apr-1-config
:

And now by typing /, the 'less' prompt (:) will change. If I search "lsusb":

-rwxr-xr-x 1 root   root         9588 2011-02-24 09:43 appres
-rwxr-xr-x 1 root   root         6930 2011-05-20 23:53 apr-1-config
/lsusb
lrwxrwxrwx 1 root   root           13 2011-05-26 01:24 lsusb -> ../sbin/lsusb
-rwxr-xr-x 1 root   root       114528 2010-11-24 18:16 ltrace
[...]
share|improve this answer
add comment

This is fairly easy in sed: print all lines from the first line matching the pattern to the last line ($).

sed -n '/pattern/,$p'

If you want to omit the line matching the pattern, delete everything up to and including it.

sed '1,/pattern/d'
share|improve this answer
add comment

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.