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

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

To get the third field from a string that is separated by forward slash (/) characters in a Unix or Linux shell, you can use the cut command with the -d option to specify the delimiter, and the -f option to specify the field number.

For example, to get the third field from the string /var/log/messages, you can use the following command:

ref‮t re‬o:lautturi.com
echo "/var/log/messages" | cut -d '/' -f 3

This will output log, which is the third field in the string.

You can also use the awk command to extract fields from a string that is separated by a delimiter. For example, to get the third field from the string /var/log/messages using awk, you can use the following command:

echo "/var/log/messages" | awk -F '/' '{print $3}'

This will output log, which is the third field in the string.

It is important to note that both the cut and awk commands are zero-indexed, so the first field is field 0, the second field is field 1, and so on.

For more information about the cut and awk commands and their options, you can consult the documentation for your specific Unix or Linux system.

Created Time:2017-10-30 14:27:29  Author:lautturi