Find the answer to your Linux question:
Results 1 to 4 of 4
I am having trouble to grep a command result against another. I am trying to take the ls -la command and then grep this against a script that was written ...
  1. #1
    Just Joined!
    Join Date
    Nov 2006
    Posts
    1

    grep off a command

    I am having trouble to grep a command result against another. I am trying to take the ls -la command and then grep this against a script that was written called dates.sh. I though the syntax is:

    ls -la|grep $dates.sh|less

    Any help is greatly appreciated. Thanks.

  2. #2
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    Try
    Code:
    ls -la | grep 'dates.sh' | less
    (Although if you're only expecting one result, it's not necessary to pipe the output to less.)

  3. #3
    Linux User
    Join Date
    Aug 2005
    Posts
    408
    ls -la|grep $dates.sh|less

    If you're writing it like this, the problem is that you need spaces between your pipes.

    Code:
    ls -la | grep dates.sh
    In addition, it's not a variable, but a filename you're looking for, so you need no "$". In addition, there is nothing to pipe into the program "less" because you're just looking for the entry with list. If you want to feed the whole script into less and you know it's there, you only need to pass the name of the file to less, like so:
    Code:
    less dates.sh

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Well, this is somewhat unclear.

    Are you trying to find the results of the ls inside of the dates.sh file? Or are you trying to find the text 'dates.sh' inside of the ls output?

    If the former, this will not work: you will actually need to use the "xargs" command. Something like this:
    Code:
    ls -la | xargs -I{} grep -H '{}' dates.sh
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

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