I have a very large word list that has been damaged, maybe by improper vim exit or memory exhaustion?

Everything in my word list should be left-justified. But now it is like this:

Cat                           Dog
Soup                          Rat
Cass                          Audrey

I can do a simple tr -d " " to make that into:


But that is not what I want. I want the right justified words on their own new line like this:


I do not know what type of blank space separates those left and right-justified words. How do I get rid of all those spaces while preserving the right hand words using tr, sed or awk?

(The above lists I typed do not look like what I mean, hopefully the question is self explanatory).


I think I see what was happening... I was using cat to go through the very long word list. Cat works a lot faster than the new info could be buffered to the screen. So it was looking like some words were appearing right-justified. I went through the same file more slowly with vim and there were no right-justified words.

closed as off-topic by Philippos, Archemar, Jeff Schaller, sourcejedi, heemayl Nov 14 '17 at 14:30

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Philippos, Archemar, Jeff Schaller, sourcejedi, heemayl
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    You get code formatting (which preserves whitespace and linebreaks) by indenting four or hitting the curly-brackets button in the editor. – Ulrich Schwarz Nov 14 '17 at 6:30
  • set -f; printf ‘%s\n’ $(<file); set +f. This is halfway a joke, because there are other types of expansion in the shell besides globs, but in some hackish cases it might be a very simple solution. – kojiro Nov 14 '17 at 12:25

etopylight was almost right:

tr -s ' \t' '\n'

because the question asks to replace tabs, too.

  • 1
    The POSIX equivalent would be tr -s ' \t' '[\n*]'. See also tr -s '[:space:]' '[\n*]' or tr -s '[:blank:]' '[\n*]' – Stéphane Chazelas Nov 14 '17 at 18:45

Basically, you could do it in GNU sed:

sed 's/\s\+/\n/g'

There you go...


You should be able to use

sed -e 's/[[:space:]]\{1,\}/\n/'

to replace any sequence of one or more whitespace characters (including oddities like formfeed and vertical tabs) with a single newline.

  • 6
    Almost portable, but most sed versions will insert a backslash and an n, because \n in the replacement is undefined by the standard. Use a literal newline instead (typically by typing backslash, Ctrl-V, Ctrl-J). – Philippos Nov 14 '17 at 7:29

You can use the option -s from tr to squeeze repeated characters into one and replace it into a new line

tr -s " " "\n"

If gnu-grep available,

grep -Po '\S+'

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