Introduction
In this lab, you will learn how to use the Linux look command to search for specific words or phrases in text files. The look command is a useful tool for quickly finding lines that start with a given string of characters. You will explore the purpose and syntax of the look command, and practice combining it with other Linux commands for more advanced text processing tasks. The lab covers searching the system dictionary, as well as searching custom text files, with options to make the search case-insensitive and limit the output. This lab provides practical examples to help you become more proficient in text processing and editing using the Linux command line.
Understand the Purpose and Syntax of the look Command
In this step, you will learn about the purpose and syntax of the look command in Linux. The look command is used to search for lines in a file that begin with a given string of characters.
To use the look command, the basic syntax is:
look [options] string [file]
Where:
stringis the text you want to search forfileis the file you want to search in (optional)
If you don't specify a file, look will search through the system dictionary file, typically located at /usr/share/dict/words.
Let's try some examples:
look apple
This will search the system dictionary and display all words that start with "apple".
look -f apple ~/project/words.txt
This will search the words.txt file in the ~/project directory for lines starting with "apple", and the -f option makes the search case-insensitive.
look -n 5 app ~/project/words.txt
This will display the first 5 lines that start with "app" in the words.txt file.
Example output:
apple
applejack
applesauce
appliance
application
The look command can be a useful tool for quickly searching through text files for specific words or patterns.
Search for Specific Words or Phrases in Text Files
In this step, you will learn how to use the look command to search for specific words or phrases within text files.
First, let's create a sample text file to work with:
echo "The quick brown fox jumps over the lazy dog." > ~/project/sample.txt
Now, let's search for the word "fox" in the sample.txt file:
look fox ~/project/sample.txt
This will output:
fox
You can also search for phrases by enclosing the search term in quotes:
look "quick brown" ~/project/sample.txt
This will output:
quick brown
The look command is case-sensitive by default. To make the search case-insensitive, use the -f option:
look -f "the" ~/project/sample.txt
This will output:
The
the
You can also use the look command to search for lines that start with a specific pattern:
look app ~/project/words.txt
This will search the words.txt file (which we created in the previous step) and display all lines starting with "app".
Example output:
apple
applejack
applesauce
appliance
application
The look command can be a powerful tool for quickly searching through text files for specific words or phrases.
Combine the look Command with Other Linux Commands
In this final step, you will learn how to combine the look command with other Linux commands to perform more advanced text processing tasks.
One common use case is to combine look with grep to filter the output further. For example, let's say we want to find all words in the system dictionary that start with "app" and contain the letter "l":
look app | grep l
This will output:
apple
applaud
applause
applicable
application
You can also use the look command to find lines in a file that match a specific pattern, and then use other commands to perform additional operations on those lines.
For instance, let's say we have a file employees.txt with employee names and salaries, and we want to find all employees with a salary over $50,000:
look -f over ~/project/employees.txt | grep -E '[0-9]+\,[0-9]+' | awk -F, '{print $1, "$" $2}'
This will output:
John Doe $75,000
Jane Smith $62,500
Here's how it works:
look -f over ~/project/employees.txtsearches theemployees.txtfile for lines starting with "over" (case-insensitive).grep -E '[0-9]+\,[0-9]+'filters the output to only include lines with a number, comma, and another number (the salary).awk -F, '{print $1, "$" $2}'formats the output to display the name and salary in a readable format.
The look command can be a powerful tool when combined with other Linux commands like grep, awk, sed, and cut to perform complex text processing tasks.
Summary
In this lab, you learned about the purpose and syntax of the look command in Linux, which is used to search for lines in a file that begin with a given string of characters. You also learned how to use the look command to search for specific words or phrases within text files, including the ability to make the search case-insensitive. The look command can be a useful tool for quickly searching through text files for specific words or patterns.



