Printing a set of lines after a pattern match

I had to do some pattern matching last week, and needed a way to print the two lines that occurred after each line that matched a specific string. Since awk provides robust pattern matching, I came up with the following awk command line to grab the information I needed:

$ cat test
foo
1
2
3
foo
1
2
3
$ awk ‘BEGIN { i=0 } /foo/ { while (i < 2 ) { getline; print $0; i++ } i=0}' test
1
2
1
2

I digs me some awk!

Not grep!

While reviewing some shell scripts last week, I saw the infamous find | grep:

$ /usr/bin/find /foo -type f | egrep -v \*.inp

I am not real sure why more people don’t leverage the logic operations build into find:

$ /usr/bin/find /foo -type f -not -name \*.inp

This saves a fork() and exec(), and should be a bit faster. I am curious if folks use grep because it’s easier to read, or because they don’t know about the logic operations built into find. I shall need to investigate …

Formatting text for printing

I periodically need to format text data on my Linux desktop for printing, and have always gone about this in one of two ways. If I want to add margins and make the data conform to a specific page length, I use the pr utility. Here is an example that formats FILE with a #4 margin, a #72 width (the default), and a length of 60 lines:

$ pr -o 4 -l 60 FILE

If I have a text document that I want to format to 80-characters per line, I typically use fmt with the width option. The following example will force all lines to 80 characters, and will gracefully wrap lines if they exceed this limit:

$ fmt -80 FILE

If you have any other methods for formatting text, I would love to hear them.

Bash short cuts / hotkeys

I use bash as my primary shell, and have come to rely on the following bash short cuts:

alt-f   -- move forward one word
alt-b  -- move backwards one word
ctrl-a  -- takes you to the begining of the command you are currently typing.
ctrl-b  -- move backwards one character
ctrl-c  -- kills the current command or process.
ctrl-d  -- kills the shell.
ctrl-e  -- takes you to the end of the command you are currently typing in.
ctrl-f  -- move forward one character
ctrl-h  -- deletes one letter at a time from the command you are typing in.
ctrl-l  -- clear screen
ctrl-r  -- does a search in the previously given commands so that you don't have to repeat long command.
ctrl-u  -- clears the typing before the hotkey.
ctrl-z  -- puts the currently running process in background
esc-b  -- takes you back by one word while typing a command.
esc-p  -- like ctrl-r lets you search through the previously given commands.
esc-.  -- gives the last command you typed.

Viewing previous dates with GNU date

GNU date has some nifty options, and is a time keepers toolbox rolled up into an ELF executable. One really cool option in GNU date is the ability to print a date in the past using the the “–date” option, and the “days ago” format string:

$ date –date=”30 days ago”
Wed Aug 30 15:15:51 EDT 2006

$ date –date=”10 years ago”
Sun Sep 29 15:15:56 EDT 1996

Now if only more vendors would include GNU date with their operating system. :)