I have a string which looks something like thisdir/subdir/othersubdir/file.txtBut it can also look likedir/file.txtor dir/subdir/file.txtNow, I am making a script where $P is the full path to the directory were file.txt is.Now I use this sed 's/\/.*txt//g'To be replace anything that ends with txt and comes after a /. This works fine when there is something like dir/file.txt, but not any of my other examples. How can I fix so that it matches what I want?I don't want to match any subdirectories before .*txt. I want to remove only the file base name.
Strip the file base name, keep the directory part
text processing;sed;regular expression
If you don't insist on using sed then you could consider using dirname:S=dir/subdir/othersubdir/file.txtP=$(dirname $S)echo $Pdir/subdir/othersubdirS=dir/file.txtP=$(dirname $S)echo $Pdir