using grep or sed to find a tab


Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: using grep or sed to find a tab

  1. #1
    Join Date
    Apr 2002
    Posts
    120

    using grep or sed to find a tab

    Does anyone know how to represent a tab when grepping? I know this is more of a scripting/programming question but no one seems to know on that forum and this one seems to get more traffic
    Here are some things ive tried that didnt work:
    grep '<tab>' file
    grep '\t' file
    grep '/t' file
    grep '(actually hitting tab)' file
    grep '[[:space:]]' file
    grep '[[:tab:]]' file
    etc etc.
    I cant seem to find anything in the man pages at all (im running Redhat 7.1 if you are wondering) and the reason i need to know this is for more complicated reasons than the example listed (hehe, hopefully obviously). Does anyone have any idea how to represent a tab? 5 points to the first person who gets it! hehe, thanks!

    --Morphman

  2. #2
    Join Date
    Feb 2000
    Posts
    252
    still playing...

    grep doesn't use c-style translations (I read that somewhere)

    cat ./filename | tr '\t' 'char' | grep 'char'

    seems to do something similar
    cat /dev/null > ~/.sig

  3. #3
    Join Date
    Apr 2002
    Posts
    120
    Hmm, so you are saying to create a file with just a tab and pipe that in where the tab should be? Remember, im still a junior grasshopper Thanks for the help so far!

  4. #4
    Join Date
    Aug 1999
    Location
    Juneau, AK USA
    Posts
    778
    I think he meant...
    ./filename to be the file you were running grep against..
    Hope that's a bit of help.

    I'm off to read "man tr"
    We'll get thisright yet!

  5. #5
    Join Date
    Apr 2000
    Location
    Linköping, Sweden
    Posts
    140
    You could probably use awk

    awk '/regular_expression/'

    is basicly the same as

    (e)grep regular_expression

    but it will handle the \t correctly. Using

    awk '/\t/' file

    will read data from the file file and only print the lines having at least one tab in them.

  6. #6
    Join Date
    Apr 2002
    Posts
    120
    Ok, i tried using "awk '/\t/' filename" and it did print every line with a tab. But what if i want to substitute every tab in a file for a space and then word, for example:
    sed 's/\t/ Word/g' filename
    See what i mean? Is there a way to pipe the awk output into sed so i can make the substitution without having to find a tab?
    Also, the \t did work for a tab in awk, but not sed or grep. And that is where i need it most. Thanks for the help so far! I work at IBM and im trying to do some research on linux/unix and im stuck on this little point. Ive asked several linux people what to do and no one seems to know. Help!

  7. #7
    Join Date
    Apr 2000
    Location
    Linköping, Sweden
    Posts
    140
    AFAIK, sed and grep can't use the \t for some stupid reason. grep and sed will find tabs if [[:space:]] is used but that matches space, newline, tab etc.. so it is probably not usefull in this case.

    There are some ugly tricks you could use to get it working though. Note that I use bash so if you are using another shell this might not work. grep and sed can match a tab char if you actually hit the tab tangent in your script. I.e. type something like
    Code:
    grep "<hit tab key>" filename
    sed 's/<hit tab key>/ TAB /g' filename
    where the <hit tab key> is the result of pressing tab. The sed command will replace every \t with " TAB ".

    Using something like
    Code:
    tab=$(echo -e "\t")
    grep "$tab" filename
    sed "s/$tab/ TAB /g" filename
    or
    Code:
    grep "$(echo -e "\t")" filename
    sed "/$(echo -e '\t')/ s// TAB /g" filename
    also works for me.

    <edit>Note that I used the double quote (") around the sed rule. If you use the single quote (') $tab will not be expanded to the tab char (at least not in bash) and sed will search for the string '$tab'</edit>

    You could ofcourse also chose to use (g)awk to simulate your sed script. This is what I came up with after reading the info pages for (g)awk, type info gawk to read them. (They are confusing , but it looks like you can do a lot of cool stuff using (g)awk.)
    Code:
    awk '{ gsub (/\t/, " TAB "); print $0 }' filename
    awk '/\t/ { gsub (/\t/, " TAB "); print $0 }' filename
    The last one is the equivalent to
    Code:
    grep "$tab" filename | sed "s/$tab/ TAB /g"
    if $tab is defined as before.

    Hope this helps.

    <edit>Corrected some errors</edit>

    [ 19 April 2002: Message edited by: marvin ]

  8. #8
    Join Date
    Apr 2002
    Posts
    120
    *phew* thats juicy! Its going to take me some time to work through that one. Hmpf, i guess im going to have to settle for the theory that there is no way to represent a tab normally with grep or sed? That sucks. hehe, thanks for all your help though! It looks like you went through some real trouble to get to that one!

    --Morphman, greatful and dissappointed linux newbie

  9. #9
    Join Date
    Apr 2000
    Location
    Linköping, Sweden
    Posts
    140
    Glad I could help. It took a little while to figure the awk part out. Worst of all, now I really want to learn how to use awk properly and that is probably going to take a some time

    Good luck with your scripting.

  10. #10
    Join Date
    Apr 2002
    Posts
    120
    Well, i just figured out an easier way around it. Im not sure why i didnt think of this earlier! It seems like the easiest way to find a tab using grep is to actually create a small temporary file, just put a tab in it, and then use:
    fgrep -f temp filename
    and it will just get the pattern from the temporary file--in this case a tab. Woohoo! Thats not so bad after all!! Thanks for inspiring creativity folks, hehe.

    --Morphman, over and out

  11. #11
    Join Date
    Apr 2002
    Posts
    120
    Then again, another problem arises from this... How do I include regular expressions in a file that im using as a patter?
    Ex:
    If i create a file named "tab" and put a tab in it that will work fine if i only want to find a tab in a file. I could just use:
    fgrep -f tab file
    and all lines with a tab in them will be printed to the terminal. But what if i want to find all lines containing a tab followed by a 3, 5, or 7? I tried just adding this to the file named "tab":
    (hit tab here)[357]
    then i tried this:
    fgrep -f tab filename
    but i get nothing because anything within the file is considered a pattern and apparently i cant use regular expressions. Once again, i read all the man pages and i didnt see any way to tack on a regular expression to a file that is being used as a pattern. I know this is getting pretty complicated but the only way i think i can ever expect to find out this information is posting on a forum and hope some linux guru can spare a couple minutes to explain. hehehe, thanks again humbly...
    --Morphman, terribly curious linux newbie

  12. #12
    Join Date
    Apr 2000
    Location
    Linköping, Sweden
    Posts
    140
    fgrep is an alias for grep -F and greps for fixed strings. The [357] is interpreted as the exact string [357] and not as any of 3, 5 or 7. Use the normal grep instead and it should work.

  13. #13
    Join Date
    Jul 2007
    Posts
    3
    You can try to type $ grep '^[(space)(tab)+].*[(space)(tab)+]$' filename
    That will take care of any line that has one OR more space (AND/OR tab) at the beginning AND has one OR more space (AND/OR tab) at the end.

    (space) = actually hitting the space key
    (tab) = actually hitting the tab key

    I am new at this, but it worked for me. Hope it'll work for you 2

  14. #14
    Join Date
    Sep 2003
    Location
    Rochester, MN
    Posts
    3,607
    *looks at the date on the other posts in the thread*
    *looks at his join date*

    Damn, this thread is older than I am.

  15. #15
    Join Date
    Jul 2007
    Posts
    3

    Talking

    Lol for the next generation who are going to search for the truth.
    Last edited by UnixAdm; 07-06-2007 at 07:47 PM.

Posting Permissions

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