Appearance
Day 3: Working with Text Editors and Files
Objectives
- Learn to use basic text editors: nano and gedit.
- Understand file manipulation commands: viewing, searching, and editing text files.
- Get familiar with text processing tools like
cat
,grep
, andawk
.
1. Using Text Editors
nano Text Editor
nano is a simple and user-friendly command-line text editor.
Opening a File with nano
- To create or open a file:bash
$ nano filename
- To create or open a file:
Basic nano Commands
- Save Changes: Press
Ctrl + O
, then pressEnter
. - Exit nano: Press
Ctrl + X
. - Cut Text: Press
Ctrl + K
. - Paste Text: Press
Ctrl + U
. - Search Text: Press
Ctrl + W
.
- Save Changes: Press
gedit Text Editor
gedit is a graphical text editor for the GNOME desktop environment.
Opening a File with gedit
- To create or open a file:bash
$ gedit filename &
- The
&
symbol runs gedit in the background, allowing the terminal to be used for other commands.
- To create or open a file:
Basic gedit Features
- Save Changes: Click
Save
in the menu or pressCtrl + S
. - Exit gedit: Click
Close
in the menu or pressCtrl + Q
. - Find and Replace: Click
Search
in the menu, thenFind...
orReplace...
.
- Save Changes: Click
2. File Manipulation Commands
Viewing Text Files
cat Command
- Concatenate and display file content:bash
$ cat filename
- Display multiple files:bash
$ cat file1 file2
- Concatenate and display file content:
more Command
- View file content one screen at a time:bash
$ more filename
- Navigate through the file using
Space
for the next page,b
for the previous page, andq
to quit.
- View file content one screen at a time:
less Command
- Similar to
more
but with more navigation options:bash$ less filename
- Navigate using arrow keys,
Space
for the next page,b
for the previous page, andq
to quit.
- Similar to
Searching Text Files
- grep Command
- Search for specific text in a file:bash
$ grep "search_term" filename
- Search recursively in all files in a directory:bash
$ grep -r "search_term" directory
- Common options:
-i
: Ignore case.-n
: Show line numbers.-v
: Invert match, showing lines that do not match the search term.
- Search for specific text in a file:
Editing Text Files
sed Command
- Stream editor for filtering and transforming text.
- Replace text in a file:bash
$ sed 's/old_text/new_text/' filename
- Replace text globally in the file:bash
$ sed 's/old_text/new_text/g' filename
- Save changes to the same file:bash
$ sed -i 's/old_text/new_text/g' filename
awk Command
- Pattern scanning and processing language.
- Print specific columns from a file:bash
$ awk '{print $1, $3}' filename
- Print lines matching a pattern:bash
$ awk '/pattern/ {print}' filename
Practical Exercises
Exercise 1: Using nano
- Open a new file with nano:bash
$ nano myfile.txt
- Write the following text:
Hello, World! This is a sample file.
- Save the file and exit nano.
Exercise 2: Using gedit
- Open a new file with gedit:bash
$ gedit myfile2.txt &
- Write the following text:
Welcome to gedit. Enjoy editing text files!
- Save the file and close gedit.
Exercise 3: Viewing Files with cat, more, and less
- Create a sample text file with multiple lines using nano or gedit.
- Display the content using
cat
:bash$ cat myfile.txt
- View the content one page at a time using
more
:bash$ more myfile.txt
- Navigate through the file using
less
:bash$ less myfile.txt
Exercise 4: Searching Files with grep
- Search for the word "sample" in
myfile.txt
:bash$ grep "sample" myfile.txt
- Search for the word "sample" ignoring case:bash
$ grep -i "sample" myfile.txt
- Display line numbers while searching:bash
$ grep -n "sample" myfile.txt
Exercise 5: Editing Files with sed and awk
- Replace the word "Hello" with "Hi" in
myfile.txt
usingsed
:bash$ sed 's/Hello/Hi/' myfile.txt
- Print the first and second columns of
myfile.txt
usingawk
:bash$ awk '{print $1, $2}' myfile.txt