Find the answer to your Linux question:
Results 1 to 7 of 7
Good Evening! I have collided with grep command problem. I have a 1.txt file which filled with this hello hello dlrow. Is there is any grep command by which I ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2012
    Posts
    4

    Question Grep, inverted word search


    Good Evening!

    I have collided with grep command problem. I have a 1.txt file which filled with this hello hello dlrow. Is there is any grep command by which I can input "world" and output will be "world"? In other words how to make right to left search with grep? I have tryed many commands and I did not find same examples in internet. I also tried grep|rev but the output was world olleh olleh I need only one word "world" in output. Thanks!

    grep |rev "world" 1.txt
    rev: world: No such file or directory
    world olleh olleh
    Usage: grep [OPTION]... PATTERN [FILE]...
    Try `grep --help' for more information.

  2. #2
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    477
    welcome to the forum

    using your example file 1.txt="hello hello dlrow"
    this seems to work:

    rev 1.txt | tr [:blank:] "\n" | grep world

    the tr command puts each word on its own line
    the sun is new every day (heraclitus)

  3. #3
    Just Joined!
    Join Date
    Sep 2012
    Posts
    4
    Quote Originally Posted by tpl View Post
    welcome to the forum

    using your example file 1.txt="hello hello dlrow"
    this seems to work:

    rev 1.txt | tr [:blank:] "\n" | grep world

    the tr command puts each word on its own line
    Thanks! Sorry for those delay. I typed $ rev 1.txt | grep -o world. But I thought on one thing if we have the same file 1.txt with hello hello dlrow world what command I need if I want that output give world and dlrow with out hello hello. I tryed this command grep -e "world\|dlrow" 1.txt But outputjust give me hello hello dlrow world. Thnaks again!

  4. #4
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,533
    Try grep -e -o "world\|dlrow" 1.txt

    More information on grep which has a huge number of options to configure it.
    What do we want?
    Time machines!

    When do we want 'em?
    Doesn't really matter does it!?


    The Fifth Continent

  5. #5
    Just Joined!
    Join Date
    Sep 2012
    Posts
    4
    Unfortunately this command did not get right output:

    [asus@mgt1 test]$ grep -e -o "world\|dlrow" 1.txt
    grep: world\|dlrow: No such file or directory
    [asus@mgt1 test]$ grep -e -o "dlrow\|dlrow" 1.txt
    grep: dlrow\|dlrow: No such file or directory
    [asus@mgt1 test]$

  6. #6
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,533
    Sitting in front of my Linux box now. Cretaed a text file called test.txt with the following contents
    Code:
    hello blah
    blah
    olleh
    blah
    blah
    hello olleh
    blah
    and then ran
    Code:
    grep -E 'hello|olleh' -o test.txt
    and got the results expected. Which were
    Code:
    hello
    olleh
    hello
    olleh
    What do we want?
    Time machines!

    When do we want 'em?
    Doesn't really matter does it!?


    The Fifth Continent

  7. #7
    Just Joined!
    Join Date
    Sep 2012
    Posts
    4
    Quote Originally Posted by elija View Post
    Sitting in front of my Linux box now. Cretaed a text file called test.txt with the following contents
    Code:
    hello blah
    blah
    olleh
    blah
    blah
    hello olleh
    blah
    and then ran
    Code:
    grep -E 'hello|olleh' -o test.txt
    and got the results expected. Which were
    Code:
    hello
    olleh
    hello
    olleh
    Thank you for this commands, it worked!

Posting Permissions

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