26

I have a file that might contain a line like this.

A B //Seperated by a tab

I wanna return true to terminal if the line is found, false if the value isn't found.

when I do grep 'A' 'file.tsv', It returns to row (not true / false) but

grep 'A \t B' "File.tsv"

or

 grep 'A \\t B' "File.tsv"

or

grep 'A\tB' 

or

grep 'A<TAB>B' //pressing tab button

doesn't return anything.

How do I search tab seperated values with grep.

How do I return a boolean value with grep.

Ank
  • 6,040
  • 22
  • 67
  • 100
  • 2
    I would suggest `grep $'A\tB'`, an answer from [here](http://stackoverflow.com/questions/9954515/grep-find-lines-that-contains-t) – sleepsort Nov 03 '13 at 08:19

3 Answers3

31

Use a literal Tab character, not the \t escape. (You may need to press Ctrl+V first.) Also, grep is not Perl 6 (or Perl 5 with the /x modifier); spaces are significant and will be matched literally, so even if \t worked A \t B with the extra spaces around the \t would not unless the spaces were actually there in the original.

As for the return value, know that you get three different kinds of responses from a program: standard output, standard error, and exit code. The latter is 0 for success and non-0 for some error (for most programs that do matching, 1 means not found and 2 and up mean some kind of usage error). In traditional Unix you redirect the output from grep if you only want the exit code; with GNU grep you could use the -q option instead, but be aware that that is not portable. Both traditional and GNU grep allow -s to suppress standard error, but there are some differences in how the two handle it; most portable is grep PATTERN FILE >/dev/null 2>&1.

geekosaur
  • 59,309
  • 11
  • 123
  • 114
  • 1
    Also note that escapes like `\t` *may* work in recent GNU `grep` with the `-P` option to enable PCRE regexes. – geekosaur Apr 06 '12 at 01:45
  • Even the literal TAB character doesn't work. When I press the tab character, a minus sign like something appears.. but it doesn't search – Ank Apr 06 '12 at 01:48
  • 2
    "A minus sign like something" sounds wrong. In `bash`, `Tab` by itself beeps/flashes screen according to terminal settings; `Ctrl`+`V` followed by `Tab` inserts a tab character. Are you typing this directly at a terminal, or in some editor (and if the latter, which?). – geekosaur Apr 06 '12 at 01:53
  • I'm typing directly to the terminal. Using Mac OSX. btw if I have to embed this grep in a PHP script as a shell command, do I press CTRL+V and Tab in the PHP script too – Ank Apr 06 '12 at 01:56
  • I'm also on OS X (Lion), and using the standard Terminal. On the other hand, if you're embedding it in a script, you are presumably using an editor and how you enter a literal tab character depends on the editor (in `vi`/`vim` insert mode a tab will work by itself; depending on how greedy the mode is, in Emacs you may need `Ctrl`+`Q` to prevent the tab from reindenting the entire line; some GUI editors may not allow entry of tabs at all but instead use it as the usual GUI widget focus change). – geekosaur Apr 06 '12 at 02:00
21

Two methods: use the -P option:

grep -P 'A\tB' "File.tsv"

enter ctrl+v first and enter tab

grep 'A  B' "File.tsv"
yaronli
  • 699
  • 5
  • 10
  • I like the -P switch, turning on "proper" Perl-style RegExp and searching on the Tab character. Note that this could run slower, or not at all on some systems. If you 'suspect' there's a tab, but aren't sure, `grep -P "A\s+B"` should gobble up all the tabs AND whitespace in-between A and B, and is probably what you want. – Tom Auger Aug 08 '12 at 14:08
12

Here's a handy way to create a variable with a literal tab as its value:

TAB=`echo -e "\t"`

Then, you can use it as follows:

grep "A${TAB}B" File.tsv

This way, there's no literal tab required. Note that with this approach, you'll need to use double quotes (not single quotes) around the pattern string, otherwise the variable reference won't be replaced.

John Sichi
  • 166
  • 1
  • 4