Searching in files
In this lesson, we will cover how to search the contents of files as well as what pipes are and how to use them.
To look for text within a file, use the
grep command. A typical format of this command is
grep followed by a search pattern and the file that you are searching for the pattern in.
grep <pattern> <file>
||Perform a case insensitive search (ignoring case).|
||Count the number of occurrences in a file.|
||Precede output with line numbers.|
||Invert Match. Print lines that do not match the search pattern.|
Let's look at the content of the file test.txt:
$ cat test.txt Hello world line 1 firstname: Tom lastname: South last line
To search this file with the
grep command: $ grep Tom test.txt firstname: Tom
To search all occurrences that match o:
$ grep o test.txt Hello world line 1 firstname: Tom lastname: South
$ grep -v o test.txt last line
It is matching case by default. To ignore case, use
$ grep Firstname test.txt $ grep -i Firstname test.txt firstname: Tom
You can also combine options, for example, combine
-c for count and
-i for ignore case. And
grep says there is only 1 line that matches Firstname.
$ grep -ci Firstname test.txt 1
-i will display which line number that the match occurs on.
$ grep -ni Firstname test.txt 2:firstname: Tom
There are some clues to guess what a file might contain. For example, some files will have an extension. If the file ends in .txt , it has large changes that it is a text file.
If a file has executable permissions set, it might be an executable programe. An easy way to determine the type of a file is to run the
file command against that file.
$ file <filename>
If it is a text file, it will say that is text in the output of the
$ file sales.data sales.data: ASCII text $ file * Applications: directory Documents: directory Downloads: directory Dropbox: directory Library: directory Pictures: directory Public: directory node_modules: directory test.txt ASCII text
Searching for text in Binary Files using
A binary file is a file that is in machine readable format and not a human readable format. If you run
grep against a binary file it will simply display whether or not that informaiton is found in the file, but it will not display the text.
To look at the textual data within a binary file, use the
The vertical bar command (
|) is called pipe. You can chain two commands together with a pipe.
Pipe takes the standard output from the preceding command and passes it as the standard input to the following command. If the first command displays error messages, those will not be passed to the second command by default. Error messages are displayed on standard error. If you want standard error to be passed in as input to the command following the pipe, you can redirect standard error to standard input.
cut command allows you to select portions of a file or to cut out pieces of a file.
- Dependency injection
- Directives and Pipes
- Data binding
- HTTP Get vs. Post
- Node.js is everywhere
- MongoDB root user
- Prefer Async Script Loading
- Components, Bootstrap and DOM
- What is HEAD in git?
- Show the changes in Git.
- What is AngularJS 2?
- Confidence Interval for a Population Mean
- Accuracy vs. Precision
- Sampling Distribution
- Working with the Normal Distribution
- Standardized score - Z score
- Evaluating the Normal Distribution
- What is Nodejs? Advantages and disadvantage?
- How do I debug Nodejs applications?
- Sync directory search using fs.readdirSync