Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

How to get only the line indicated with "grep" ...

 
SOLVED
Go to solution
Highlighted
Super Advisor

How to get only the line indicated with "grep" ...

Hi .. how can i get only "/usr" if there are directories containing the same word as follows?
i am in / ... into / there are the following directories and fiiles:
dr-xrwxrwx 5 bin bin /usr
drwxrwxrwx 5 bin bin /usr/user1
drwxrwxrwx 5 bin bin /usr/user2/b.txt
drwxrwxrwx 5 bin bin /usr/user3/a.txt
drwxrwxrwx 5 bin bin /usr/company1/
drwxrwxrwx 5 bin bin /usr/company1/user4
drwxrwxrwx 5 bin bin /usr/company1/user5

if i list /usr i get:
i am under /
ls -lrt | grep /usr .... i get all the lines that contains "/usr" ...
dr-xrwxrwx 5 bin bin /usr
drwxrwxrwx 5 bin bin /usr/user1
drwxrwxrwx 5 bin bin /usr/user2/b.txt
drwxrwxrwx 5 bin bin /usr/user3/a.txt
drwxrwxrwx 5 bin bin /usr/company1/
drwxrwxrwx 5 bin bin /usr/company1/user4
drwxrwxrwx 5 bin bin /usr/company1/user5


but i only want to get theline that only contains "/usr" as follows:
dr-xrwxrwx 5 bin bin /usr

and .. how about if i only want to get the line that says: /usr/company1/

ls -lrt | grep "/usr/company1/"
i only want to get:
drwxrwxrwx 5 bin bin /usr/company1/

and not all the lines below ...
drwxrwxrwx 5 bin bin /usr/company1/
drwxrwxrwx 5 bin bin /usr/company1/user4
drwxrwxrwx 5 bin bin /usr/company1/user5

please your help, thanks in advance.
12 REPLIES 12
Highlighted
Honored Contributor
Solution

Re: How to get only the line indicated with "grep" ...

Hi,

For your case just try adding a blank space before last double quotes:
#ls -lrt | grep "/usr/company1/"

Rgds.
Highlighted

Re: How to get only the line indicated with "grep" ...

>how can I get only "/usr" if there are directories containing the same word as follows?

If you are looking for directories, you are going about this wrong. Use -d to only list the one directory: ll -d /usr /usr/company

But if you are practicing grep, you can look into -w or -x.

>Jose: For your case just try adding a blank space

That's not likely to work since there isn't a blank at the end of the line. You can use "$" as the end of line anchor instead:
ls -lrt | grep "/usr/company1/$"
Highlighted
Super Advisor

Re: How to get only the line indicated with "grep" ...

thanks for answering ..
never mind if it is a directory or file .. i mean, i am doing a loop and there i do not specify if it is a directory or not ... i only get the line with that word "/usr/company1/" the problem here is i get the 5 lines below when i use "grep", i forget tell you i have files under those directories ...

drwxrwxrwx 5 bin bin /usr/company1/
drwxrwxrwx 5 bin bin /usr/company1/user4
drwxrwxrwx 5 bin bin /usr/company1/user5
-rwxrwxrwx 5 bin bin /usr/company1/user5/file1
-rwxrwxrwx 5 bin bin /usr/company1/user5/file2

if i want to get the line with "/usr/company1/" i only want to get
drwxrwxrwx 5 bin bin /usr/company1/ and not the 5 lines .. how can i do that? what command or parameter can i use?

thanks in advance.





Highlighted

Re: How to get only the line indicated with "grep" ...

>never mind if it is a directory or file

That's important. If you don't want to expand directories, add that -d. (It doesn't change how non-directories are displayed.)

>how can I do that? what command or parameter can I use?

As I said above, adding -d would be easier. Or you can use "$" in the grep.
Highlighted
Acclaimed Contributor

Re: How to get only the line indicated with "grep" ...

Hi Manuales:

It is worth your noting that in addition to limiting matches to a string that is anchored (pinned) to the end of a line, you can also match a string only if it is anchored to the beginning of a line.

The '$' as the last character of a regular expression anchors the match to the end of a line.

The '^' as the first character of a regular expresson anchors the match to the beginning of a line.

Hence, as noted, to match '/usr/company/1' in your example, you could do:

# ... grep /usr/company/1$

If you had a 'ls -l' listing of both files and directories, you could match only files, for example, by doing:

# ls -l | grep ^-

...since files are listing on a line that begins with a '-'. Correspondingly, since symbolic links begin with 'l', this would find those:

# ls -l | grep ^l

For much much more about regular expressions, see the manpages for 'regexp(5)'.

Regards!

...JRF...
Highlighted
Honored Contributor

Re: How to get only the line indicated with "grep" ...

Hi manuales,

long story short:
use
ls -lrt ... | grep '/usr/company1/$'

or if there may or may not a "/" at the end of your string
ls -lrt ... | grep '/usr/company1/*$'

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Highlighted
Super Advisor

Re: How to get only the line indicated with "grep" ...

Hi .. it works:
ls -lrt ... | grep '/usr/company1/*$'

thanks a lot.
Highlighted
Honored Contributor

Re: How to get only the line indicated with "grep" ...

I am not sure it is working in that way ? OR I misunderstood totaly what you are looking for:
I mean:
# ls -lrt ... | grep '/usr/local/*$'
... not found
# ls -lrt | grep '/usr/local/*$'

but;

# cd
# ls -lR | grep "/usr/local/*"
./usr/local/include:
./usr/local/lib:
./usr/local/man:
./usr/local/man/de:


(you need to put company1 instead of local)
Highlighted
Honored Contributor

Re: How to get only the line indicated with "grep" ...

# ls -ald /usr/company1