Skip to content

Instantly share code, notes, and snippets.

@arthurattwell
Last active November 24, 2021 11:52
Show Gist options
  • Save arthurattwell/55d168c6b584e901b3f9eaa80ca97063 to your computer and use it in GitHub Desktop.
Save arthurattwell/55d168c6b584e901b3f9eaa80ca97063 to your computer and use it in GitHub Desktop.

Revisions

  1. arthurattwell revised this gist Dec 8, 2018. 1 changed file with 5 additions and 7 deletions.
    12 changes: 5 additions & 7 deletions split.sh
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,16 @@
    #!/bin/bash
    # That tells Linux to use a Bourne shell interpreter.

    # On OSX, run this script from the current directory.
    if [[ "$OSTYPE" == "darwin"* ]]; then
    d -- "$(dirname "$0")"
    fi
    # Run this script from the current directory. (Required on OSX.)
    cd -- "$(dirname "$0")"

    # Don't echo these commands.
    set +v

    # Get the filename from the user.
    echo "Enter the file name to split: "
    read filename
    echo 'Splitting ' $filename ' ...'
    echo 'Splitting' $filename ' ...'

    # Replace the first of each set of YAML frontmatter
    # with a string delimiter we can replace later.
    @@ -21,7 +19,7 @@ echo 'Splitting ' $filename ' ...'
    perl -i -0pe 's/(^---$)(.+?)(^---$)/---thisisnotthreehyphens$2---/gms' $filename

    # Get the number of splits we're going to do.
    frontmatter=$(grep -c "^\-\-\-thisisnotthreehyphens$" t.md)
    frontmatter=$(grep -c "^\-\-\-thisisnotthreehyphens$" "$filename")
    echo 'Found' $frontmatter 'markdown docs.'

    # Set the number of times we'll repeat the csplit.
    @@ -49,4 +47,4 @@ for file in xx*
    done

    # All done!
    echo 'Done!'
    echo 'Done!'
  2. arthurattwell revised this gist Dec 8, 2018. 1 changed file with 16 additions and 6 deletions.
    22 changes: 16 additions & 6 deletions split.sh
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,10 @@
    #!/bin/bash
    # That tells Linux to use a Bourne shell interpreter.

    # Run this script from the current directory. (Required on OSX.)
    d -- "$(dirname "$0")"
    # On OSX, run this script from the current directory.
    if [[ "$OSTYPE" == "darwin"* ]]; then
    d -- "$(dirname "$0")"
    fi

    # Don't echo these commands.
    set +v
    @@ -18,10 +20,16 @@ echo 'Splitting ' $filename ' ...'
    # one line at a time.
    perl -i -0pe 's/(^---$)(.+?)(^---$)/---thisisnotthreehyphens$2---/gms' $filename

    # Get the number of splits we're going to do.
    frontmatter=$(grep -c "^\-\-\-thisisnotthreehyphens$" t.md)
    echo 'Found' $frontmatter 'markdown docs.'

    # Set the number of times we'll repeat the csplit.
    # This should be one less than the number of frontmatters.
    csplitRepeats=$(($frontmatter-1))

    # Split the file at the delimiter we just created.
    # OSX's csplit can't use {*}, so we use a real number 10000.
    # This means, technically, this will only split up to 10000 files.
    csplit -ks $filename '/---thisisnotthreehyphens/' {10000}
    csplit -ks $filename '/---thisisnotthreehyphens/' {$csplitRepeats}

    # In the original file and the files we just created,
    # remove the string we put there as a delimiter.
    @@ -35,7 +43,9 @@ rm xx00
    for file in xx*
    do
    perl -i -pe 's/---thisisnotthreehyphens/---/' $file
    mv "$file" "${file/xx/}.md"
    newFilename=${file/xx/}.md
    mv "$file" "$newFilename"
    echo 'Created' $newFilename
    done

    # All done!
  3. arthurattwell revised this gist Dec 8, 2018. 1 changed file with 16 additions and 8 deletions.
    24 changes: 16 additions & 8 deletions split.sh
    Original file line number Diff line number Diff line change
    @@ -1,33 +1,41 @@
    #!/bin/bash
    # That tells Linux to use a Bourne shell interpreter.
    # Don't echo these commands:

    # Run this script from the current directory. (Required on OSX.)
    d -- "$(dirname "$0")"

    # Don't echo these commands.
    set +v

    # Get the filename from the user
    # Get the filename from the user.
    echo "Enter the file name to split: "
    read filename
    echo 'Splitting ' $filename ' ...'

    # Replace the first of each set of YAML frontmatter
    # with a string delimiter we can replace later.
    # This is necessary because csplit can only search
    # one line at a time.
    perl -i -0pe 's/(^---$)(.+?)(^---$)/---thisisnotthreehyphens$2---/gms' $filename

    # Split the file at the delimiter we just created
    csplit -ks $filename '/---thisisnotthreehyphens/' {*}
    # Split the file at the delimiter we just created.
    # OSX's csplit can't use {*}, so we use a real number 10000.
    # This means, technically, this will only split up to 10000 files.
    csplit -ks $filename '/---thisisnotthreehyphens/' {10000}

    # In the original file and the files we just created,
    # remove the string we put there as a delimiter.
    perl -i -pe 's/---thisisnotthreehyphens/---/' $filename

    # Remove the first, blank file (we assume there is
    # nothing before our first --- that we want to keep)
    # Remove the first, blank file. (We assume there is
    # nothing before our first '---' that we want to keep.)
    rm xx00

    # Put back the --- in the files we created, and rename them
    # Put back the '---' in the files we created, and rename them.
    for file in xx*
    do
    perl -i -pe 's/---thisisnotthreehyphens/---/' $file
    rename -f 's/xx(\d+)/$1.md/' $file
    mv "$file" "${file/xx/}.md"
    done

    # All done!
  4. arthurattwell created this gist Nov 16, 2018.
    34 changes: 34 additions & 0 deletions split.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    #!/bin/bash
    # That tells Linux to use a Bourne shell interpreter.
    # Don't echo these commands:
    set +v

    # Get the filename from the user
    echo "Enter the file name to split: "
    read filename

    # Replace the first of each set of YAML frontmatter
    # with a string delimiter we can replace later.
    # This is necessary because csplit can only search
    # one line at a time.
    perl -i -0pe 's/(^---$)(.+?)(^---$)/---thisisnotthreehyphens$2---/gms' $filename

    # Split the file at the delimiter we just created
    csplit -ks $filename '/---thisisnotthreehyphens/' {*}

    # In the original file and the files we just created,
    perl -i -pe 's/---thisisnotthreehyphens/---/' $filename

    # Remove the first, blank file (we assume there is
    # nothing before our first --- that we want to keep)
    rm xx00

    # Put back the --- in the files we created, and rename them
    for file in xx*
    do
    perl -i -pe 's/---thisisnotthreehyphens/---/' $file
    rename -f 's/xx(\d+)/$1.md/' $file
    done

    # All done!
    echo 'Done!'