Skip to content

Instantly share code, notes, and snippets.

@bdmorin
Created August 5, 2024 14:05
Show Gist options
  • Save bdmorin/81ea8ebd4cd07c2a1cb948848604c2ae to your computer and use it in GitHub Desktop.
Save bdmorin/81ea8ebd4cd07c2a1cb948848604c2ae to your computer and use it in GitHub Desktop.
fml - first middle last
#!/usr/bin/env python3
# cat life.txt | fml.py -n 10
import sys
import argparse
def process_stream(n):
lines = []
for line in sys.stdin:
lines.append(line.rstrip())
total_lines = len(lines)
if total_lines <= 3 * n:
for line in lines:
print(line)
else:
for i in range(n):
print(lines[i])
print("...")
middle_start = (total_lines - n) // 2
for i in range(middle_start, middle_start + n):
print(lines[i])
print("...")
for i in range(total_lines - n, total_lines):
print(lines[i])
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Print first, middle, and last N lines of input")
parser.add_argument("-n", type=int, default=10, help="Number of lines to print for each section")
args = parser.parse_args()
process_stream(args.n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment