0

extract starting and ending with a special character

Content from the file:

/genomes/date/pa341/abc.txt

/genomes/date/ha76870/xyz/a1.pdf

Result should be

/genomes/date/pa341/

/genomes/date/ha76870/xyz/
Fravadona
  • 13,917
  • 1
  • 23
  • 35
Aht
  • 1
  • 1
  • `xargs dirname < file.txt` – Shawn Dec 12 '22 at 15:17
  • you're only removing the filename from the filepaths; try to come up with something using `sed` – Fravadona Dec 12 '22 at 15:17
  • @Shawn that's a wild use of `xargs` o_O – Fravadona Dec 12 '22 at 15:19
  • 1
    @Fravadona I like to try to think outside the box. – Shawn Dec 12 '22 at 15:27
  • 1
    Comparing your input and output, all I see is stripping filename and extension. What's this about "special characters"? – Charles Duffy Dec 12 '22 at 18:30
  • 1
    @Shawn, ...try that with `/genomes/by-researcher/Firstname Lastname/123.txt` -- it'll return `/genomes/by-researcher` as one line and `Lastname` as a second one, because `xargs` when called without `-d` or `-0` operates on words (with shell-like but not fully shell-compatible parsing/splitting rules), not lines. Should at least be (with GNU xargs) `xargs -d $'\n'` dirname -- – Charles Duffy Dec 12 '22 at 18:31

0 Answers0