Find the answer to your Linux question:
Results 1 to 8 of 8
searching a file with wildcard characters in the search criteria example: searching a property file abc.txt for getting a result string "heartbeat" file contents as below: heartbeat hardbeat nobeet none ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2009
    Posts
    8

    [SOLVED] help with grep / egrep wildcard search


    searching a file with wildcard characters in the search criteria

    example: searching a property file abc.txt for getting a result string "heartbeat"

    file contents as below:
    heartbeat
    hardbeat
    nobeet
    none


    command executed is:

    heart*
    he*beat
    *beat

    all the above should print the result as heartbeat

    Assuming many files like abc.txt are in the folder, the final command/shellscript should print matching results from all the files.

  2. #2
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    477
    how about something like this:

    cd /folder
    grep heartbeat *

    ?
    the sun is new every day (heraclitus)

  3. #3
    Just Joined!
    Join Date
    Sep 2009
    Posts
    8
    Quote Originally Posted by tpl View Post
    how about something like this:

    cd /folder
    grep heartbeat *

    ?
    thanks, but user do not know full string, so he will try something like hea*et.

    grep hea*et is not fetching heartbeat, which is my problem.

  4. #4
    Linux Newbie lugoteehalt's Avatar
    Join Date
    Jan 2004
    Posts
    232
    Quote Originally Posted by leo7889 View Post
    thanks, but user do not know full string, so he will try something like hea*et.

    grep hea*et is not fetching heartbeat, which is my problem.
    Should think
    Code:
    $ egrep "hea.*et"
    would find it. Not an expert, the . may not be necessary. You could also have
    Code:
    $ egrep "^hea.*et$
    if you knew it was the only word on the line. Using bot ^ and $ is overkill (copyright Pentagon).

    It is highly instructive to make it colour what it has found: --colour ??
    All power is violence; all power is evil.
    Money As Debt

  5. #5
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    Quote Originally Posted by leo7889 View Post
    grep hea*et is not fetching heartbeat, which is my problem.
    The * searches for the preceeding character, in this case 'a', zero or more times. So strings like the following would match:

    heet
    heaet
    heaaet

    Since you want any character zero or more times precede the '*' with the metacharacter '.'. So you should use:

    hea.*et

  6. #6
    Just Joined!
    Join Date
    Sep 2009
    Posts
    8

    Thumbs up Thanks guys

    lugoteehalt and lomcevak

    Thanks for your answers and explanation. Will try this and get back to you.

  7. #7
    Just Joined!
    Join Date
    Sep 2009
    Posts
    8

    Thanks alot, it works

    I really appreciate your time answering this issue, it works like a charm.

    I have another small issue left out, that is when I search for the search string if the result line has a \ at the end of line, I am not getting the text on the next line, which I need to show, is that possible?

  8. #8
    Just Joined!
    Join Date
    Sep 2009
    Posts
    8

    Found the soultion

    Found it from a friend.

    grep -A1 "heart.*at" prop.txt


    Result:
    ----------
    heartbeat \
    more beats

Posting Permissions

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