using grep or sed to find a tab - Page 2


Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20

Thread: using grep or sed to find a tab

  1. #16
    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,936
    OK, if we're chiming in for future people, then:

    Code:
    grep $'\t' file
    will work fine, too, and is way shorter than anything else I've seen so far. The shell (assuming you're using bash, anyway) will replace the $'\t' with a real tab character. But you do have to be careful if you want to insert the tab into the middle of a larger grep pattern: you may have to close any quotes you have open. And if you have nested quotes, it might not be possible.

    OTOH, if you're writing a script, then you can just hit the tab key -- that character isn't special inside a script, it only tries to complete filenames in an interactive shell. (Of course $'\t' will still work inside a script, too, but it's not necessary.)

  2. #17
    Join Date
    Jul 2007
    Posts
    3

    Thumbs up

    Thanx, it works! I've been using ksh and csh, and it seems like using grep with '\t' doesn't really work in those, but it worked perfectly when I changed to bash

  3. #18
    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,936
    Hmm; I thought that would work in ksh too. Let me check some manpages...

    It looks like it depends on which version of ksh you're using. The manpage here, for instance, under the "Quoting" section, says:

    Code:
    A single quoted string preceded by an unquoted $ is processed as an ANSI-C string except for the following:
    
    \0
        Causes the remainder of the string to be ignored. 
    \E
        Equivalent to the escape character (ascii 033), 
    \e
        Equivalent to the escape character (ascii 033), 
    \cx
        Expands to the character control-x. 
    \C[.name.]
        Expands to the collating element name.
    So it looks like that version of ksh acts like bash. But this pdksh manpage doesn't say anything about that, so I'd guess that this version of pdksh doesn't support it.

    Hmm; I thought that was more portable than it apparently is...

  4. #19
    Join Date
    Mar 2007
    Posts
    16
    Could you find the hex or octal code for the tab character and put that in? It has been a long time since I used grep or awk.

  5. #20
    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,936
    You could if grep or sed or awk has an escape sequence to let you do that. Otherwise you can use the same shell escape sequence (in bash and new versions of ksh only, based on the above) of $'\x9' or $'\011'.

    (Tab is 9 in decimal, 9 in hex, and 11 in octal.)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •