coreutils manual says

tsort reads its input as pairs of strings, separated by blanks, indicating a partial ordering.

If a string has a blank within it, according to the manual, I can't use tsort on it and other strings. How can I still use tsort on the string and other strings? Thanks.

  • 1
    What happened when you tried? – Jeff Schaller Nov 17 at 16:28
  • " I'm confused by your question" – Tim Nov 17 at 16:40
  • I mean, the man page says that it delimits the inputs by blanks, and so I might be curious to try running it with various inputs, such as a b c or "a b" c and see what happened. If I was then confused about what happened, I'd propose a question that showed the documentation, my understanding of it, the inputs I tried, the output I got, and what output I expected. That's why I have downvoted this question. Does that clarify my "what happened when you tried?" question? – Jeff Schaller Nov 17 at 17:35
  • 1
    If I think a question is self clear without those hassles, I won't go that way. – Tim Nov 17 at 17:42
up vote 5 down vote accepted

You can't use tsort directly if some of your input strings contain whitespace.

You can check the source, you'll see the delimiters are hardcoded to space, tab and newline and there's no option to change them.

If you want to use tsort on a data set where words may contain whitespace, my recommendation would be to pre-process the data set to encode whitespace as a non-blank character (or sequence of non-blank characters), then run tsort on it and finally post-process the final output to decode it back into the original whitespace.

You can probably use sed for the pre- and post-processing steps. Which character to use to encode whitespace depends on your data set, if there are other characters that are invalid (e.g. # or @, $ or \), maybe you can simply use them directly. Otherwise, you might want to consider a two character encoding (e.g. encode space as \s) and include a way to encode the quoting character itself (e.g. \\ to encode a single backspace.)

  • 1
    For single character encodings, tr is likely to be easier. – D. Ben Knoble Nov 17 at 19:21
  • Thanks. Filipe and @D.BenKnoble: if a string has a blank within it, and the string separator is a blank, can you "pre-process the data set to encode whitespace as a non-blank character" using some program without manual distinguish a blank between within-a-string and separating-two-strings cases? – Tim Nov 18 at 13:28
  • @Tim It depends on your specific case... If you have one entry per line (one pair using two consecutive lines) then it's safe to replace spaces with another character, leaving newlines unchanged... Without knowing the specifics of what you're trying to accomplish, it's hard to say whether you can automatically distinguish the cases after assembling the file, or if you need to handle them beforehand... – Filipe Brandenburger Nov 18 at 16:18

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.