Mostly Linux & Python syntax notes and hyperlinks.

Friday, March 5, 2010

Linux: grep for tab character

Two ways:
  1. grep quote control-v tab quote
  2. cat -T then pipe to grep '\^I'
First I created a file with tabs for testing.  I typed "Hi there" then the tab character, then return, then "this is a " then control-V, then the tab character then "real tab", and then the next line without tabs:
$ cat > foo
Hi there
this is a ^     real tab
isn't it.
$ cat foo
Hi there
this is a       real tab
isn't it.
  1. You can grep for the tab character directly (sort of) by typing quote, then control-v, then another quote:
    $ grep "   " foo
    Hi there
    this is a       real tab
  2. cat -T is an option to show the tabs in a file as "^I".
    It's tricky to grep for the "^I" though. grep \^I and grep "^I" don't work. You need grep '\^I'
    $ cat -T foo
    Hi there^I
    this is a ^Ireal tab
    isn't it.
    $ cat -T foo | grep "^I"
    $ cat -T foo | grep '\^I'
    Hi there^I
    this is a ^Ireal tab

1 comment:

  1. All the companies I've every worked have always made tabs illegal in code (including GOOG).

    In emacs and vi you can expand all tabs to spaces...then you use a python (or whatever) mode for your editor that has the virtual tabs set where you want (but they are implemented with spaces, always)

    ReplyDelete

Blog Archive