Cat, Grep, and Go: Leveling Up Your Text Manipulation

Cover image for Cat, Grep, and Go: Leveling Up Your Text Manipulation

Published at Sep 21, 2024
Updated at Sep 21, 2024

Primary topic

Bash and Shell Automation

Learn the shell through real commands, scripts, and text-manipulation workflows that make terminal work faster and more useful.

Explore the Bash hub

cat prints file contents, grep filters text by word or pattern, and pipes plus redirection let you turn simple commands into repeatable shell workflows. Use them together when you need to inspect files, search logs, and save the exact output you care about without leaving the terminal.

This guide starts with the beginner-friendly commands, then moves into the combinations that are actually useful in everyday Bash work.

Quick answer

  • cat is the fastest way to print small file contents directly to the terminal.
  • grep is the command-line search tool you reach for when you need matching lines from files or streamed output.
  • Pipes (|) and redirection (>, >>) are what turn one-off commands into useful workflows.

Who this is for

  • Developers who are comfortable opening a terminal but want better day-to-day command-line instincts
  • Beginners learning Bash or shell basics
  • Anyone who keeps bouncing between logs, config files, and code search

Prerequisites

  • You can open a terminal and run basic commands
  • You know what a file and directory are
  • You have a small text file or log file to experiment with

What you’ll learn

  • when to use cat and when not to
  • how grep helps you search faster
  • how pipes and redirection fit together
  • which beginner mistakes waste time in real shell work

1. cat – The Simple Viewer with Hidden Power

cat is often the first tool you’ll use to view the contents of a file. It’s short for concatenate, but most people just use it to output text to the terminal. Simple, right?

cat file.txt

That’s your basic use case—display the contents of file.txt. But cat can do more:

  • Concatenate files: You can combine multiple files into one:

    cat file1.txt file2.txt > combined.txt

    This will combine the contents of file1.txt and file2.txt into combined.txt.

  • Number lines: Want to see line numbers? Just add -n:

    cat -n file.txt

Pro Tip: While cat is great for small files, when you’re working with larger ones, you might want to consider less or more so you can scroll through them more easily.


2. grep – The Search Master

Next up is grep, the go-to tool for searching through text. It’s like the Ctrl+F of the command line, but with way more power.

Want to find every line in a file that contains a specific word? Easy:

grep "word" file.txt

This will return all the lines in file.txt that contain the word “word.” Want to search across all .txt files in a directory? No problem:

grep "word" *.txt

Pro Tip: Use --color to Highlight Matches

When searching through long files with lots of text or code, it can be difficult to spot exactly where the match is. That’s where the --color flag comes in handy. It highlights your search term, making it easier to pick out from the rest of the output:

grep --color "word" file.txt

Now, every instance of “word” will be brightly colored in the output, helping you locate it faster—especially useful when you’re dealing with files that have hundreds or thousands of lines.

Advanced grep: You can combine grep with regular expressions to search for patterns, not just words. For example, this searches for lines that start with “error”:

grep "^error" file.txt

Combining cat and grep: Now here’s where the fun begins. You can combine cat and grep using pipes to take text from one command and pass it as input to another.

cat log.txt | grep --color "error"

This outputs the contents of log.txt and highlights all the lines containing “error.”


3. Piping and Redirection: Connecting the Dots

Pipes (|) allow you to take the output of one command and use it as input for another, creating a chain of actions.

For example, if you have a log file and want to find all lines with “error” and then count how many times it appears, you can chain grep with wc -l (word count):

cat log.txt | grep "error" | wc -l

This command prints the number of lines containing “error”. Boom—instantly more powerful!


4. Redirection (>, >>)

Redirection lets you send the output of a command to a file instead of displaying it in the terminal.

  • Overwrite with >: This will take the output of your command and overwrite the contents of a file.

    echo "This will overwrite the file" > output.txt
  • Append with >>: If you don’t want to overwrite, use >> to append the output to the file.

    echo "This will add to the file" >> output.txt

You can combine redirection with other commands, too. For example, find all instances of “error” in a log file and save them to a new file:

grep "error" log.txt > errors.txt

If you run this command again and want to add more results to errors.txt instead of overwriting it:

grep "warning" log.txt >> errors.txt

5. Combining Everything Together

Now that you know how to use cat, grep, and combine them with pipes and redirection, let’s look at a more advanced example. Suppose you want to:

  1. Search for all error lines in multiple log files.
  2. Save the results to a file while still viewing the output in the terminal.

Here’s the magic one-liner:

cat *.log | grep "error" > errors.txt && cat errors.txt

Let’s break this down:

  • grep "error": We’re searching for the word “error” across multiple .log files.
  • > errors.txt: This uses redirection to save the output of grep to the file errors.txt.
  • && cat errors.txt: The && ensures that the file is created first, then we use cat to display the contents of errors.txt back to the terminal, effectively letting you both see the results and save them at the same time.

This command allows you to search through all .log files for “error,” save the results to errors.txt, and then view them immediately.


Common mistakes

  • Using cat file | grep "word" when grep "word" file is simpler. Pipes are useful, but they are not always necessary.
  • Forgetting that > overwrites a file while >> appends to it.
  • Searching without quotes when the pattern contains spaces or characters your shell might expand.
  • Using cat on huge files when less or targeted grep output would be easier to scan.

FAQ

Should I use cat or less?

Use cat when the file is small and you just want the output immediately. Use less when you need to scroll, search, or inspect a larger file without dumping everything into the terminal at once.

Do I always need cat before grep?

No. Most of the time, grep "pattern" file.txt is cleaner than cat file.txt | grep "pattern". Reach for the pipe when the input is already coming from another command.

What is the real benefit of redirection?

Redirection turns disposable terminal output into something you can keep, reuse, diff, or send into another workflow.

Next steps

Join the Community!

If you’re into coding, Linux, and just love being around people who want to learn, grow, and help each other out, come hang out with us on Discord! It’s a community of like-minded folks who share tips, talk shop, and support each other on our coding journeys. Whether you’re a beginner or a seasoned pro, there’s a place for you.

Click here to join the conversation!

Comments

No comments yet.

You must be logged in to add a comment.

Subscribe

© Copyright 2023. All rights reserved.