LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Simple but can't figure it out: print or store value from egrep statement (https://www.linuxquestions.org/questions/linux-software-2/simple-but-cant-figure-it-out-print-or-store-value-from-egrep-statement-4175517115/)

PelleP 09-02-2014 02:32 AM

Simple but can't figure it out: print or store value from egrep statement
 
Hi!

Quick q:

I want to filter out only the location from a file path. I.e. getting only the string /root/ from
the string /root/file.db .
I do this using regexp, no problem. So if I have the file path text string in a file I'm getting what I want using:
Code:

egrep '^(.*[\\\/])' /path/to/file
which returns
/root/file.db with /root/ in red color.

But how the h**l do I extract that into a variable? If I use awk with the same regexp it prints the whole line.

And
Code:

var=$(egrep '^(.*[\\\/])' /path/to/file)
also stores the whole line into var.

Regards,
Pelle

jensd 09-02-2014 04:27 AM

You can use dirname to do this:
Code:

dirname /root/test
/root


PelleP 09-02-2014 04:41 AM

That was a much easier way! :D
Thanks a lot!

/P


All times are GMT -5. The time now is 04:50 PM.