Remove first portion of string

I have a script which currently uses a file containing a list of directories as an argument. The file is read in to an array, and then the array is iterated in a for loop.
What I would like to do is cut off the first few directories of the directory path (they won't exist on the server where the items are being copied).

Here is an example:
On local server:
/usr/local/logs/serverlogdir

On remote server:
/logs/serverlogdir <- note that the /usr/local is removed

Is there any easy way to do this?

See if this works for you:

echo /usr/local/logs/serverlogdir | sed 's#.*\(/.*/\)#\1#'
1 Like

Haha, you know, immediately after posting it I remembered that sed exists. Its been a many-cup-of-coffee type of morning. Thanks for answering! I know it'll work :slight_smile:

Or if your array is called A, you could try this:

for i in "${A[@]#/usr/local}"
do
  echo "$i"
done
 
echo "/usr/local/logs/serverlogdir" | sed 's!.*\/local!!'
echo "/usr/local/logs/serverlogdir" | awk -F"/" '{ $1=$2=$3=""; }1' OFS="/"

Althoguh awk produces excess "/" infront of the output , it should not cause any issue to functionality.

Here is another way to do this in ksh:

var="/usr/local/logs/serverlogdir"; echo ${var#/*[a-z]/*[a-z]/}