Skip to main content

Day 4 of My DevOps Journey: awk, sed, and find

Updated 4 min readMarkdown
Day 4 of My DevOps Journey: awk, sed, and find

Shorter day today - didn't get as much time as usual - but still covered a few text-processing tools that are genuinely used all the time when working with logs and config files on Linux: awk, sed, and find.

awk

awk is a text-processing tool built for scanning files line by line and acting on patterns you give it. It's much more than a search tool - it understands columns and supports actual logic.

Print the whole file:

awk '{print}' file_name

Print only lines matching a pattern:

awk '/text_to_find/ {print}' file_name

This scans every line, and only prints the ones containing text_to_find.

Print specific columns from matching lines:

awk '/text_to_find/ {print $1,$2}' file_name

awk automatically splits each line into columns (by whitespace, by default) - $1 is the first column, $2 the second, and so on.So this prints just the 1st and 2nd columns from any line that matches the pattern.

Combine with head/tail:

awk '{print}' file_name | head -n 5
awk '{print}' file_name | tail -n 5

Since awk's output is just text, it can be piped into head or tail the same way any other command's output can - useful for previewing results instead of dumping everything to the screen.

Using conditions inside awk:

awk '{if ($1=="july" && $2=="11") print $3,$4}' file_name

This is where awk goes beyond grep - you can write actual conditional logic. Here, it checks whether column 1 equals "july" AND column 2 equals "11" (essentially filtering for a specific date), and if both match, prints columns 3 and 4. This kind of structured filtering is very handy for parsing log files that follow a consistent column format.

grep vs awk

Now that I've used both, the difference is clearer:

  • grep is built around regular expressions - you give it a pattern, and it returns the lines that match. Simple and fast for "does this text exist / show me lines containing X."

  • awk is a full command-line tool with programmatic behavior - it understands columns, supports conditions (if, &&, ||), and lets you extract or transform specific parts of matching lines rather than the whole line.

Rule of thumb: reach for grep for a quick search, reach for awk when you need to filter on specific fields or apply logic.

sed - the stream editor

sed is used for find-and-replace operations on text, and it's the standard tool for this on Linux.

Preview a replacement (doesn't modify the file):

sed 's/text_to_replace/replacement_text/g' file_name
  • s - substitute

  • g - apply globally (replace every occurrence on a line, not just the first)

This prints the result to the terminal but leaves the original file untouched - good for checking what a change would look like before committing to it.

Apply the replacement and save it:

sed -i 's/text_to_replace/replacement_text/g' file_name

The -i flag edits the file in place, meaning the changes are actually saved to the file. This is the version you'd use once you're confident about the replacement.

find

find . -name file_name

Searches for a file by name, starting the search from the current directory (.). Useful when you know a file's name but not exactly where it lives within a directory tree.

What's next

Continuing with more Linux commands and moving into networking commands next.

Twitter - Nisarg Makwana on X: "Day 4 of learning DevOps Short day, but covered: awk (pattern matching + column filtering) grep vs awk (regex vs conditional logic) sed (find & replace in files) find (locate files by name) More Linux + networking next. #DevOps #Linux #LearningInPublic" / X

Github - daily_devops/Day_04.md at main · NisargMakwana142/daily_devops


No comments yet. Be the first to comment.