Incidentally, you know why it’s called “grep” ?


$ re=root # re = "regular expression"
$ echo g/$re/p | ed - /etc/passwd # spot the command name
root:*:0:0:System Administrator:/var/root:/bin/sh
...

…in case you’re ever hacked remotely logged into a system where grep has been removed but ed remains; wrap it in a shellscript and you’re all set.

Similarly more can be lashed-up using a while-loop in Bourne shell, with a file redirect.

Comments

4 responses to “Incidentally, you know why it’s called “grep” ?”

  1. Shawn Ferry

    I had never thought to pipe it to ed instead of just using ed or vi the same way or with a heredoc

    The pipe makes it much easier to change files vs
    $re=foo
    ed -s /etc/passwd <<EOF
    g/$re/p
    q
    EOF

    1. There’s a bunch of ways to do that – before now I have written here-doc versions with full filename parsing and loops. The pipe is a cute one-liner, though.

  2. Chris Gerhard

    Ah memories of all the things you can do with the shell when you only have the statically linked shell on SunOS 4. No ed there though.

    1. But there were shell functions, which was necessary when you were – as part of the network security group, of course – busted into a machine with a readonly chroot environment and no utilities other than a shell:

      cat() {
      file=$1
      while read line
      do
      echo $line
      done <$file
      }

      ls() {
      echo *
      }

      …and so on. Used to have quite an advanced little kit in “.profile” format.

Leave a Reply

Your email address will not be published. Required fields are marked *