Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save 0mp/9fc86a16f634f44aadc4dae357d745e7 to your computer and use it in GitHub Desktop.
Save 0mp/9fc86a16f634f44aadc4dae357d745e7 to your computer and use it in GitHub Desktop.
Insert the first file into the second file after the first empty line with sed.
# basefile.txt - The file where we want to put the content of the
# injectedfile.txt.
# injectedfile.txt - The file we want to inject into the basefile.txt.
# -n disables automatic line printing.
sed -n \
# Set up a label to jump to.
# It's called 'head' because we use it to print lines before the first empty
# line.
-e ':head' \
# Print the line and if the current line is not empty load the next one and
# jump to 'head'.
-e 'p;/./n;/./b head' \
# We get here if we didn't jump to head (the current line is empty). Print
# the empty line and inject the content of injectedfile.txt.
-e 'p;r injectedfile.txt' \
# Set up a label called 'tail' to handle the remaining lines of basefile.txt.
-e ':tail' \
# Load the next line, print it and jump to 'tail'.
-e 'n;p;b tail' \
basefile.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment