add a body command
- ID
4de7868- date
2023-07-17 01:24:26+00:00- author
Alex Chan <alex@alexwlchan.net>- parent
37e95be- message
add a `body` command- changed files
2 files, 46 additions
Changed files
text/README.md (2301) → text/README.md (2596)
diff --git a/text/README.md b/text/README.md
index 376ee87..bc76a92 100644
--- a/text/README.md
+++ b/text/README.md
@@ -6,6 +6,17 @@ These are utilities for manipulating streams of text; I consider them in a simil
<dl>
<dt>
+ <a href="https://github.com/alexwlchan/scripts/blob/main/text/body">
+ <code>body -n [LINENO] [PATH]</code>
+ </a>
+ </dt>
+ <dd>
+ print the nth line of a file.
+ This is meant to fill a gap between the Unix utilities <code>head</code> and <code>tail</code>.
+ </dd>
+
+
+ <dt>
<a href="https://github.com/alexwlchan/scripts/blob/main/text/midline">
<code>midline [PATH]</code>
</a>
text/body (0) → text/body (832)
diff --git a/text/body b/text/body
new file mode 100755
index 0000000..f5d46f2
--- /dev/null
+++ b/text/body
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3
+"""
+Prints the nth line of a file.
+
+This is meant to fill a gap between the Unix utilities ``head`` and
+``tail``, which I normally use in one of the following forms:
+
+ $ head -n 5 myfile.txt
+ $ tail -n 5 myfile.txt
+
+which print the first/last 5 lines of a file, respectively.
+
+This command fills the gap -- it gets the nth line of a file, e.g. this
+prints the 100th line of the file:
+
+ $ body -n 100 myfile.txt
+
+"""
+
+import argparse
+import subprocess
+
+
+if __name__ == "__main__":
+ parser = argparse.ArgumentParser()
+
+ parser.add_argument("-n", required=True, dest="lineno", type=int)
+ parser.add_argument("PATH")
+
+ args = parser.parse_args()
+
+ for lineno, line in enumerate(open(args.PATH), start=1):
+ if lineno == args.lineno:
+ print(line, end="")
+ break