Perl Guru's, I need you!
May 11, 2006 8:11 AM   RSS feed for this thread Subscribe

I need, post haste, a perl or shell script that will loop through a directory and combine files. The files are named like this: 212_Class001, 212_Class002, 212_Class0** There are some gaps in the sequence. In retrospect it was a bad idea to store class notes in individual files. I'd like to combine them in the sequence they are numbered. Can someone please help ‽
posted by Grod to education (10 comments total)
ls | sort | xargs cat > result-file-name.txt

in the directory you've got your notes in seems like it ought to work. This is assuming they're stored on a computer that's sufficiently unixy.
posted by jacobm at 8:18 AM on May 11, 2006


at a shell prompt, all you need to do is:


cat 212_Class0?? > 212_Class_Notes


That will concatenate all 212_Class0* files, in order, into 212_Class_Notes
posted by aerosolkid at 8:20 AM on May 11, 2006


Damn, I forgot to say, there are other files in there, too, like 235_Class0** and 206_Class0** and *.pdf and so on, I want to isolate the 212 notes. And the system is GNU/Linux, so that should work.
posted by Grod at 8:21 AM on May 11, 2006


aerosolkid, perfect thank you! one of these days I really need to sit down and learn how to use all those powerful little tools.
posted by Grod at 8:22 AM on May 11, 2006


aerosolkid's solution will work too --- or, between the ls and the sort in my pipeline, stick in grep '212_Class':

ls | grep '212_Class' | sort | xargs cat > outputfile.txt
posted by jacobm at 8:25 AM on May 11, 2006


Cool, what if I wanted to insert two line breaks between the end of one file and the beginning of another?
posted by Grod at 8:31 AM on May 11, 2006


Great, thanks! I'm off to study for twenty minutes and then fail an exam. I hope I don't abuse the apostrophe like I did in the title of this post, Matt, could you maybe fix that for me, it's embarrassing.
posted by Grod at 8:36 AM on May 11, 2006


I would just go to a full-blown for loop (this is bash-specific now, by the way) and do:

for f in `ls | grep '212_Class' | sort`; do cat $f; echo ""; done

That echo "" will get inserted after every file, so you can change it to whatever you want.
posted by jacobm at 8:43 AM on May 11, 2006


"ls | grep" makes no sense. Why not just use the right glob wildcard? And the wildcard expansion will be sorted already.

for F in 212_Class0??; do cat $F; echo; done
posted by Rhomboid at 11:21 AM on May 11, 2006


Good point, Rhomboid. I think this just reflects my personal tastes in these things --- I tend to always use grep rather than wildcards, because it's more powerful in general, and I like to make my sorts explicit rather than relying on the details of the expansion. (I tend to always want things very explicit --- c.f. echo "" rather than echo.)
posted by jacobm at 11:39 AM on May 11, 2006


« Older What is the best way to go abo...   |   "Managing" the devel... Newer »
This thread is closed to new comments.


Related Questions
Computer languages considered in linguistic contexts? July 27, 2008
Dance, text files, dance April 12, 2007
No more shell games! May 31, 2006
Perl Problems March 15, 2004
Learning Perl, PHP, MySQL December 17, 2003