0

I have many text files prepended with digits like this:

12 some text here
some text here
some text here

Or sometimes like this:

123 text here

some more not-so-interesting text here

some text here

even more not-so-interesting text here

And I need them to appear like so:

12

some text here
some text here
some text here

Is this possible using sed or awk or some command line tool? I just need the digits to be on a new line, isolated from the other text on the line.

2

Just remember the number and replace the space after it with a newline:

sed 's/^\([0-9][0-9]*\) /\1\n/'

If your sed supports it, you can use an extended regex to improve readability:

sed -E 's/^([0-9]+) /\1\n/'
  • [0-9] matches a digit
  • * means "zero or more times"
  • + means "at least once"
  • \(...\) or (...) create a "capture group", the first capture group can be referenced as \1, etc.
  • \n represents a newline

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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