Reputation: 6270
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.
Upvotes: 26
Views: 40719
Reputation: 166
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.
Upvotes: 12
Reputation: 705
Two methods: use the -P option:
grep -P 'A\tB' "File.tsv"
enter ctrl+v first and enter tab
grep 'A B' "File.tsv"
Upvotes: 21
Reputation: 61449
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
.
Upvotes: 31