Splitting Lines and Numbering the Pieces
Thinking about how each tool you use works together.
Join the DZone community and get the full member experience.Join For Free
As I mentioned in my computational survivalist post, I’m working on a project where I have a dedicated computer with little more than basic Unix tools, ported to Windows. It’s given me a new appreciation for how the standard Unix tools fit together; I’ve had to rely on them for tasks I’d usually do a different way.
I’d seen the
nl command before for numbering lines, but I thought, “Why would you ever want to do that? If you want to see line numbers, use your editor.” That way of thinking looks at the tools one at a time, asking what each can do, rather than thinking about how they might work together.
Today, for the first time ever, I wanted to number lines from the command line. I had a delimited text file and wanted to see a numbered list of the column headings. I’ve written before about how you can extract columns using cut, but you have to know the number of a column to select it. So, it would be nice to see a numbered list of column headings.
You may also like: Functions in Shell Script.
The data I’m working on is proprietary, so I downloaded a PUMS (Public Use Microdata Sample) file named,
ss04hak.csv from the US Census to use in this example instead. The first line of this file is
I want to grab the first line of this file, replace commas with newlines, and number the results. That’s what the following one-liner does.
head -n 1 ss04hak.csv | sed "s/,/\n/g" | nl
The output looks like this:
1 RT 2 SERIALNO 3 DIVISION 4 MSACMSA 5 PMSA ... 100 FWATP 101 FYBLP
Now, if I wanted to look at a particular field, I could see the column number without putting my finger on my screen and counting. Then, I could use that column number as an argument to
Published at DZone with permission of John Cook, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.