textutil - macOS


Overview

The textutil command in macOS is a versatile tool that enables users to manipulate and transform text content in various ways. It offers functionalities for encoding, decoding, filtering, and modifying text data from the command line.

Syntax

textutil [options] [command] [arguments]

Options/Flags

  • -h, –help: Display help information.
  • -v, –verbose: Enable verbose output.
  • -s, –stdin: Read input from standard input instead of a file.
  • -o, –outputFile: Specify the output file path.
  • -e, –encoding: Set the input/output text encoding.
  • -f, –format: Set the output text format.
  • -l, –line-length: Set the maximum line length for wrapping.

Commands

| Command | Description |
|—|—|
| encode | Encode text using specified encoding. |
| decode | Decode text using specified encoding. |
| filter | Filter text using specified criteria. |
| replace | Replace text patterns with given replacements. |
| strip | Strip HTML or RTF tags from text. |
| analyze | Analyze text statistics (e.g., word count, sentence count). |

Examples

Encode text as UTF-8:

textutil encode -e utf-8 input.txt

Decode text from Base64:

textutil decode -e base64 input.txt

Filter text for specific words:

textutil filter -s "macOS textutil" | wc -l

Replace every occurrence of “word” with “replacement”:

textutil replace -f word replacement input.txt

Strip HTML tags from text:

textutil strip -f html input.html

Common Issues

  • Incorrect encoding: Ensure the correct encoding is used for input/output to avoid character corruption.
  • Invalid command syntax: Refer to the syntax section for proper usage.
  • Insufficient permissions: The output file may require write permissions.

Integration

  • Pipe the output of textutil to other commands for further processing (e.g., grep, cut).
  • Use textutil in conjunction with scripting languages to automate text manipulation tasks (e.g., AppleScript, shell scripts).
  • grep
  • sed
  • awk