DEV Community

Kervie Sazon
Kervie Sazon

Posted on • Edited on

Linux Fundamentals - Part 11: Text Processing

Using grep Command

The grep is used to search for text within files.
It prints the line that has the text that you want to see.

Example:
To search for text "Kervie" in a file named "textpro.sh".

The grep command has options to change how it works:
-i - Search ignoring case differences (uppercase or lowercase)
-r - Search through all files in a directory and its subdirectories
-v - Find lines that do not match the pattern

The grep -i (Ignore Case)

The grep -r (Recursive Search):

The grep -v (Invert Match):

Using tee Command

tee used to create a file and write content while displaying it on the terminal.

Example:

Using awk Command

awk is a text-processing tool mainly used for:

  • Reading files line by line
  • Splitting each line into fields (columns)
  • Performing actions based on patterns or conditions
  • Printing formatted output

The structure looks like this:

awk 'pattern { action }' file
Enter fullscreen mode Exit fullscreen mode
  • pattern - when to run the action (optional)
  • action - what to do (usually print something)
  • If no pattern is given → action runs on every line

Example:
awk "{ print $2 }" filename

In this example:
I used awk to read the file line by line. print to display it and $2 means second column.

Using sort Command

sort is used to sort line alphabetically or numerically.

Commonly used when working with:

  • Email lists
  • Logs
  • Structured text data

Example:

Other sort options:
sort -n - number.
sort -r - reverse order.

Using cut Command

cut is used to extract selected bytes from text.

Example:

This helps for parsing usernames, IDs, or fixed-format text files.

Using diff Command

diff is used to identify differences between two files.
Widely used for Comparing configuration files and Troubleshooting issues.

Example:

Today, I learned some useful Linux text-processing commands that are essential for working with logs and configuration files. I practiced using grep to search for patterns in files, tee to write created content while displaying it on terminal. awk and cut to extract specific fields or columns from text, sort to organize data, and diff to compare files and see what changed.

Top comments (0)