Fundamentals 5 min read

Quick Shell Tricks to Remove Blank Lines from Logs with grep, sed, awk, tr, and vi

This article shares concise, easy-to-understand shell command techniques—using grep, sed, awk, tr, and vi—to efficiently delete empty lines from log files, highlighting each tool's syntax, options, and practical usage for embedded software developers.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Quick Shell Tricks to Remove Blank Lines from Logs with grep, sed, awk, tr, and vi

The author reflects on the saying "scripts are fiercer than tigers," noting that scripts are powerful yet error‑prone, and that shell scripts are ideal for quick prototypes and log processing in embedded development.

1. grep

Use $ grep -v '^$' file to output only non‑empty lines. The -v flag inverts the match, selecting lines that do not match the regular expression. The caret ^ anchors the start of a line and $ anchors the end.

2. sed

The stream editor sed can delete blank lines with $ sed '/^$/d' file. The pattern ^$ matches empty lines, and the d command deletes them. See man sed for more options.

3. awk

Awk can filter out empty lines using $ awk '!/^$/' file. Awk processes each line, and the default action is print, so the command prints only lines that do not match ^$. Awk programs consist of pattern { action } pairs.

4. tr

The translate utility tr can squeeze repeated newline characters: $ cat in.txt | tr -s '\n'. The -s (squeeze‑repeats) option collapses consecutive identical characters into a single instance.

5. vi

Within the vi editor, the command :g/^$/d globally searches for empty lines ( ^$) and deletes them ( d).

These five methods provide a variety of ways to remove blank lines from logs, each suited to different workflows and preferences.

The author plans future articles covering shell script fundamentals, tips, coding standards, and analyses of open‑source shell projects.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ShellViLog ProcessingGrepawksedtr
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.