Using wc
Practice 1: Counting Lines in a File
Objective: Use wc to count the number of lines in a file.
Instructions:
1. Create a file named Heroes.txt with the following content:
IronMan
Thor
Hulk
BlackWidow
CaptainAmerica
2. Count the number of lines in the file:
wc -l Heroes.txt
3. Verify that the output matches the number of lines.
Practice 2: Counting Words in a File
Objective: Use wc to count the number of words in a file.
Instructions:
1. Count the number of words in Heroes.txt:
wc -w Heroes.txt
2. Verify that each name is counted as one word.
Practice 3: Counting Characters in a File
Objective: Use wc to count the number of characters in a file.
Instructions:
1. Count the total number of characters in Heroes.txt:
wc -m Heroes.txt
2. Verify that spaces and newline characters are included in the count.
Practice 4: Displaying All Counts Together
Objective: Use wc to display lines, words, and characters in a file.
Instructions:
1. Display all counts (lines, words, characters) for Heroes.txt:
wc Heroes.txt
2. Verify that the output shows all three counts.
Practice 5: Counting Lines from Command Output
Objective: Use wc to count lines from another command’s output.
Instructions:
1. List all files in the current directory and count the lines:
ls | wc -l
2. Verify that the count matches the number of files and directories.
Practice 6: Counting Words from a Command Output
Objective: Use wc to count words from another command’s output.
Instructions:
1. Display all .txt files and count the words in the output:
ls *.txt | wc -w
2. Verify that each filename is counted as one word.
Practice 7: Counting Characters from a Command Output
Objective: Use wc to count characters from a command’s output.
Instructions:
1. Echo a message and count the characters in the output:
echo "Avengers Assemble!" | wc -m
2. Verify that spaces and punctuation are included in the count.
Practice 8: Combining wc with Pipes
Objective: Use wc with pipes to process output from multiple commands.
Instructions:
1. Search for lines containing Thor in Heroes.txt and count them:
grep Thor Heroes.txt | wc -l
2. Verify that the count matches the occurrences of Thor.
Practice 9: Counting Multiple Files
Objective: Use wc to process multiple files at once.
Instructions:
1. Create two files, Avengers.txt and Guardians.txt, with hero names.
2. Count the lines in both files together:
wc -l Avengers.txt Guardians.txt
3. Verify that the output includes counts for each file and a total.
Practice 10: Counting Specific Parts of a File
Objective: Use wc with filtering commands to count specific data.
Instructions:
1. Count the number of heroes starting with B in Heroes.txt:
grep ^B Heroes.txt | wc -l
2. Verify that the count matches the number of matching lines.