質問

We are having a small issue would appreciate any help.

We have the following path :

/root/dir1/dir2/dir3/file.sav

We also have :

 /root/dir1/dir2

We would like to do something like /root/dir1/dir2/dir3/file.sav - /root/dir1/dir2 and get only dir3/file.sav.

We have tried working with tr -d but with no success.

役に立ちましたか?

解決

There's no need for an external tool. Bash can solve this problem for you by itself. Use ${variable#substring} to print a variable with a substring deleted from the beginning. Example:

$ PATH1=/root/dir1/dir2/dir3/file.sav
$ PATH2=/root/dir1/dir2
$ echo ${PATH1#${PATH2}/}
dir3/file.sav

他のヒント

Just offering other alternates...

bash:

echo ${PATH1/${PATH2}\//}

sed:

sed "s#$PATH2/##" <<< $PATH1

awk:

awk -v var="$PATH2" '{sub(var"/","",$0)}1' <<< "$PATH1"

replace:

replace "$PATH2/" "" <<< "$PATH1"
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top