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.
catworks well for small files or for combining files and printing content directly.lessis better for larger files because it loads content as needed and supports paging and searching.headandtaildisplay the beginning and end of a file. The-foption intailfollows 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:
-ito ignore case.-vto reverse the match and show non-matching lines.-nto include line numbers.-cto 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.
-dsets the field delimiter, often a tab by default.-fselects fields by number or range, such as1,3or1-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.
sortorders lines. Options include-nfor numeric order,-rfor reverse order, and-kfor sorting by a specific column.uniqremoves adjacent duplicate lines. It is often used withsortfor global deduplication. The-coption 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.