How to use the command 'look' (with examples)
- Osx
- December 25, 2023
The ’look’ command is used to search lines in a sorted file. It provides a way to quickly search for lines that match a specific prefix and also has an option to ignore case.
Use case 1: Look for lines which begins with the given prefix
Code:
look prefix path/to/file
Motivation:
You might want to use this use case when you have a sorted file and need to search for lines that start with a specific prefix. This can be useful in situations where you have a large file and need to quickly find relevant lines based on a specific pattern.
Explanation:
look
: The command itself.prefix
: The prefix to search for in the lines of the file.path/to/file
: The path to the sorted file to search in.
Example output:
If we have a sorted file named ’names.txt’ with the following contents:
apple
banana
cherry
grape
And we run the command look b names.txt
, it will search for lines starting with ‘b’ and the output will be:
banana
Use case 2: Look for lines ignoring case
Code:
look --ignore-case prefix path/to/file
Motivation:
Sometimes you may want to search for lines in a case-insensitive manner. This use case allows you to do just that. It can be beneficial when you want to search for a prefix regardless of whether it is in uppercase or lowercase.
Explanation:
look
: The command itself.--ignore-case
: An option to specify that the search should be case-insensitive.prefix
: The prefix to search for in the lines of the file.path/to/file
: The path to the sorted file to search in.
Example output:
For instance, if we have a sorted file named ‘fruits.txt’ with the following contents:
Apple
Banana
Cherry
Grape
And we run the command look --ignore-case ba fruits.txt
, it will search for lines starting with ‘ba’ while ignoring the case and the output will be:
Banana
Conclusion:
The ’look’ command provides a simple way to search for lines in a sorted file. Whether you need to search for lines based on a specific prefix or want to perform a case-insensitive search, ’look’ can help you quickly find the desired lines.