Solved

Unix Shell Command to extract portion of a string

Posted on 2013-01-14
1
563 Views
Last Modified: 2013-01-14
I have a directory name in a string and I need to extract all except the last 2 subdirectories.  For example:

Starting String:

/abc/def/ghi/jkl/mno/

I need to get:
/abc/def/ghi

The number of levels of the directory is variable, but I always need to chop off the last two subdirectories from the string.  

I would like to do this in a single command.
0
Comment
Question by:rip55jcp
1 Comment
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 38774455
dir=/abc/def/ghi/jkl/mno/
stripped=$(dirname $(dirname $dir))

echo $stripped
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to mount an LVM partition that is part of a disk image. 3 73
LINUX backups with VEEAM 8 82
awk sed variable in file 3 38
number in printf 13 27
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question