Merging files with pr
Tonight, I’ve been poring over a rather large data set that I want to get some useful information out of. All the data was originally stored in a .html file, but after some (very) crude extraction techniques, I managed to pull out just the data I wanted, and shove it into a comma-separated file. Earlier, I had given up on my tools at hand and typed up an entire list of row headings for my newly-gotten data. So I had two files like so:
headings.txt
Alpha
Bravo
Charlie
values.csv
1,2,3,4
5,6,7,8
9,10,11,12
I spent quite a bit of time trying to figure out how to combine the two columns into one file with what I knew, but none of my tools could quite do it without nasty shell scripting. It took me a while, but I eventually found this post that cracked the case for me. The pr command, ostensibly for paging documents, has enough horsepower to solve my problem in short order, like so:
$ pr -tm -s, headings.txt values.csv
The -t tells the program to omit headers and footers, and -m tells it to merge each line. The -s, tells it to use commas as field-separators. My desired result, like so:
headings.txt
Alpha,1,2,3,4
Bravo,5,6,7,8
Charlie,9,10,11,12
There are numerous other options to pr, and depending on your potential line lengths, one may have to experiment. But for me, this got the job done.
External Links




