Tag Archives: cut

truncate: Arbitrarily chopping things off

When I mentioned that there were useful and interesting tools in coreutils and util-linux and bsd-games (and I should probably add binutils), I wasn’t exactly thinking of truncate.

2014-09-16-6m47421-truncate

truncate wasn’t on my list when it began five years ago, or even in later additions. I can see why: It’s a rather arbitrary and vicious tool, snapping off files at predetermined lengths and leaving the remainder to flutter away in the wind.

I can’t think of any exact use for truncate aside from determining an exact, to-the-byte length of a file, perhaps for some sort of network testing or disk performance check. And considering the leftovers are summarily discarded, it’s a lethal decision to use it.

truncate follows the same flags for size and units as split and some other toys from coreutils. If you’re familiar with much of what’s in the suite, it will only take you a second to get used to truncate.

And that’s about all I can think of to say about truncate. Use wisely. πŸ˜‰

csvkit: Lots of little tools, in a pack

I don’t work with csv files much, but I hear they are popular among those who use databases.

I have run through one or two, for example when converting from sc format to csv, for access through conventional graphical spreadsheets. That requires a little search-and-replace acrobatics, as you might expect. To that end, csvkit has some nifty tools in it.

2013-09-27-v5-122p-csvkit

csvclean can touch up small errors in a csv file. csvlook displays the contents in a table format, drawn with a header and ASCII lines.

csvstat can give you a breakdown of each column in a csv file, and there are csvcut, csvgrep, csvsort, csvjoin … among still others.

It seems that most of the tools have some unified flags and options; for example, the flag to declare a different delimiter in csvlook is the save in csvstat. Uniformity: That’s a good idea.

There are enough tools in here to keep the maddest scientist busy for a while, and I doubt it would take long to adopt them. If you work with csv files a lot, this might be the tool you’ve been waiting for. πŸ˜‰

cut: Exactly what it says

I will only take a few moments to show what cut can do. Another gem from the coreutils package, cut … well, cut … cuts apart text. πŸ™„

So here’s an idea for you: Let’s imagine that you’re a bit obsessive like me, and you want to inject the file creation date of a video file into its filename.

Yeah, I know. It’s odd. 😳

I can extract that information with mediainfo. I can filter out the “Encoded date” with grep, but I still have a line of data that looks like this.

Encoded date                             : UTC 2013-09-22 04:32:37

Now I need to carve out the date and time so they can be pushed into the file name. That’s where cut comes in.

With cut, I can select specific character counts, certain delimiters, or just about anything else, and get back the string of data I want. So …

cut -c48-

The -c flag means cut at characters. The 48 means the 48th character in line. And the final hyphen means continue to the end of the stream. And what do I get in reply?

2013-09-22 04:32:37

Nice and pretty.

That’s just one oddball example; there are pages and pages around the Internet that can coach you through cut. Not to play favorites, but here’s one that might help you get started.

Enjoy. πŸ˜‰

P.S.: Why such an obtuse example? Because I just needed cut for that very thing, only three days before I wrote this post. … πŸ™„