How to grep for tabs without using literal tabs and why does \t not work?


grep is using regular expressions as defined by POSIX. For whatever reasons POSIX have not defined \t as tab.

You have several alternatives:

tell grep to use the regular expressions as defined by perl (perl has \t as tab):

grep -P "\t" foo.txt

use printf to print a tab character for you:

grep "$(printf '\t')" foo.txt

or, as you have already mentioned, use the literal tab character:

grep "^V" foo.txt

that is: type grep ", then press ctrl+v, then press tab, then type " foo.txt. pressing ctrl+v in the shell causes the next key to be taken literally. that means the shell will insert a tab character instead of triggering some function bound to the tab key.

if you use bash you can use the ansi c quoting feature:

grep $'\t' foo.txt

See the wikipedia article about regular expressions for an overview of the defined character classes in POSIX and other...

0 0

Using the 'sed-as-grep' method, but replacing the tabs with a visible character of personal preference is my favourite method, as it clearly shows both which files contain the requested info, and also where it is placed within lines:

sed -n 's/\t/\*\*\*\*/g' file_name

If you wish to make use of line/file info, or other grep options, but also want to see the visible replacement for the tab character, you can achieve this by

grep -[options] -P '\t' file_name | sed 's/\t/\*\*\*\*/g'

As an example:

$ echo "A\tB\nfoo\tbar" > test $ grep -inH -P '\t' test | sed 's/\t/\*\*\*\*/g' test:1:A****B test:2:foo****bar

EDIT: Obviously the above is only useful for viewing file contents to locate tabs --- if the objective is to handle tabs as part of a larger scripting session, this doesn't serve any useful...

0 0

Check man 7 regex for the POSIX regular expressions you can use, while -P for PCRE is supported in lots of places its not available everywhere (and the manualpage says its buggy so I don't always trust it myself) the POSIX ones should work in more places.

You can try something like this:

grep "^[[:digit:]]\+[[:space:]]\+-"

^ matches the beginning of the line.
[[:digit:]]\+ matches one or more digits.
[[:space:]]\+ matches one or more whitespace characters.
- finally matches the negative value you were looking for.

Manual page for regex(7):

Optional solution:

You can use awk to do this too (and certainly other tools). This is an awk example that prints lines where the second column is less than 0 (negative):

awk '{if ($2 < 0)...
0 0

Some lines of a file do not seem to match \t in a regex. Would anyone have an idea why ?

Let's take the example file that you can download from

$ wget --2011-02-03 16:24:08-- Resolving Connecting to||:80... connected. HTTP request sent, awaiting response... 200 OK Length: 31204 (30K) [text/plain] Saving to: `countryInfo.txt' 100%[===================================================================================================================================================================================================>] 31,204 75.0K/s in 0.4s 2011-02-03 16:24:10 (75.0 KB/s) - `countryInfo.txt' saved [31204/31204] $ cat countryInfo.txt | grep -E 'AD.AND' AD AND 200 AN Andorra Andorra la Vella 468 84000 EU .ad EUR Euro ...
0 0

Mozilla Keyboard Shortcuts

7 Aug 2006 Command, Windows, Mac, Linux. Copy, Ctrl+C, Cmd+C, Ctrl+C Paste, Ctrl+V, Cmd+V, Ctrl+V. Cut, Ctrl+X, Cmd+X, Ctrl+X Move to Previous Frame (in web pages using frames), Shift+F6, Ctrl+Shift+Tab or Shift+F6 (if F6 is not programmed for another command), Shift+F6. New Navigator Window, Ctrl+N

Keyboard shortcuts for copy / paste into console · Issue #235

20 Apr 2016 I vote for Ubuntu terminal shortcuts Ctrl + Shift + C/X/V as a primary ones and maybe an alternative Ctrl/Shift + Insert . But that's only my preference because I use bash on Linux in most cases.

Vi Cheat Sheet -

r, Replace one character. R, Replace many characters. CTRL-v char, While inserting, ignores special meaning of char (e.g., for inserting characters like ESC and CTRL) until ESC is used. :r file, Reads file and inserts it after current line. :nr file, Reads file and...

0 0
Hey folks, another linux/unix newbie here trying to get some basics down. I cant seem to find out how to search for a "tab" using the grep command. Here are some things ive tried so far:
grep '' filename
grep '(actually hitting tab here)' filename
grep '/t' filename
grep '\t' filename
grep '[[:space:]]' filename
etc etc.
Does anyone know what the proper way to search for a tab is? The actual reason i need it is of course much more complicated than this example but just knowing how to find tab would be enough. Thanks a ton!

--Morphman, Linux Newbie

You may be able to just use the ASCII code for tab somehow. Just a thought. There probably is a cleaner way. Heh, I'm actually kinda surprised the '(actually hitting tab here)' didn't work. I don't know how to do it with grep but you can use perl to make a really simple grep like program that will allow tabs. \t is the perl regex for tab.


0 0
# no tabs for Python please! grep -q ' ' *.py && exit 1$ egrep '\t' FILE ... $ grep -E '\t' FILE ...$ grep "`printf '\t'`" FILE ...$ grep '+' FILE ...$ grep -P '\t' FILE ...bash$ grep $'\t' FILE ...

(Recommended) Use regular expression syntax supported by grep(1). Modern grep(1) supports two forms of POSIX 1003.2 regex syntax: basic (obsolete) REs, and modern REs. Syntax is described in details on re_format(7) and regex(7) man pages which are part of BSD and Linux systems respectively. The GNU grep(1) also supports Perl-compatible REs as provided by the pcre(3) library.

Both basic regular expressions and Linux extended REs apparently have no support for the \t. Please consult UNIX utility man page to know which regex language it supports (hence the difference between sed(1), awk(1), and pcregrep(1) regular expressions).

For Bourne shell (and not only) the same behaviour may be emulated using command substitution...

0 0
0 0

Hi Adam, Michael,

Michael, did you install a version of grep, say with Fink? It doesn't work for me either. Also neither "man grep" nor "man re_format" mention escape characters. Try /usr/bin/grep and see if you get the same result. Also, if I could convince you to use zsh, you could use "where grep" to see all versions of grep in your path. With bash, "which grep" would at least tell you the location of the first one found.

Adam, is there a problem with using a literal tab character? You have to quote the tab but you can enter it in the command line with the key sequence -v,. The tab character is special in most shells, initiating command completion, so you must escape it's special meaning with the -v.
'Naomi, sex at noon taxes.' I moan.
Never odd or even.
A man, a plan, a canal, Panama.
Madam, I'm Adam.
Sit on a potato pan, Otis.
Sit on Otis.
-- The Mad...

0 0

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...

0 0
XTerm – Frequently Asked Questions (FAQ)
Copyright © 1997-2016,2017 by Thomas E. Dickey

Here is the latest version of this file.

From the manual page:

The xterm program is a terminal emulator for the X Window System. It provides DEC VT102/VT220 and selected features from higher-level terminals such as VT320/VT420/VT520 (VTxxx). It also provides Tektronix 4014 emulation for programs that cannot use the window system directly. If the underlying operating system supports terminal resizing capabilities (for example, the SIGWINCH signal in systems derived from 4.3bsd), xterm will use the facilities to notify programs running in the window whenever it is resized.

That is, xterm (pronounced "eks-term") is a specific program, not a generic item. It is the standard X terminal emulator program.

This FAQ presents various useful bits of information for both the specific program as well as other...

0 0
Perl as a command line tool


In modern Unix programming environment Perl can be used as AWK replacement even in simple scripts. AWK no longer has advantage due to its more compact size as on modern computer load time of Perl interpreter is negligible unless it is done in a deeply nested loops. Recently Perl became a standard, installed by default in all major Unixes including AIX, HP-UX, Linux and Solaris. It is usually available in /usr/bin.

Default availability dramatically changed role of Perl in Unix system scripting and routine text processing. For most administrators it is much easier to use Perl that any of the older alternatives (AWK, sed), or newer scripting languages like Python and Ruby because it is closer to shell, the language they already know (and many know really well).

Python is far less "aligned" with Unix then Perl. Ruby puts too much emphasis on OO and is not available by default on any of the major platforms. That does not...

0 0