file

How to copy text from the MacOS Terminal to the clipboard

If you ever need to copy text (or a text file) from the MacOS Terminal to the Mac clipboard, I can confirm that the Mac pbcopy command works. It reads from STDIN and copies the text to the clipboard, so commands like these work:

$ echo "foo bar baz" | pbcopy

$ cat /etc/passwd | pbcopy
Scala code to read a text file to an Array (or Seq) alvin January 17, 2017 - 5:04pm

As a quick note, I use code like this read a text file into an Array, List, or Seq using Scala:

def readFile(filename: String): Seq[String] = {
    val bufferedSource = io.Source.fromFile(filename)
    val lines = (for (line <- bufferedSource.getLines()) yield line).toList
    bufferedSource.close
    lines
}
How to use ‘awk’ to print columns from a text file (in any order) alvin August 29, 2016 - 11:14pm

Printing columns of information from Unix text files is easy, especially using commands like awk, perl, and more recently, ruby. This short tutorial shows my old-school awk way of doing this.

awk column printing examples

Suppose you have a file named foo with contents like this:

1 2 3
a b c

You can easily use awk to print columns of information from this file. Here are a few examples that show how to print the data columns from the file:

What is the Unix/Linux “bit bucket”? alvin July 21, 2016 - 10:44am

Unix/Linux FAQ: What is the “bit bucket”?

The bit bucket is a way of referring /dev/null. Sending output to the /dev/null device file is like sending output directly to the trash. That’s why you see code like this a lot of times:

aCommand 2> /dev/null

That’s a way of saying, “Run the command aCommand and send it’s error output to the bit bucket.” In use like this, “error output” refers to STDERR, and redirecting STDERR to the bit bucket is the same as throwing it into the trash (or throwing it into a black hole, if you prefer).

Another way you can demonstrate this is by sending STDOUT to the bit bucket. In this next example, I send the output from the ls command to the bit bucket:

ls -l > /dev/null

If you run that command, you won’t see any output because the standard output — STDOUT — is redirected to /dev/null. There’s no practical reason for doing this in the real world; I just wanted to demonstrate that you can redirect both STDOUT and STDERR to the bit bucket, if you ever need to.

For more information, here’s a short tutorial on How to redirect Unix STDOUT and STDERR to the same location.

Sorting ‘ls’ command output by filesize alvin July 17, 2016 - 1:01pm

I just noticed that some of the MySQL files on this website had grown very large, so I wanted to be able to list all of the files in the MySQL data directory and sort them by filesize, with the largest files shown at the end of the listing. This ls command did the trick, resulting in the output shown in the image:

ls -Slhr

The -S option is the key, telling the ls command to sort the file listing by size. The -h option tells ls to make the output human readable, and -r tells it to reverse the output, so in this case the largest files are shown at the end of the output.

Applescript: How to open a PDF in Preview and go directly to a page alvin July 7, 2016 - 7:43pm

I’m currently generating my new book on “functional programming in Scala” as a PDF using a combination of Pandoc and LaTeX, and as a result it feels like I’m opening the same PDF file about 100 times a day.

vim: How to go to the last position when reopening a file alvin July 4, 2016 - 5:54pm

vim tip: If you reopen a file with vim and want to go to your last position in that file, type '"

(That’s an apostrophe followed by a double-quote.)

Perl “file test” characters (for if/then tests) alvin June 28, 2016 - 4:18pm

This image comes from this Perl.org web page, and shows the Perl “file test” characters. As I show on this page, an if file test looks like this in Perl:

A Perl script to delete binary files alvin June 28, 2016 - 10:24am

As a quick note and a little bit of source code sharing, I wrote the following Perl script to delete all of the binary files it finds in a list of files it’s given. I named this script deleteBinaryFiles.pl, and it should be called like this:

deleteBinaryFiles.pl listOfFilesToLookAt

where listOfFilesToLookAt is a file that contains a list of filenames, with one filename per line.

Given that brief introduction, here’s the source code: