25 Terminal Tips Every Mac User Should Know
Posted 12/11/2008 at 11:40am
| by Johnathon Williams

If you like Spotlight, you’ll love grep, an old-school pattern-matching utility. Like Spotlight, grep searches the full contents of files. Unlike Spotlight, however, grep specializes in locating patterns, which makes it ideal for analyzing text documents. Meanwhile, diff, fmt, and textutil offer other ways to quickly compare, format, and manage text files.
18. Find Patterns In Text Docs
One of our favorite uses of grep is checking finished documents for words and phrases we use too frequently. For instance, we tend to overuse compound sentences joined by “but.” The following command tells us how many lines in article.txt contain the offending word: grep -ic but article.txt.
n this example, the i option tells grep to ignore case (counting both uppercase and lowercase instances), and the c option tells it to return only the number of matches, not all of the lines in which the search term appears. For details, execute man grep.
19. Compare The Differences Between Two Text Files

Management--there’s another word we use way too often.
Here’s one for writers and office workers who deal with a lot of document revisions. The next time you need to quickly compare the differences between two text files, execute diff -y name-of-first-file name-of-second-file.
The -y option tells diff to split the output into two columns, one for each file, so the differences can be seen more easily.
20. Combine And Normalize Text Files

We could combine and convert all these documents by hand, or we could execute a single command in the Terminal.
If you’ve ever been involved in a large research project, you know how cutting and pasting from lots of different sources can produce a Frankenstein-like collection of documents, with different line lengths and inconsistent spacing. Next time, combine and clean those docs with this command: fmt -sp filneme1 filename2 filename 3 > name-of-new-file.txt.
The command will force line lengths of 65 characters, and normalize tabs and spacing.
21. Combine And Convert Documents of All Type
Not everyone wants or needs a copy of Microsoft Word. Unfortunately, we all sometimes need to work with Word documents. Textutil can convert between Word, rich-text, and plain-text formats--and it can combine multiple documents, change fonts, and adjust font size while doing it. To convert and combine all Word documents in the current directory to a single rich-text document called combined.rtf, execute textutil -cat rtf -output combined.rtf *.doc.
Sometimes, of course, you only need to convert a single file. Use this command to do so while changing the font to 12pt Helvetica textutil -cat rtf -font Helvetica -fontsize 10 -output converted-file.rtf file-to-convert.doc.
Next OS X Tweaks