Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
#cat sample
1   -1  2
2   2   2
2   1-1 3

I need to get all the lines that contains negative value, that is 1st line only. Values are tab-separated. First I tried

# grep "\-1" sample
1   -1  2
2   1-1 3

But if I try

 grep "\t\-1" sample

I get nothing (no match). What am I doing wrong?

share|improve this question
up vote 3 down vote accepted

In Bash, you can do this:

grep $'\t-1' sample

The $'' causes escapes to be interpreted.

It's not necessary to escape the minus sign.

share|improve this answer
This reminds me: everyone should go and (re)read bash documentation every once in a while. You sort of discover something that you didn't know before every time. – cjc Jan 19 '12 at 16:56
It works too, thank you. Unfortunatelly I can't mark more than 1 answer as accepted. – Putnik Jan 19 '12 at 18:53

Try something like:

grep -P '\t-1' sample

By the way, the sample you've provided is tab-indented. You may try the following for generic whitespace matching:

grep -P '\s-1' sample
share|improve this answer
Thank you, -P is what I need! :) – Putnik Jan 19 '12 at 18:51

There is no facility for \t being tab in basic regular expressions (those described by man 7 regex). As Georgi Hristozov pointed out, you can use Perl Compatible Regular Expressions to get this ability. Some implementations of grep won't support -P, but other basic tools will have more rich regular expression languages, here are some examples which should work:

sed -n '/\t-/p' sample
awk '/\t-/' sample
perl -ne '/\t-/ && print' sample

In order to get it working with regular grep (without using pcre) you are going to have to put a literal tab expression in your regex. In many shells where the tab key does something like completion, you can get a literal tab character with Ctrl-v<tab>. so you'd type:

grep 'Ctrl-v<tab>-' sample
share|improve this answer

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) print}'
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.