text 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 macOS 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

How to use ‘awk’ to print columns from a text file (in any order)

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:

How to process a CSV file in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.5, “How to process a CSV file in Scala.”

Problem

You want to process the lines in a CSV file, either handling one line at a time or storing them in a two-dimensional array.

Solution

Combine Recipe 12.1, “How to Open and Read a Text File” with Recipe 1.3, “Splitting Strings”. Given a simple CSV file like this named finance.csv:

How to process every character in a text file in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.4, “How to process every character in a text file in Scala.”

Problem

You want to open a text file and process every character in the file.

Solution

If performance isn’t a concern, write your code in a straightforward, obvious way:

How to process every line in a file with a Unix/Linux shell script

Unix/Linux shell script FAQ: How do I write a Unix or Linux shell script where I "do something" for every line in a text file?

Solution: An easy way to process every line in a text file is to use a Unix/Linux while loop in combination with the Linux cat command, like this:

How to save the output from a MySQL query to a file

I had a problem with this website last Wednesday, and as part of troubleshooting the problem, I need to look at one of the Drupal database tables, specifically the watchdog table. This table had thousands of records in it, and I couldn't find what I needed with SQL SELECT queries, so I finally dug through the MySQL documentation, and found that I could save the output from a SELECT query to a text file.

In this tutorial I'll show you both (a) how to save the results of a MySQL query to a text file, and also (b) how to log your entire MySQL session to a text file.

Perl CSV file column extraction

Perl CSV column data extraction FAQ: Can you share an example of how to extract one or more columns from a Perl CSV file or other similarly-formatted flat text file?

Perl is a terrific language for text processing, but several readers have written wondering about how to extract columns of data from text files with Perl. For instance, when you have a text database that looks like this:

A Ruby script to remove binary (garbage) characters from a text file

Problem: You have a file that should be a plain text file, but for some reason it has a bunch of non-printable binary characters (also known as garbage characters) in it, and you'd like a Ruby script that can create a clean version of the file.

Solution: I've demonstrated how to do this in another blog post by using the Unix tr command, but in case you'd like a Ruby script to clean up a file like this, I thought I'd write up a quick program and share it here.