Results 1 to 9 of 9

Thread: Grep .

  1. #1
    Join Date
    Nov 2016
    Beans
    22

    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,830
    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 Week Ago at 08:29 PM.

  3. #3
    Join Date
    Nov 2008
    Location
    Metro Boston
    Beans
    14,937
    Distro
    Kubuntu 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 Week 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
    22

    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 Week Ago at 10:08 PM.

  6. #6
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    19,830
    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.

  7. #7
    Join Date
    Nov 2016
    Beans
    22

    Re: Grep .

    I looked first at 'man grep' in the terminal window, and searched for the pattern "grep . " and got nothing. I'll take another look, but is the terminal window the best place to look, or is there a more definitive resource for manual pages ?

  8. #8
    Join Date
    Nov 2007
    Location
    London, England
    Beans
    6,969
    Distro
    Xubuntu 20.04 Focal Fossa

    Re: Grep .

    The man pages are _the_ resource for manuals. But there are plenty of tutorials you can find with Google - the man pages can be rather terse at time

    Similar to what TheFu said,
    Code:
    grep <options> <regex>  <list of files to search>
    Options always start with a minus sign - there weren't any in your command
    Regex is the search string (regular expression). Google for tutorials on this subject. In your case, the search term was "." which matches any character, so will find all non-blank lines
    List of files in your case is whatever filenames "/sys/bus/usb/devices/*/power/wakeup" expands to.

    In your case, it appears that this will print every line from every file in that list, which looks to me like a list of usb devices with wakeup features. These files almost certainly only contain one line each.

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

    Re: Grep .

    Quote Originally Posted by yegnal View Post
    I looked first at 'man grep' in the terminal window, and searched for the pattern "grep . " and got nothing. I'll take another look, but is the terminal window the best place to look, or is there a more definitive resource for manual pages ?
    There's a difference between having a manual and having every possible combination of example. grep and the language of regex is just too complex to even attempt every possible example in any document. Plus, grep USES regex. It isn't a reference for REGEX. Regex is a language unto itself, used by many, many, tools on all computers around the world.

    a) Yes, the manpage IS the best place to learn to use a command.

    b) Yes, certain commands are best initially learned by seeing 50 examples. Google 'regex examples' or 'grep examples'. There are just so many uses.

    c) Yes, regex needs to be learned to a basic level if you intend to be a shell guru.

    d) There are slightly different versions of the regex language. Some have a much smaller vocabulary and can be implemented much easier. OTOH, the full, extended regex can be used to match any possible UTF8, Unicode, ASCII pattern that I've ever imagined. At one job, we'd use regex to create hyperlinks from documents ToC into the correct pages, figures, tables, AND from the Index back into the correct pages. That level of use is pretty seldom. Usually we just want to match a few, specific filenames.

    e) Bash supports a limited regex, sometimes called file-globbing. <-- That's the real term.

    f) There are lots of regex pattern engines online. Many have tutorials.

    g) On your system, right now, you can ask the man system about regex. man -k regex On my 20.04 system, 13 results came back. man regex is for the C function. man -s 7 regex is to explain a little about POSIX.2 regex.

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
  •