Search code examples
linuxbashawkcut

How to take a text between "/" with Awk / cut?


I have this command in a script:

find /home/* -type d -name dev-env 2>&1 | grep -v 'Permiso' >&2 > findPath.txt

this gives me this back:

/home/user/project/dev-env

I need to take the second parameter between "/" (user) to save it later in a variable. I can not find the way to just pick up the "user" text.


Solution

  • Using cut:

    echo "/home/user/project/dev-env" | cut -d'/' -f3
    

    Result:

    user
    

    This tells cut to use / as the delimiter and return the 3rd field. (The 1st field is blank/empty, the 2nd field is home.)

    Using awk:

    echo "/home/user/project/dev-env" | awk -F/ '{print $3}'
    

    This tells awk to use / as the field-seperator and print the 3rd field.