Showing posts with label Unix. Show all posts
Showing posts with label Unix. Show all posts

Wednesday, June 16, 2021

Windows Grep Equivalent – findstr Examples for Grep Command

1. Overview

In this tutorial, We'll learn how to use the unix grep command in windows to search the files efficiently and what is the windows grep equivalent tool or command.

When you are using windows machine, many times you might have encountered the situations to search a piece of text in the windows machine.

After doing research, we found that there is a tool called findstr for windows operating system which matches to the grep command.

Let us explore the different type of search using findstr command.

Grep for Windows – findstr Examples for Grep Command


2. By Filtering the Results


First, use the ls command and filter the results using grep command.

// example of unix grep command to filter
ls -l | grep javaprogramto

// windows equivalent command
dir | findstr javaprogramto

We can pass the multiple search patterns to the grep and findstr command.

unix grep: Use -E flag with grep command to pass multiple patterns and delimited by | pipe symbol. This pattern should be enclosed in double quotes.
windows findstr: Just need to pass the space separator to findstr command and keep the patterns in double quotes.

// example of unix grep command to filter
ls -l | grep -iE "javaprogramto|kotlin"

// windows equivalent command
dir | findstr "javaprogramto kotlin"

3. By Searching the Files


Let see now how to find the matches the in file for a specific pattern.

Below example is to find the match and second one is for get the matches count.

#Example 1
#Linux - find the matches
$ grep javaprogramto input.txt

#Windows - find the matches
c:\> findstr javaprogramto input.txt

#Example 2
#Linux - count the matches
$ grep -c javaprogramto input.txt

#Windows - count the matches
c:\> findstr -N javaprogramto input.txt


unix: -c flag works with grep command to get the count for the matched records
windows: -N flag for findstr

4. By Searching List Of Files


Next, search for a files names which has the given pattern in the given folder.

#Example 1
#Linux - search in folders
$ grep javaprogramto  -lr /path/folder

#Windows - search in folder files
c:\> findstr /M javaprogramto c:\folder\*


5. Help Command


We can get the help command for grep and findstr.

#Example to get help from provider
$ grep --help 
$ man grep 

#Windows
c:\> findstr -?

6. Conclusion


In this article, We've seen how to search files in windows just like unix grep command and shown the examples.


Monday, March 23, 2020

Grep - Print N Lines Before and After Match: Unix Command

1. Grep Overview

In this tutorial, We'll learn how to print before and after grep command results content for each match in UNIX.

In my work some days back, I was using a UNIX grep command on the log file. My job is to find the exceptions in the log file and get the complete exception stack trace. But when we do the following command returns only the line that matches the Exception word.

grep 'Exception' apllication.log


After doing research on grep command, found useful flags that are very helpful. Before that just see the useful use case.

Print Before and After Lines Using Grep

Tuesday, April 30, 2019

How to run unix/shell command in java like chmod, mkdir, grep or any unix commands

Running shell command in Java

In this tutorial, We are going to learn how to run shell commands from java such as chmod, mkdir, grep files.

run-shell-command-in-java

Find operating system:

Before running the command, we must know the operating system where the application is running.

By calling System.getProperty("os.name") method, we will get the OS name.

private String OS = System.getProperty("os.name").toLowerCase();

This statement, may return the following operating system names.

Linux, Windows, SunOS, Mac OS, HP-UX and AIX