0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Reverse Lines in a File

Use the command tac filename or a Bash script with tac to reverse lines in a file; for example, tac input.txt prints the lines in reverse order.
📋

Examples

Inputapple banana cherry
Outputcherry banana apple
Inputline1 line2 line3 line4
Outputline4 line3 line2 line1
Input
Output
🧠

How to Think About It

To reverse lines in a file, think of reading all lines from the bottom up instead of top down. The tac command does exactly this by printing lines in reverse order. Alternatively, you can read all lines into memory and print them in reverse sequence.
📐

Algorithm

1
Get the filename as input.
2
Read all lines from the file.
3
Print the lines starting from the last line to the first line.
💻

Code

bash
#!/bin/bash
# Script to reverse lines in a file

if [ $# -ne 1 ]; then
  echo "Usage: $0 filename"
  exit 1
fi

filename="$1"

if [ ! -f "$filename" ]; then
  echo "File not found: $filename"
  exit 1
fi

# Use tac to reverse lines
reversed_lines=$(tac "$filename")
echo "$reversed_lines"
Output
cherry banana apple
🔍

Dry Run

Let's trace reversing lines of a file with content: apple\nbanana\ncherry

1

Read file lines

Lines read: apple, banana, cherry

2

Reverse lines using tac

Output lines: cherry, banana, apple

3

Print reversed lines

Printed: cherry\nbanana\napple

StepActionResult
1Read linesapple, banana, cherry
2Reverse linescherry, banana, apple
3Print linescherry\nbanana\napple
💡

Why This Works

Step 1: Using tac command

The tac command reads a file and outputs its lines in reverse order, which is exactly what we want to reverse lines.

Step 2: Checking file existence

The script first checks if the file exists to avoid errors when reading a non-existent file.

Step 3: Printing reversed lines

After reversing, the script prints the lines so the user sees the reversed content on the screen.

🔄

Alternative Approaches

Using awk
bash
awk '{line[NR] = $0} END {for (i=NR; i>0; i--) print line[i]}' filename
This uses awk to store lines in an array and print them in reverse; useful if tac is unavailable.
Using sed
bash
sed '1!G;h;$!d' filename
This sed command reverses lines by appending and printing; more complex but works without tac.
Using a Bash loop
bash
mapfile -t lines < filename; for ((i=${#lines[@]}-1; i>=0; i--)); do echo "${lines[i]}"; done
Reads lines into an array and prints in reverse; good for learning Bash arrays.

Complexity: O(n) time, O(n) space

Time Complexity

The script reads each line once and prints each line once, so time grows linearly with the number of lines.

Space Complexity

The entire file lines are stored in memory (by tac or arrays), so space grows linearly with file size.

Which Approach is Fastest?

Using tac is fastest and simplest; awk and sed are slower but useful if tac is missing; Bash arrays are good for learning but less efficient.

ApproachTimeSpaceBest For
tac commandO(n)O(n)Fastest and simplest for reversing lines
awk arrayO(n)O(n)When tac is unavailable, moderate complexity
sed commandO(n)O(n)No tac or awk, but harder to read
Bash array loopO(n)O(n)Learning Bash arrays, less efficient
💡
Use tac filename for a quick and efficient way to reverse lines in a file.
⚠️
Beginners often try to reverse lines by reading and printing line by line without storing, which prints lines in original order.