Practice Exercise: Miscellaneous Text Utilities
Objective:
Explore various miscellaneous text manipulation utilities available in Linux.
Task 1: Using rev to Reverse Text
- Open a terminal window.
- Create a sample text file named original.txtwith a sentence or phrase.
- Use the revcommand to reverse the characters in theoriginal.txtfile.
- Observe the reversed output.
[intern@intern-a1t-inf-lnx1 ~]$ echo "Hello, world!" > original.txt [intern@intern-a1t-inf-lnx1 ~]$ rev original.txt !dlrow ,olleH
Task 2: Counting Words and Characters with wc
- Create a new text file named word_count.txtwith several lines of text.
- Use the wccommand to count the number of words, lines, and characters in theword_count.txtfile.
- Experiment with different options to display specific counts.
[intern@intern-a1t-inf-lnx1 ~]$ echo "This is a sample text file." > word_count.txt [intern@intern-a1t-inf-lnx1 ~]$ echo "It contains multiple lines." >> word_count.txt [intern@intern-a1t-inf-lnx1 ~]$ echo "Counting words and characters." >> word_count.txt [intern@intern-a1t-inf-lnx1 ~]$ wc word_count.txt 3 14 87 word_count.txt [intern@intern-a1t-inf-lnx1 ~]$ wc -l word_count.txt 3 word_count.txt
Task 3: Sorting Text with sort
- Create a text file named unsorted.txtwith multiple lines of unsorted text.
- Use the sortcommand to alphabetically sort the lines in theunsorted.txtfile.
- Try sorting the text in reverse order and in numerical order.
[intern@intern-a1t-inf-lnx1 ~]$ echo "zebra apple banana grape" > unsorted.txt [intern@intern-a1t-inf-lnx1 ~]$ sort unsorted.txt apple banana grape zebra [intern@intern-a1t-inf-lnx1 ~]$ sort -r unsorted.txt zebra grape banana apple [intern@intern-a1t-inf-lnx1 ~]$ echo "5 42 3" >> unsorted.txt [intern@intern-a1t-inf-lnx1 ~]$ sort -n unsorted.txt apple banana grape zebra 3 5 42
Task 4: Removing Duplicate Lines with uniq
- Create a text file named duplicates.txtwith some duplicate lines.
- Use the uniqcommand to remove duplicate lines and save the result in a new file calledunique_lines.txt.[intern@intern-a1t-inf-lnx1 ~]$ echo "apple banana apple orange" > duplicates.txt [intern@intern-a1t-inf-lnx1 ~]$ uniq duplicates.txt apple banana apple orange [intern@intern-a1t-inf-lnx1 ~]$ sort duplicates.txt | uniq apple banana orange
- uniq is often used with sort
Task 5: Formatting Text with fmt
- Create a text file named long_paragraph.txtwith a lengthy paragraph of text.
- Use the fmtcommand to format the text, specifying a maximum line width.
- Observe how fmtwraps and formats the text.[intern@intern-a1t-inf-lnx1 ~]$ echo "This is a very long paragraph of text that needs formatting. It goes on and on, with no line breaks, making it difficult to read. Let's use the fmt command to format this text and wrap it to a specified line width." > long_paragraph.txt [intern@intern-a1t-inf-lnx1 ~]$ fmt -w 40 long_paragraph.txt This is a very long paragraph of text that needs formatting. It goes on and on, with no line breaks, making it difficult to read. Let's use the fmt command to format this text and wrap it to a specified line width.
Conclusion
In this lab exercise, you've explored various miscellaneous text manipulation utilities in Linux, including reversing text with rev, counting words and characters with wc, sorting text with sort, removing duplicate lines with uniq, extracting text columns with cut, and formatting text with fmt. These utilities are valuable for various text processing tasks in a Linux environment.