Linux Practices: Using sort and uniq
Practice 1: Sorting a List Alphabetically with sort
Objective: Use sort to arrange lines in a file alphabetically.
Instructions:
1. Create a file named Heroes.txt with the following content:
Thor
IronMan
Hulk
BlackWidow
CaptainAmerica
2. Sort the file alphabetically:
sort Heroes.txt
3. Verify that the output is sorted alphabetically.
Practice 2: Sorting a List in Reverse Order with sort
Objective: Use sort to reverse the order of lines.
Instructions:
1. Sort the Heroes.txt file in reverse order:
sort -r Heroes.txt
2. Verify that the output is in reverse alphabetical order.
Practice 3: Sorting by Numeric Values with sort
Objective: Use sort to sort lines based on numeric values.
Instructions:
1. Create a file named PowerLevels.txt with the following content:
300 Hulk
900 IronMan
700 Thor
600 BlackWidow
500 CaptainAmerica
2. Sort the file numerically:
sort -n PowerLevels.txt
3. Verify that the output is sorted by power levels in ascending order.
Practice 4: Sorting by a Specific Column with sort
Objective: Use sort to sort lines based on a specific field.
Instructions:
1. Sort PowerLevels.txt by the hero names instead of numbers:
sort -k 2 PowerLevels.txt
2. Verify that the output is sorted alphabetically by hero names.
Practice 5: Removing Duplicate Lines with uniq
Objective: Use uniq to eliminate duplicate lines in a file.
Instructions:
1. Create a file named DuplicateHeroes.txt with the following content:
Thor
IronMan
Thor
Hulk
IronMan
2. Remove duplicate lines using:
sort DuplicateHeroes.txt | uniq
3. Verify that only unique lines remain.
Practice 6: Counting Duplicate Lines with uniq -c
Objective: Use uniq to count occurrences of duplicate lines.
Instructions:
1. Count how many times each hero appears in DuplicateHeroes.txt:
sort DuplicateHeroes.txt | uniq -c
2. Verify the output includes the count for each hero.
Practice 7: Highlighting Unique Lines with uniq -u
Objective: Use uniq to display only lines that are not duplicated.
Instructions:
1. Display unique lines from DuplicateHeroes.txt:
sort DuplicateHeroes.txt | uniq -u
2. Verify that only non-repeated heroes are listed.
Practice 8: Sorting and Finding Unique Entries from Multiple Files
Objective: Combine sort and uniq to process multiple files.
Instructions:
1. Combine and sort the contents of Avengers.txt and Guardians.txt:
cat Avengers.txt Guardians.txt | sort | uniq
2. Verify that duplicates are removed and the list is sorted.
Practice 9: Ignoring Case Sensitivity with uniq
Objective: Use uniq to process lines while ignoring case differences.
Instructions:
1. Create a file named MixedCaseHeroes.txt with the following content:
Thor
thor
IronMan
ironman
2. Remove duplicates while ignoring case:
sort MixedCaseHeroes.txt | uniq -i
3. Verify that case differences are ignored.
Practice 10: Finding Repeated Lines with uniq -d
Objective: Use uniq to display only duplicate lines.
Instructions:
1. Display only the duplicate lines in DuplicateHeroes.txt:
sort DuplicateHeroes.txt | uniq -d
2. Verify that only repeated heroes are shown.