Artificial intelligent assistant

Sort lines while grouping indented lines with their parent I have an index-of-a-book-like list of lines, say day satur- sun- holy- night ball to- eve election christmas Now I want to sort these lines in the obvious fashion: I want to group every “parent”-entry (`day`, `night`, `eve`) with their respective indented “child”-entries (`satur-`, `sun-`, …) and sort these groups by their parent-entry. I also want to sort the child-entries within any given group. Thus, the desired output is: day holy- satur- sun- eve christmas election night ball to- How would I best achieve this by using unix core tools like `sort`?

You could pick a character that's unlikely to occur in your text file, prepend the parent name + that character to each child line, sort then remove the parent name and the separator from each child line e.g. with `gnu` sed and a low ascii char like `\x02`


sed '/^[^[:blank:]]/h;//!G;s/\(.*\)\
\(.*\)/\2\x02\1/' infile | sort | sed 's/.*\x02//'


How it works:
the 1st `sed` does the following:
`/^[^[:blank:]]/h` \- copy non indented lines (parents) over hold space
`//!G` \- on indented lines (children) append hold space content to pattern space
`s/\(.*\)\
\(.*\)/\2\x02\1/` \- swap lines in pattern space replacing the `\
`ewline with `\x02`
after that, `sort` and remove everything up to and including `\x02` with a 2nd `sed 's/.*\x02//'`

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy b9027c9824d40e0cd462abecb013f107