To read last value generated by pwd using grep/sed/awk command

Viewed 98

To read last value generated using grep/sed/awk command

[root@test 4]# pwd
/opt/lib/insta/4

from above pwd to read value 4.

i am trying something like this which is not correct exactly

pwd | grep -oP 'insta/?'
2 Answers

Without using any external utility, you can do this in bash:

echo "${PWD##*/}"

4

Or using awk:

awk -F/ '{print $NF}' <<< "$PWD"

4

You could set the field separator to / and print the last field if the second last field is insta

pwd | awk -F / '{
  if ($(NF-1) == "insta") {
    print $NF
  }
}'

Output

4

Or you could print the last field:

pwd | awk -F / '{print $NF}'
Related