Unix / Linux Shell: Get Third Field Separated by Forward Slash (/) Delimiter

I‘m writing a shell script for automation purpose. The output the path of the current working directory is stored in $PWD or it can be obtained using the pwd command. How do I find out find out 3rd field separated by the forward slash (/) delimiter using $PWD under Unix like operating systems?

You can display selected parts of lines from each FILE or shell variable using any standard Unix command line utility.

ADVERTISEMENTS

cut Command Example

You can use the cut command as follows:

var="/home/vivek/foo/bar"
cut -d/ -f4 <<<"${var}"

To get the sample value from a shell variable called $PWD, enter:

cut -d/ -f4 <<<"${PWD}"

OR

value=$(cut -d/ -f4 <<<"${PWD}")
echo "$value"

You can replace the variable name with the file name:

cut -d/ -f4 /path/to/file.name

Where,

  1. -d/ : use / for field delimiter.
  2. -f4 : Select only fourth field. Also print any line that contains no delimiter character, unless the -s option is specified.

awk Command Example

awk is a pattern scanning and text processing language. The syntax is as follows:

var="/home/vivek/foo/bar"
awk -F/ '{ print $4}' <<<"${var}"

OR

value=$(awk -F/ '{ print $4}' <<<"${PWD}")
echo "$value"

You can replace the variable name with the file name:

awk -F/ '{ print $4}' /path/to/file.txt

Where,

  1. -F/ : sets the field separator, FS, to /.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
8 comments… add one
  • Mishka Apr 29, 2012 @ 16:37

    Well, it’s somewhat blurry why exactly the 3rd component required. If it’s about getting parent directory of the current directory it can be solved as follows:

    $ basename `dirname $PWD`
    

    Of course there is a lot of ways to do that, but the cut(1) and awk(1) IMHO the most straightforward ones. Thanks!

    In case of need the following prints exactly the “3rd” component in pure shell:

    $ ( IFS=/ ; p="/home/vivek/foo/bar" ; set -- $p ; echo $4 )
    foo
    
    • 🐧 nixCraft Apr 29, 2012 @ 17:50

      Actually, I forgot about the last pure shell based solution. I appreciate your comment.

  • JackR May 5, 2012 @ 14:23

    Sorry but <<< operator is new to me. What exactly do <<< operator and what is called?

  • frhling Dec 10, 2012 @ 12:24

    If in 9th field($9) there is sth like: A/B/C and one needs C. how can we extract C and put it in output?

  • Prakash Sep 19, 2013 @ 12:07

    What if I want to add a foldername after every 2nd ‘/’.

    Basically I’ve big list of files and I want to add one folder to the path of every file.

    Help is appreciated !

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.