Results 1 to 6 of 6

Thread: Grep .

  1. #1
    Join Date
    Nov 2016
    Beans
    16

    Grep .

    With some effort I was able to find a reference to the "." in the following: grep . /sys/bus/usb/devices/*/power/wakeup
    as being a sort of wildcard to include hidden files or directories.

    My confusion is that nothing is hidden in that path, but if I exclude the "." the output is nothing.

    In other words grep /sys/bus/usb/devices/*/power/wakeup returns no result
    but grep . /sys/bus/usb/devices/*/power/wakeup does in fact return results.

    Can't find anything that explains what role the "." plays in that search.

  2. #2
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    19,744
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Grep .

    Code:
    $ man grep
    grep has 2 or more arguments. Something like
    Code:
    $ grep <options> <regex>  <file-globbing patter to search>
    The '.' is a valid regex. It matches any character. There are slightly different regex languages from a simple one to the full perl regex.

    grep -E will get the full perl-regex. Also, egrep is an alias for that.

    So ... if you want to understand how grep works, you'll need to learn regex. Start there.

    The <file globbing patter to search> is interpreted by your shell, typically bash, to provide a list of input files to grep/egrep to be searched. Different shells handle that differently and have different limitations for expansion. For example, if the globbing matches more than 20 files, that could easily overfill what the bash buffer limit allows. There are ways around that, using another tool - xargs.

    Some commands are best learned by seeing examples. egrep and find are 2 I think work best that way. Search for grep examples.

    Be certain you know the difference in how your shell deals with "" and '' groupings too. I usually use '' around my regex patterns to prevent the shell from doing odd things. A common one I use weekly is
    Code:
    $ egrep -i 'warn|erro' /var/log/*log
    This searches all current log files for error or warnings in a line. Journalctl supports regex searches too, so eventually, I won't need that egrep.

    The main reason the person use grep at all in that example, was to condense the contents of some very specific files into 1 line for each file, while having the filename output too. Nothing more. They could have used
    $ less /sys/bus/usb/devices/*/power/wakeup
    or
    $ more /sys/bus/usb/devices/*/power/wakeup
    or
    $ cat /sys/bus/usb/devices/*/power/wakeup

    Each isn't a condensed and clear as the grep . /sys/bus/usb/devices/*/power/wakeup command. Try each of them. See the differences?
    Last edited by TheFu; 1 Day Ago at 08:29 PM.

  3. #3
    Join Date
    Nov 2008
    Location
    Metro Boston
    Beans
    14,919
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: Grep .

    To search for a character with special meaning in a regex like the period, you need to "escape" the character with a backslash like this:
    Code:
    grep \. SomeTextOrFileWithA.InIt
    Like TheFu I usually enclose any regex search string in single quotes. That convention is used throughout the shell to have it treat the string inside the quotes literally. So the command above could also be written:
    Code:
    grep '.' SomeTextOrFileWithA.InIt
    The shell distinguishes between single and double quotes. With double quotes, bash first replaces any references to "environment variables" with their assigned values before interpreting the string. So
    Code:
    echo "Show me my $HOME"
    would return
    Code:
    Show me my /home/seijisensei
    with my actual home directory substituted for $HOME. If instead I used the command
    Code:
    echo 'Show me my $HOME'
    it would return
    Code:
    Show me my $HOME
    There are a lot of good guides to regular expressions on the Web. Just search Google for "regular expression".
    Last edited by SeijiSensei; 1 Day Ago at 08:18 PM.
    If you ask for help, do not abandon your request. Please have the courtesy to check for responses and thank the people who helped you.

    Blog · Linode System Administration Guides · Android Apps for Ubuntu Users

  4. #4
    Join Date
    Nov 2016
    Beans
    16

    Re: Grep .

    Thanks so much.

    I found nothing in man grep, but what your saying makes sense.. Thanks for the time and effort to respond.

  5. #5
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Grep .

    Don't give up on man pages, just make your time to learn the jargon.

    Code:
    man grep | less "+/REGULAR EXPRESSIONS"
    man grep | less "+/^.+The period"
    Last edited by sisco311; 1 Day Ago at 10:08 PM.

  6. #6
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    19,744
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Grep .

    Manpages completely rock!

    Almost all the knowledge of the Unix world is held inside those files. Some are better than others. The ssh and sshd_config manpages are works of art. So is the rsync one. Freakin' amazing.

    Grep's manpage isn't bad, assuming you already understand regex. Without understanding regex, it seems like a high-paid consultant - everything said is true, but not what you need to know, now.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •