Insert a custom string after every n-th line of a text file. The tool always writes to a new .txt file (never overwrites by default). If the target name exists, it appends a numeric suffix (e.g., .1, .2, …).
- Insert any string (
--sep) after every n-th line (--nth) - Optional trailing insert at EOF (
--include-trailing) - Safe output: new file is created automatically (e.g.,
input.interleaved.txt; oroutput.txt,output.1.txt, …) - Optional
--stdoutto also print the result
Requires Python 3.8+.
chmod +x interleave.py./interleave.py INPUT_FILE [--sep "STRING"] [--nth N] [--include-trailing] [--outfile OUTPUT_FILE] [--stdout]- INPUT_FILE — Source .txt
- --sep — String to insert (default: ---)
- --nth — Insert after every n-th line (default: 1)
- --include-trailing — Also insert at EOF if the last group ends exactly on a multiple of n
- --outfile — Desired output file path (a new file is created; numeric suffix added if it exists)
- --stdout — Also print the transformed text to stdout
Every 2nd line, default output name:
./interleave.py input.txt --sep " | " --nth 2
# -> writes input.interleaved.txt (or input.interleaved.1.txt if needed)Every 3rd line with trailing insert, custom filename:
./interleave.py input.txt --sep "***" --nth 3 --include-trailing --outfile chunked.txt
# -> writes chunked.txt (or chunked.1.txt if needed)Also print to terminal:
./interleave.py input.txt --sep "<SEP>" --nth 5 --stdout