Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching tabs with grep

Tags:

grep

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.

like image 274
Ank Avatar asked Apr 06 '12 01:04

Ank


People also ask

How do I search for a tab in grep?

How do I grep tab (\t) in files on the Unix platform? just use grep "<Ctrl+V><TAB>" , it works (if first time: type grep " then press Ctrl+V key combo, then press TAB key, then type " and hit enter, voilà!)

How do you grep multiple items?

The basic grep syntax when searching multiple patterns in a file includes using the grep command followed by strings and the name of the file or its path. The patterns need to be enclosed using single quotes and separated by the pipe symbol. Use the backslash before pipe | for regular expressions.

How do I view tabs in a file?

To "find" a Tab, highlight a Tab, copy it (ctrl+C), then paste it into the "find" box: ctrl+v. (f) Use FORMAT= or MFORMS= to pick up every other column. This is tricky.

What is find grep?

The grep command can search for a string in groups of files. When it finds a pattern that matches in more than one file, it prints the name of the file, followed by a colon, then the line matching the pattern.


1 Answers

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.

like image 187
geekosaur Avatar answered Oct 05 '22 12:10

geekosaur