In this article, we will solve VNSGU TYBCA Semester 5 Linux (UNIX) Practical – Set D using shell scripting.
This practical focuses on basic file handling and text-processing commands such as:
catheadtailwcgrep
📌 Practical Question
Write a shell script to perform following:
- Create a file named
marks.txtand insert 5 lines of text. - Display the first 3 lines of
marks.txt. - Display the last 2 lines of
marks.txt. - Count the number of words, lines, and characters in
marks.txt. - Search for the word Linux in
marks.txt.
🧠 Commands Used
| Command | Purpose |
|---|---|
cat |
Create and display file content |
head |
Display the beginning of a file |
tail |
Display the end of a file |
wc |
Count lines, words, and characters |
grep |
Search for text |
✅ Solution 1: Using read, head, tail, wc and grep
This solution takes 5 lines interactively from the user and stores them in marks.txt.
Shell Script
#!/bin/bash
echo "Enter 5 lines of text:"
> marks.txt
for ((i=1; i<=5; i++))
do
read -p "Enter line $i: " line
echo "$line" >> marks.txt
done
echo
echo "----- First 3 Lines -----"
head -n 3 marks.txt
echo
echo "----- Last 2 Lines -----"
tail -n 2 marks.txt
echo
echo "----- File Statistics -----"
echo "Lines : $(wc -l < marks.txt)"
echo "Words : $(wc -w < marks.txt)"
echo "Characters : $(wc -m < marks.txt)"
echo
echo "----- Searching for Linux -----"
grep "Linux" marks.txt
🔍 Step-by-Step Explanation
Step 1: Create or Empty the File
> marks.txt
This creates marks.txt if it does not exist.
If the file already exists, its previous content is removed.
Step 2: Take 5 Lines from the User
for ((i=1; i<=5; i++))
do
read -p "Enter line $i: " line
echo "$line" >> marks.txt
done
The loop runs exactly 5 times.
For example:
Enter line 1: Linux is an operating system
Enter line 2: Linux supports shell scripting
Enter line 3: Shell commands are useful
Enter line 4: Unix and Linux are related
Enter line 5: Linux is widely used
Each line is appended to marks.txt.
The >> operator is used to append content to the file.
Step 3: Display First 3 Lines
head -n 3 marks.txt
The head command displays the beginning of a file.
Here:
-n 3
means:
Display the first 3 lines.
Step 4: Display Last 2 Lines
tail -n 2 marks.txt
The tail command displays the end of a file.
Here:
-n 2
means:
Display the last 2 lines.
Step 5: Count Lines
wc -l < marks.txt
The -l option counts the number of lines.
For our five-line file:
5
The < redirects the file content to wc, so only the number is displayed.
Step 6: Count Words
wc -w < marks.txt
The -w option counts the number of words in the file.
Step 7: Count Characters
wc -m < marks.txt
The -m option counts the number of characters.
This is preferable to plain wc when the question specifically asks for characters.
Step 8: Search for Linux
grep "Linux" marks.txt
The grep command searches for the word or pattern Linux.
It displays every line containing Linux.
🖥️ Sample Output
Enter 5 lines of text:
Enter line 1: Linux is an operating system
Enter line 2: Linux supports shell scripting
Enter line 3: Shell commands are useful
Enter line 4: Unix and Linux are related
Enter line 5: Linux is widely used
----- First 3 Lines -----
Linux is an operating system
Linux supports shell scripting
Shell commands are useful
----- Last 2 Lines -----
Unix and Linux are related
Linux is widely used
----- File Statistics -----
Lines : 5
Words : 24
Characters : 142
----- Searching for Linux -----
Linux is an operating system
Linux supports shell scripting
Unix and Linux are related
Linux is widely used
The exact word and character counts will depend on the text entered by the user.
🔄 Solution 2: Using cat to Insert 5 Lines
The question can also be solved using cat.
#!/bin/bash
echo "Enter 5 lines of text:"
cat > marks.txt
echo
echo "----- First 3 Lines -----"
head -n 3 marks.txt
echo
echo "----- Last 2 Lines -----"
tail -n 2 marks.txt
echo
echo "----- File Statistics -----"
echo "Lines : $(wc -l < marks.txt)"
echo "Words : $(wc -w < marks.txt)"
echo "Characters : $(wc -m < marks.txt)"
echo
echo "----- Searching for Linux -----"
grep "Linux" marks.txt
After entering the five lines, press:
Ctrl + D
to finish the input.
Which Method Is Better?
For beginners, Solution 1 is easier to understand because it clearly shows the five iterations of the loop.
The cat method is shorter and demonstrates interactive file creation using cat.
🔄 Solution 3: Automatically Create the File Using Here Document
If you don't want the user to enter the five lines manually, you can use a here document.
#!/bin/bash
cat > marks.txt << EOF
Linux is an operating system
Linux supports shell scripting
Shell commands are useful
Unix and Linux are related
Linux is widely used
EOF
echo "----- First 3 Lines -----"
head -n 3 marks.txt
echo
echo "----- Last 2 Lines -----"
tail -n 2 marks.txt
echo
echo "----- File Statistics -----"
echo "Lines : $(wc -l < marks.txt)"
echo "Words : $(wc -w < marks.txt)"
echo "Characters : $(wc -m < marks.txt)"
echo
echo "----- Searching for Linux -----"
grep "Linux" marks.txt
Here:
<< EOF
allows multiple lines of text to be provided as input to the command.
The EOF at the end marks the end of the input.
🔍 Searching for Linux Case-Insensitively
By default:
grep "Linux" marks.txt
is case-sensitive.
Therefore, these are treated differently:
Linux
linux
LINUX
If the search should ignore uppercase/lowercase differences, use:
grep -i "Linux" marks.txt
The -i option makes the search case-insensitive.
🔄 Alternative: Using sed
Although head and tail are the simplest commands for this question, sed can also be used.
First 3 Lines
sed -n '1,3p' marks.txt
Here:
-
-n→ Suppresses automatic output -
1,3→ Lines 1 to 3 -
p→ Print
Last 2 Lines
We can calculate the total number of lines first:
total=$(wc -l < marks.txt)
start=$((total - 1))
sed -n "${start},${total}p" marks.txt
However, for this practical, head and tail are simpler and easier to remember.
📊 Important Commands at a Glance
| Requirement | Command |
|---|---|
| Create/empty file | > marks.txt |
| Append text | echo "text" >> marks.txt |
| First 3 lines | head -n 3 marks.txt |
| Last 2 lines | tail -n 2 marks.txt |
| Count lines | wc -l marks.txt |
| Count words | wc -w marks.txt |
| Count characters | wc -m marks.txt |
| Search Linux | grep "Linux" marks.txt |
| Case-insensitive search | grep -i "Linux" marks.txt |
🧠 Why Use wc -m for Characters?
A common mistake is to use:
wc marks.txt
and assume that the third value is the character count.
By default, wc generally displays:
lines words bytes
Therefore, when the question specifically asks for characters, use:
wc -m marks.txt
This makes the intention of the script clear.
⚠️ Common Mistakes
Mistake 1: Using > Instead of >>
This:
echo "$line" > marks.txt
overwrites the file every time.
Therefore, after the loop only the last line would remain.
Use:
echo "$line" >> marks.txt
to append each line.
Mistake 2: Using wc for Character Count
Instead of:
wc marks.txt
use:
wc -m marks.txt
when you specifically need the character count.
Mistake 3: Forgetting Case Sensitivity
This:
grep "Linux" marks.txt
does not necessarily match:
linux
LINUX
Use:
grep -i "Linux" marks.txt
when a case-insensitive search is required.
🎯 Practical Exam Tips
Remember these commands:
head -n 3 marks.txt
→ First 3 lines
tail -n 2 marks.txt
→ Last 2 lines
wc -l marks.txt
→ Number of lines
wc -w marks.txt
→ Number of words
wc -m marks.txt
→ Number of characters
grep "Linux" marks.txt
→ Search for Linux
📚 Related Linux Practical Sets
This solution is part of the VNSGU TYBCA Sem 5 Linux (UNIX) Practical – OCT/Nov 2025 solution series.
- Set A – Simple Interest & Compound Interest
- Set B – Vowels Count & Case Conversion
- Set C – Palindrome String
- Set D – File Operations & Text Processing
- Set E – Display Lines with Validation
- Set F – Count Files & Directories
- Set G – Recently Modified Files
- Set H – Employee Gross Salary
🐧 Conclusion
Set D is an important Linux practical because it combines several commonly used file and text-processing commands in one shell script.
The main commands to remember are:
cat → File creation/input
head → Beginning of file
tail → End of file
wc → Counting
grep → Searching
Practice these commands individually first and then combine them into the complete shell script.
💬 What Do You Prefer?
For displaying specific lines from a file, which approach do you prefer?
head / tail or sed?
Share your approach in the comments! 🐧💻
📌 Tags
linux shell unix beginners
Top comments (0)