Search code examples
greppwd

How can i make grep show a line ignoring the words i want?


I am trying to use grep with the pwd command.

So, if i enter pwd, it shows me something like:

/home/hrq/my-project/

But, for purposes of a script i am making, i need to use it with grep, so it only prints what is after hrq/, so i need to hide my home folder always (the /home/hrq/) excerpt, and show only what is onwards (like, in this case, only my-project).

Is it possible?

I tried something like

pwd | grep -ov 'home', since i saw that the "-v" flag would be equivalent to the NOT operator, and combine it with the "-o" only matching flag. But it didn't work.


Solution

  • Given:

    $ pwd
    /home/foo/tmp
    
    $ echo "$PWD"
    /home/foo/tmp
    

    Depending on what it is you really want to do, either of these is probably what you really should be using rather than trying to use grep:

    $ basename "$PWD"
    tmp
    
    $ echo "${PWD#/home/foo/}"
    tmp