0

I have doing tons of research and can't seem to figure it out.

I am using sed and trying to process a file of paths

/path/to/somewhere/for-something.rar

I need to remove everything past the last / but not the / itself. Can anyone help me with this? I can never handle sed lol

4
  • It looks like you want to do something that dirname command do. Can you try this one?
    – mrc02_kr
    Commented Nov 14, 2019 at 6:44
  • Every line is different. I have a find command that searches for .rar files and another that extracts them. But I need it to show only the folder name for the command to work properly Commented Nov 14, 2019 at 6:52
  • That's exactly what dirname do. But to be sure, could you provide examples of input and output?
    – mrc02_kr
    Commented Nov 14, 2019 at 7:01
  • Could you say something about the underlying issue in your question, e.g. why you have a file with pathnames in it and whether this is part of solving -another task? Storing pathnames in a file is rarely necessary.
    – Kusalananda
    Commented Nov 14, 2019 at 8:37

2 Answers 2

0

Use dirname:

path=/path/to/somewhere/for-something.rar
dir="$(dirname $path)/"

Or use shell:

dir="${path%\/*}/"

Output:

$ echo "$dir"
/path/to/somewhere/
0

To find all "/..../file.rar" and print "/..../" in a file-of-paths you can use good old grep...

grep -Po '/\S+/(?=\S+\.rar\b)' file-of-paths

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .