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.
Use a literal Tab character, not the
\tescape. (You may need to press Ctrl+V first.) Also,grepis not Perl 6 (or Perl 5 with the/xmodifier); spaces are significant and will be matched literally, so even if\tworkedA \t Bwith the extra spaces around the\twould 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
grepif you only want the exit code; with GNUgrepyou could use the-qoption instead, but be aware that that is not portable. Both traditional and GNUgrepallow-sto suppress standard error, but there are some differences in how the two handle it; most portable isgrep PATTERN FILE >/dev/null 2>&1.