Interested in switch -P what does it do? And "G\t" pattern explanation?

Please do not explain du -xh or basics of command grep.

du -xh / | grep -P "G\t"
5.1G    /var/oracle/XE/datafile
5.1G    /var/oracle/XE
5.1G    /var/oracle
1.1G    /var/lib
6.9G    /var
1.9G    /opt/softwareag/webMethods/install/fix
1.9G    /opt/softwareag/webMethods/install
1.2G    /opt/softwareag/webMethods/Designer
1.3G    /opt/softwareag/webMethods/common
1.9G    /opt/softwareag/webMethods/CCE
1.2G    /opt/softwareag/webMethods/IntegrationServer/instances/default/replicate/salvage
3.0G    /opt/softwareag/webMethods/IntegrationServer/instances/default/replicate
2.3G    /opt/softwareag/webMethods/IntegrationServer/instances/default/packages
5.5G    /opt/softwareag/webMethods/IntegrationServer/instances/default
5.5G    /opt/softwareag/webMethods/IntegrationServer/instances
5.7G    /opt/softwareag/webMethods/IntegrationServer
16G     /opt/softwareag/webMethods
16G     /opt/softwareag
16G     /opt
1.1G    /usr/share
3.0G    /usr
11G     /u01/app/oracle/oradata/XE
11G     /u01/app/oracle/oradata
12G     /u01/app/oracle
12G     /u01/app
12G     /u01
39G     /

As explained in the grep manual, -P enables the use of PCREs, i.e. Perl Compatible Regular Expressions.

The PCRE expression G\t matches a G followed by a tab.

The effect is that you only get a listing of directories whose size is listed in gigabytes (or whose name happens to match the pattern).

An alternative pipeline that would more reliable match the G at the end of the first tab-delimited column only:

... | awk -F '\t' '$1 ~ /G$/'
  • 1
    Thank you a lot, i understand now. I saw from man pages , but could not understand until u explained. And i dont know why guys are giving bad points for post, when this is website for helping others. If i m less cleaver then you, and dont understand it alone, its not reason to give minus points for post. No question that is asked is bad. – Miloš Stojanović Jun 18 at 10:22
  • @MilošStojanović Sorry, but I did not downvote your question. That must have been somebody else. People downvote questions for all sorts of reasons, ranging from bad grammar or unreadable text to leaving out information. – Kusalananda Jun 18 at 10:25
  • I m just wondering. Can u help and give me advice how to make new lines when i type the post? because everything is staying at the same line. And only ```` trick i know ```` but that cant be used for simple text,because i got correction already by some people for using it for simple text. – Miloš Stojanović Jun 18 at 10:28
  • @MilošStojanović use <br /> or double new line. – rush Jun 18 at 10:30
  • 1
    @Milo you can use the formatting sandbox (meta.stackexchange.com/q/3122/270345) to test formatting and unix.stackexchange.com/editing-help to learn the syntax – muru Jun 18 at 10:36

-P enables Perl-compatible regex engine, which isn't the default one. In your example it is required to make \t treated as a tab symbol.

Otherwise grep will consider it as a plain t (escaped with \).

PS. You could easily figure this out based on man page:

Matcher Selection
   -P, --perl-regexp
          Interpret the pattern as a Perl-compatible regular expression (PCRE).  This is experimental and grep -P may warn of unimplemented features.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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