sed - kdaisho/Blog GitHub Wiki

This line fetch a page from Moby Dick (license free), then replace (substitute) whitespace with a newline globally

For GNU/Linux

curl -s https://www.gutenberg.org/cache/epub/2701/pg2701.txt | sed -r 's/\s+/\n/g'
Flags
  • -s: silently
  • -r: interpret regular expressions as extended
  • g: globally
Explanation of s/\s+/\n/g
  • First Slash (/): Starts the pattern (what you want to search for).
  • \s+: The pattern itself, which matches one or more whitespace characters.
    • \s: Matches any whitespace character (spaces, tabs, newlines, etc.).
    • +: Matches one or more occurrences of the preceding element \s.
  • Second Slash /: Ends the pattern and starts the replacement part.
  • Replacement \n: Specifies what to replace the pattern with (a newline character, in this case).
  • Third Slash /: Ends the replacement part.

Disclaimer: Above command works on GNU/Linux, not on BSD (macOS) 😞

For BSD

curl -s https://www.gutenberg.org/cache/epub/2701/pg2701.txt | sed -E 's/[:space:](/kdaisho/Blog/wiki/:space:)+/\'$'\n''/g' | grep -i whale | wc -l
Flags
  • -E: interpret regular expressions as extended, same as -r with the command for GNU
  • -l: (word count by) lines