Text Management Commands

Managing text from the terminal is less about memorizing every flag and more about building reliable habits. GUI editors are convenient, but command-line tools remain essential for reading logs, changing configuration files, cleaning data, and answering quick questions about file contents.

When files grow large, opening them in a full editor can become slow or impractical. The terminal offers lightweight utilities that handle inspection, search, extraction, sorting, and editing without loading everything into a graphical interface.

Inspecting File Contents

Several basic commands help you view text files.

  • cat works well for small files or for combining files and printing content directly.
  • less is better for larger files because it loads content as needed and supports paging and searching.
  • head and tail display the beginning and end of a file. The -f option in tail follows a file, which is useful for logs.

Example: view the last 20 lines of a system log and keep the file open for new entries:

tail -n 20 -f /var/log/syslog

Searching and Filtering Text with grep

grep is one of the most widely used text-processing commands. It searches for lines that match a pattern.

Common options include:

  • -i to ignore case.
  • -v to reverse the match and show non-matching lines.
  • -n to include line numbers.
  • -c to count matching lines.

Example: find lines containing ERROR and show their line numbers:

grep -n "ERROR" app.log

Extracting Specific Fields with cut

When text is structured, such as CSV files or whitespace-delimited logs, field extraction becomes important.

The cut command can extract selected fields.

  • -d sets the field delimiter, often a tab by default.
  • -f selects fields by number or range, such as 1,3 or 1-3.

Example: from /etc/passwd, extract usernames and shells. The file uses : as a delimiter, with the username in field 1 and shell in field 7.

cut -d: -f1,7 /etc/passwd

Sorting, Counting, and Finding Duplicates

Data preparation often starts with sorting and deduplication.

  • sort orders lines. Options include -n for numeric order, -r for reverse order, and -k for sorting by a specific column.
  • uniq removes adjacent duplicate lines. It is often used with sort for global deduplication. The -c option counts repeated occurrences.

Example: count the most frequent IP addresses in an access log. The first field is assumed to be the IP address:

awk '{print $1}' access.log | sort | uniq -c | sort -nr

The pipe character | passes the output of one command as the input for the next.

Stream Editing with sed

sed is a stream editor used for automated text changes. It can handle insert, delete, replace, and search operations through a command language.

A common substitution uses the s command, the old string, the new string, and the g flag for global replacement.

sed 's/old_string/new_string/g' filename

Example: replace localhost with 127.0.0.1 in a configuration file:

sed -i 's/localhost/127.0.0.1/g' config.conf

The -i option edits the file in place. Without it, sed prints the result to standard output.

Building Practical Text Workflows

The real power of these tools comes from combining them. A typical workflow might involve searching logs, filtering results, extracting fields, sorting, and replacing values.

For example, you might search for errors, remove irrelevant lines, extract timestamps, and count patterns. Each command can be chained through pipelines, making text processing fast and repeatable.

Final Thoughts

Learning a small set of command-line text tools is a practical investment. Commands such as cat, less, head, and tail help you inspect files. grep finds patterns. cut, sort, and uniq help structure data. sed edits streams. Together, they support efficient log analysis, configuration work, and data cleaning.

The key is not just knowing individual commands. It is learning how to combine them into focused workflows. Start with simple examples, then expand into regular expressions and advanced options. Over time, command-line text management becomes a natural part of daily system administration and development.