Creating an index of first lines
December 8, 2012 11:35 AM

Creating an index of first lines, from a group of many poems, each stored in a seperate .txt file. Are there any applications or scripts that would help me to do that?

Hello,

I've got about a hundred poems that I'm doing some research on and organization with, and I thought it would be nice to include an index of first lines (particularly because the authors titles changed over time a little bit)

Are there any applications or scripts that would help me to do that?
posted by dylan_k to Writing & Language (7 answers total) 2 users marked this as a favorite
In unix you can do this by doing:

head -n 1 [filenames here]

Assuming the poems start on the first line of each file.

This is the perfect task for unix command line stuff.
posted by delicious-luncheon at 11:47 AM on December 8, 2012


If all the .txt files are in one directory, a shell script something along these lines would do it:
#!/bin/sh
for i in *.txt
do
   head -n 1 $i >> firstlines.index
done
That'll work on anything with a Unix command line, which includes the OS X terminal. If you want to include the filename in the index file, so that you can map the first line to the title, then you could use echo to add it alongside the head command.
posted by holgate at 11:53 AM on December 8, 2012


Or in Python:

import os
with open("c:/.../firstlines.txt", "w") as firstlines:
    for path in os.listdir("c:/.../"):
        firstlines.write(open(path).readline())

It's also easy to tell it to look through multiple directories, if you want it to.
posted by katrielalex at 11:57 AM on December 8, 2012


The poems all start on the fourth line. the first line is the title, followed by two empty lines.
posted by dylan_k at 12:54 PM on December 8, 2012


In that case, you could use sed -n '4p' instead of head -n 1. Here's a list of sed one-liners.

(Or awk, which is another general-purpose file-munger.)
posted by holgate at 1:03 PM on December 8, 2012


Editing holgate's solution to just grab the fourth line:


#!/bin/sh
for i in *.txt
do
head -n4 $i | tail -n1 >> firstlines.index
done

posted by primer_dimer at 3:50 AM on December 10, 2012


Thanks for the help everyone!
Do Sed or Awk have any GUI tools, or are they only for command line use?
posted by dylan_k at 8:52 AM on December 15, 2012


« Older Is there a tool like the Dremel that I can buy as...   |   Three cats, two sisters and one stray, one new... Newer »
This thread is closed to new comments.