Skip to main content
2 of 5
added 497 characters in body
Melab
  • 4.4k
  • 10
  • 41
  • 59

Using sed to replace all occurrences at the beginning with a matching number of replacement strings

I'm looking to manipulate the output of $tree --noreport$ in such a way that replaces the leading box-drawing characters and spaces on each line with a matching number of spaces. If I were to write the pattern for matching these characters, it would be ^\\(\u2500\\|\u2514\\|\u251C\\| \\)*\u2500. This string would be wrapped in $'...' because Unicode escape sequences are not recognized by sed. This pattern occurs on every line of the output of tree --noreport except for the first. Each character in each matching string needs to be replaced with a space.

Example input:

.
├── docs
│   ├── jokes
│   │   └── knock_knock.txt
│   └── work
├── images
└── .profile

Example output:

.
    docs
        jokes
            knock_knock.txt
        work
    images
    .profile

The output I'm trying to get to using this question as a stepping stone:

.
    docs/
        jokes/
            knock_knock.txt
        work/
    images
    .profile
Melab
  • 4.4k
  • 10
  • 41
  • 59