2

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?

4 Answers 4

5

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.

2
  • 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, 2012 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, 2012 at 21:04
4

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

1
  • +1, but why not just bang on the '9' key (-A99999) a few times? Jan 24, 2012 at 0:34
3

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
[...]
1

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'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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