Bash Filters and Text Manipulation: head, tail, and wc

Bash, the powerful command-line interface for Linux and Unix systems, offers a suite of tools known as filters that streamline text processing. In this post, we’ll focus on three essential filters: head, tail, and wc. Let’s uncover how these tools can help you extract, analyze, and manipulate text data with ease.

What are Filters?

Filters are programs that take input from standard input (stdin), process it, and then output the results to standard output (stdout). This allows you to chain commands together using pipes (|) for efficient data flow.

head: Getting the First Glimpse

The head command displays the first few lines of a file or input. By default, it shows the first 10 lines, but you can customize this with the -n option.

head -n 5 file.txt # Display the first 5 lines of file.txt

tail: Seeing the End

The tail command does the opposite, showing the last few lines. It also defaults to 10 lines and accepts the -n option. Additionally, tail has a powerful -f (follow) option for monitoring files that are being updated in real time.

tail -n 3 logfile.txt # Display the last 3 lines of logfile.txt

wc: Counting Words, Lines, and Characters

The wc command provides counts for words, lines, and characters in a file or input. Use options like -l (lines), -w (words), and -c (characters) to specify what you want to count.

wc -l myfile.txt # Count the number of lines in myfile.txt

Combining Filters for Precision

Filters are even more powerful when combined. For example, to display lines 11 through 15 of a file:

head -n 15 file.txt | tail -n 5

Example: Monitoring Log Files

tail -f /var/log/syslog # Continuously monitor system log

Here Document Example

while read -r time; do
  echo "Current time: $time"
  sleep 1
done << EOF
$(date)
$(date)
$(date) 
EOF

This script outputs the current time three times, with a one-second delay between each output. The << EOF creates a “here document” that provides input to the while loop.