Sitemap

Stop Reading Text Files Manually: 7 Linux Tricks to Find What Matters Faster

7 min readOct 9, 2026

--

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:

  • grep
  • cut
  • awk
  • sed
  • sort
  • uniq
  • wc

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 high

To find errors:

grep "ERROR" server.log

To search without worrying about uppercase or lowercase:

grep -i "error" server.log

To display line numbers:

grep -n "ERROR" server.log

To find lines that do not contain a particular pattern:

grep -v "INFO" server.log

You 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.log

The ^ 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,70000

You want only the employee names.

Use:

cut -d',' -f1 employees.csv

Here:

  • -d',' specifies the comma delimiter.
  • -f1 selects the first field.

To extract names and salaries:

cut -d',' -f1,3 employees.csv

To extract the second field:

cut -d',' -f2 employees.csv

You can also extract character positions rather than delimited fields:

cut -c1-10 file.txt

This 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",75000

Because 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.

Press enter or click to view image in full size

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 70000

Save it as employees.txt.

To print only the first column:

awk '{print $1}' employees.txt

Output:

Alice
Bob
Charlie
Diana

To print names and salaries:

awk '{print $1, $3}' employees.txt

Now suppose you want only employees earning more than 70,000:

awk '$3 > 70000' employees.txt

You can calculate the average salary:

awk '{sum += $3; count++} END {if (count) print sum/count}' employees.txt

This works in two stages:

  1. For every line, add the third field to sum and increment count.
  2. After processing the file, calculate the average.

You can also calculate the total:

awk '{sum += $3} END {print sum}' employees.txt

Why 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=production

You 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.txt

This prints the modified text to the terminal. It does not change the original file.

The substitution syntax is:

s/old-text/new-text/g

The 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.txt

On 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.txt

To print only lines 10 through 20:

sed -n '10,20p' config.txt

A 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
50

Run:

sort numbers.txt

The default is lexicographic sorting, not numerical sorting. Therefore, the result may not be the numerical order you expect.

For numerical sorting:

sort -n numbers.txt

Output:

9
20
50
100

For reverse numerical order:

sort -nr numbers.txt

For a tab-separated file, you can sort by the second column:

sort -t $'\t' -k2,2 data.tsv

Here:

  • -t specifies the delimiter.
  • -k2,2 selects the second field as the sort key.

To sort that field numerically in descending order:

sort -t $'\t' -k2,2nr data.tsv

When 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
apple

You might expect:

uniq fruits.txt

to 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 | uniq

To count occurrences:

sort fruits.txt | uniq -c

To display the most frequent entries first:

sort fruits.txt | uniq -c | sort -nr

The result will resemble:

3 apple
2 banana
1 orange

This is a useful pattern to remember:

sort file.txt | uniq -c | sort -nr

It 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.log

Count words:

wc -w document.txt

Count bytes:

wc -c document.txt

Count characters:

wc -m document.txt

These 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.log

Or count files in the current directory using Bash filename expansion:

printf '%s\n' * | wc -l

This 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 -l

When 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 10

Let’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.

--

--

Raja ishaq Nabi Khan
Raja ishaq Nabi Khan

Written by Raja ishaq Nabi Khan

Veterinarian & Molecular Biology–Trained Bioinformatics Scientist | RNA Epigenomics & Functional Genomics | Aging, Cognition, Host–Pathogen Interactions