Practice Exercise: Exploring Text Manipulation with cat and echo
Objective
Gain hands-on experience with text manipulation in Linux using the cat and echo commands.
Task 1: Viewing File Contents with cat
- Open a terminal window.
- Create a new text file named
sample.txtusing a text editor (e.g.,nanoorvim) and add some text to it. - Use the
catcommand to view the contents of thesample.txtfile. - Experiment with options like
-nto display line numbers and-bto number non-empty lines only.[intern@intern-a1t-inf-lnx1 ~]$ vim sample.txt [intern@intern-a1t-inf-lnx1 ~]$ cat sample.txt This is not an empty line Another not empty line [intern@intern-a1t-inf-lnx1 ~]$ cat sample.txt -n 1 This is not an empty line 2 3 Another not empty line 4 [intern@intern-a1t-inf-lnx1 ~]$ cat sample.txt -b 1 This is not an empty line 2 Another not empty line
Task 2: Creating and Appending to Text Files with cat
- Create a new text file named
mynotes.txtusing thecatcommand and add some notes to it directly (e.g.,cat > mynotes.txt). - Append additional text to the
mynotes.txtfile using thecatcommand and the>>operator. - Use
catto display the updated contents of themynotes.txtfile.[intern@intern-a1t-inf-lnx1 ~]$ cat > mynotest.txt This is a test Another line # Press ctrl + d to exit [intern@intern-a1t-inf-lnx1 ~]$ cat mynotest.txt This is a test Another line [intern@intern-a1t-inf-lnx1 ~]$ cat >> mynotest.txt Appending another text # Press ctrl + d to exit [intern@intern-a1t-inf-lnx1 ~]$ cat mynotest.txt This is a test Another line Appending another text
Task 3: Using echo for Simple Text Output
- Open a terminal.
- Use
echoto print a simple message to the screen (e.g.,echo "Hello, Linux!"). - Experiment with escape sequences, such as
\nfor a new line and\tfor a tab. - Redirect the output of
echoto create a new text file namedgreeting.txt.[intern@intern-a1t-inf-lnx1 ~]$ echo "Hello, Linux!" Hello, Linux! [intern@intern-a1t-inf-lnx1 ~]$ echo -e "Hello, Linux! \n" Hello, Linux! [intern@intern-a1t-inf-lnx1 ~]$ echo -e "\tHello, Linux! \n" Hello, Linux! [intern@intern-a1t-inf-lnx1 ~]$ echo -e "\tHello, Linux! \n" > greeting.txt [intern@intern-a1t-inf-lnx1 ~]$ cat greeting.txt Hello, Linux!
Task 4: Combining echo and cat
- Use
echoto generate some text (e.g., a list of numbers or names). - Pipe the output of
echointo a new text file (e.g.,echo "1\n2\n3\n4" > numbers.txt). - Use
catto view the contents of thenumbers.txtfile. - Practice appending additional text to the same file using
echoand the>>operator.[intern@intern-a1t-inf-lnx1 ~]$ echo -e "1\n2\n3\n4" > numbers.txt [intern@intern-a1t-inf-lnx1 ~]$ cat numbers.txt 1 2 3 4 [intern@intern-a1t-inf-lnx1 ~]$ echo -e "\n5" >> numbers.txt 1 2 3 4 5
Task 5: Creating and Using Shell Scripts
- Create a new shell script file named
greet.shusing a text editor. - Inside the script, use
echoto print a personalized greeting to the screen. - Save the script and make it executable using the
chmodcommand. - Execute the script to see your custom greeting.
[intern@intern-a1t-inf-lnx1 ~]$ vim greet.sh [intern@intern-a1t-inf-lnx1 ~]$ cat greet.sh #!/bin/bash echo "Hello $USER!" [intern@intern-a1t-inf-lnx1 ~]$ chmod +x greet.sh [intern@intern-a1t-inf-lnx1 ~]$ ./greet.sh Hello intern!
Conclusion
In this lab exercise, you've learned how to manipulate text using the cat and echo commands. You've viewed file contents, created and appended text to files, used echo for basic text output, combined echo and cat to create text files, and even created and executed a simple shell script. These skills are fundamental for working with text data in Linux and shell scripting.