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 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
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:

How to process a CSV file in Scala alvin June 19, 2015 - 11:06am

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 alvin June 19, 2015 - 11:03am

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:

Process every line in a file with this 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 alvin August 21, 2009 - 10:32am

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:

Perl 'split' function - how to process text data files

Perl FAQ: How can I split a string in Perl, such as the strings in a pipe-delimited text file?

Many times you need a Perl script that can open a plain text file, and essentially treat that file as a database. Typically these files have variable-length fields and records, and the fields in each record are delimited by some special character, usually a : or | character. When processing these files, you can use the Perl split function, which I’ll demonstrate in two short programs here.

How to append text to a file with Ruby

Ruby file FAQ: How do I append text to a file in Ruby?

Solution: Appending text to a file with Ruby is similar to other languages: you need to open the file in "append" mode, write your data, and then close the file.

Here's a quick example that demonstrates how to append "Hello, world" to a file named myfile.out in the current directory: