Stop Reading Text Files Manually: 7 Linux Tricks to Find What Matters Faster
In the first article, 10 Linux Commands Every Power User Should Know, I explored essential commands for searching files, managing processes, monitoring logs, transferring data, and controlling permissions.
In the second article, 10 Linux Techniques That Separate Beginners From Power Users, I moved beyond individual commands to explain how pipes, redirection, command substitution, conditional execution, and background processes make the command line more powerful.
In the third article, 10 Bash Time-Saver One-Liners That Turn Naive Users into Pro Users, I demonstrated how Bash can automate repetitive tasks involving files, directories, filenames, and text.
Now, it’s time to focus on a specific skill that connects all three topics: text processing.
Imagine you have a server log containing thousands of lines, a configuration file with hundreds of settings, or a text file containing records you need to analyze.
You want to find specific entries, extract a few columns, replace text, remove duplicates, or calculate a total.
Opening the file and processing everything manually is rarely the most efficient approach.
Linux provides tools that let you perform these operations directly from the terminal, often with just one command.
In this article, we’ll explore seven useful text-processing tools:
grepcutawksedsortuniqwc
Let’s get started.
1. grep: Find Exactly What You're Looking For
You’ve probably used grep to search for text. But searching becomes much more useful when you understand how to apply it to real-world problems.
Imagine a file named server.log containing:
2026-10-10 09:12:03 INFO Server started
2026-10-10 09:12:08 ERROR Connection failed
2026-10-10 09:12:12 INFO Request received
2026-10-10 09:12:19 ERROR Authentication failed
2026-10-10 09:12:25 WARNING Disk usage highTo find errors:
grep "ERROR" server.logTo search without worrying about uppercase or lowercase:
grep -i "error" server.logTo display line numbers:
grep -n "ERROR" server.logTo find lines that do not contain a particular pattern:
grep -v "INFO" server.logYou can also search using regular expressions.
For example, to find lines beginning with a date:
grep '^[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}' server.logThe ^ anchors the pattern to the beginning of a line.
When to use it: Searching logs, locating configuration settings, filtering records, and finding patterns in large text files.
2. cut: Extract Only the Columns You Need
Suppose you have a comma-separated file named employees.csv:
name,department,salary
Alice,Engineering,75000
Bob,Marketing,62000
Charlie,Engineering,81000
Diana,Finance,70000You want only the employee names.
Use:
cut -d',' -f1 employees.csvHere:
-d','specifies the comma delimiter.-f1selects the first field.
To extract names and salaries:
cut -d',' -f1,3 employees.csvTo extract the second field:
cut -d',' -f2 employees.csvYou can also extract character positions rather than delimited fields:
cut -c1-10 file.txtThis displays the first ten characters of each line.
An important limitation
cut works well with simple, consistently delimited text. It is not a full CSV parser.
For example, consider:
name,description,salary
Alice,"Engineering, Research",75000Because the description contains a comma inside quotation marks, cut -d',' will not correctly interpret the CSV fields.
For complex CSV files, use a proper CSV parser.
When to use it: Extracting fields from simple CSV files, tab-separated tables, and consistently structured text.
3. awk: Filter Records, Extract Fields, and Perform Calculations
If you learn only one tool specifically for structured text processing, consider learning awk.
It treats each line as a record and, by default, splits that line into whitespace-separated fields.
Consider this file:
Alice Engineering 75000
Bob Marketing 62000
Charlie Engineering 81000
Diana Finance 70000Save it as employees.txt.
To print only the first column:
awk '{print $1}' employees.txtOutput:
Alice
Bob
Charlie
DianaTo print names and salaries:
awk '{print $1, $3}' employees.txtNow suppose you want only employees earning more than 70,000:
awk '$3 > 70000' employees.txtYou can calculate the average salary:
awk '{sum += $3; count++} END {if (count) print sum/count}' employees.txtThis works in two stages:
- For every line, add the third field to
sumand incrementcount. - After processing the file, calculate the average.
You can also calculate the total:
awk '{sum += $3} END {print sum}' employees.txtWhy awk matters
You can use it to filter records, extract columns, count entries, calculate totals, and generate summaries.
For small, structured-text tasks, it can eliminate the need to write a separate script.
When to use it: Working with tables, processing delimited records, filtering rows by conditions, and performing calculations directly in the terminal.
4. sed: Replace Text Without Opening a File
Suppose a configuration file contains:
server=old-server
port=8080
environment=productionYou want to replace old-server with new-server.
Get Raja ishaq Nabi Khan’s stories in your inbox
Join Medium for free to get updates from this writer.
Use:
sed 's/old-server/new-server/g' config.txtThis prints the modified text to the terminal. It does not change the original file.
The substitution syntax is:
s/old-text/new-text/gThe final g means that all non-overlapping matches on each line are replaced, rather than only the first match.
To modify the file directly:
sed -i.bak 's/old-server/new-server/g' config.txtOn GNU/Linux, this changes the file and creates a backup named config.txt.bak.
To remove blank lines from the displayed output:
sed '/^[[:space:]]*$/d' config.txtTo print only lines 10 through 20:
sed -n '10,20p' config.txtA word of caution
Commands using sed -i modify files directly. Test the transformation on a copy or create a backup before applying it to important configuration files.
When to use it: Replacing text, removing unwanted lines, and applying simple transformations to one or more text files.
5. sort: Put Records in the Order You Need
Sorting is useful when you want to organize data, identify repeated entries, or prepare records for further processing.
Suppose numbers.txt contains:
100
20
9
50Run:
sort numbers.txtThe default is lexicographic sorting, not numerical sorting. Therefore, the result may not be the numerical order you expect.
For numerical sorting:
sort -n numbers.txtOutput:
9
20
50
100For reverse numerical order:
sort -nr numbers.txtFor a tab-separated file, you can sort by the second column:
sort -t $'\t' -k2,2 data.tsvHere:
-tspecifies the delimiter.-k2,2selects the second field as the sort key.
To sort that field numerically in descending order:
sort -t $'\t' -k2,2nr data.tsvWhen to use it: Ordering records, sorting by columns, preparing data for duplicate counting, and identifying the largest or smallest values.
6. uniq: Identify Repeated Entries
Suppose you have a file containing:
apple
banana
apple
orange
banana
appleYou might expect:
uniq fruits.txtto remove every duplicate.
It doesn’t.
uniq compares adjacent lines only. In the example above, the repeated entries are separated, so they will not all be collapsed.
To identify unique entries regardless of their original order, sort first:
sort fruits.txt | uniqTo count occurrences:
sort fruits.txt | uniq -cTo display the most frequent entries first:
sort fruits.txt | uniq -c | sort -nrThe result will resemble:
3 apple
2 banana
1 orangeThis is a useful pattern to remember:
sort file.txt | uniq -c | sort -nrIt sorts the input, counts adjacent identical lines, and then sorts the counts numerically in descending order.
When to use it: Finding repeated values, counting categories, and identifying frequent entries in logs or text files.
7. wc: Count Lines, Words, and Characters
Sometimes you don’t need to inspect a file’s contents. You just need to know how much data it contains.
That’s where wc comes in.
Count lines:
wc -l server.logCount words:
wc -w document.txtCount bytes:
wc -c document.txtCount characters:
wc -m document.txtThese last two are different: a multibyte character can occupy more than one byte.
You can also count the number of lines matching a pattern:
grep -c "ERROR" server.logOr count files in the current directory using Bash filename expansion:
printf '%s\n' * | wc -lThis last example counts the entries matched by *, including directories and hidden-file exclusions inherent to the default expansion. It is not a recursive file count.
For a count of regular files recursively, use:
find . -type f | wc -lWhen to use it: Counting lines, measuring text, estimating the size of a dataset, and checking the output of a processing step.
Put the Commands Together
Individual commands are useful. Combining them lets you solve more interesting problems.
Imagine you have a log file and want to identify the ten most frequent error messages.
You could run:
grep "ERROR" server.log | sort | uniq -c | sort -nr | head -n 10Let’s break that down:
CommandPurposegrep "ERROR"Select lines containing ERRORsortPlace identical lines next to one anotheruniq -cCount adjacent identical linessort -nrSort counts in descending numerical orderhead -n 10Display the first ten results
This is a compact workflow, but it is important to understand what it actually counts.
It counts identical complete log lines, not necessarily distinct error types. If each line contains a different timestamp, the same underlying error may appear as multiple separate entries.
If you want to group messages by error category, you first need to extract or normalize the relevant part of each line.
That is where tools such as awk and sed become useful.
The broader idea is simple:
Search the data, extract the relevant information, transform it, organize it, and summarize the result.
You can apply this approach to log files, configuration files, exported tables, and many other kinds of structured text.







