Linux look Command with Practical Examples

LinuxLinuxBeginner
Practice Now

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.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/head("`File Beginning Display`") linux/TextProcessingGroup -.-> linux/grep("`Pattern Searching`") linux/TextProcessingGroup -.-> linux/awk("`Text Processing`") subgraph Lab Skills linux/cat -.-> lab-422770{{"`Linux look Command with Practical Examples`"}} linux/head -.-> lab-422770{{"`Linux look Command with Practical Examples`"}} linux/grep -.-> lab-422770{{"`Linux look Command with Practical Examples`"}} linux/awk -.-> lab-422770{{"`Linux look Command with Practical Examples`"}} end

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:

  • string is the text you want to search for
  • file is 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.

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:

  1. look -f over ~/project/employees.txt searches the employees.txt file for lines starting with "over" (case-insensitive).
  2. grep -E '[0-9]+\,[0-9]+' filters the output to only include lines with a number, comma, and another number (the salary).
  3. 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.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like