GREP commands and their usages
Before understanding about GREP lets understand why we need this and how this is helping us by saving our time from manual works.
If I am looking for a particular string/specific pattern from a file or multiple files or a directory then we can’t go into each and every files and then search for that particular command, if we are doing it means a lot of time will be wasted as there are always huge files in VLSI we are working on. Here GREP command helps us. Log files and report files in VLSI will be too tough to handle if we don’t have GREP command. Let’s dig into this.
Global Regular Expression Print (GREP) is Linux command which helps us to search for a particular pattern. GREP is case sensitive.
General Syntax:
grep <Search Pattern> <File Name>
Let’s understand with examples.
If I want to print all the lines which contains create_clock from file1.sdc then we use below example.
grep create_clock file1.sdc
We can use multiple file at a time to search for the pattern.
grep create_clock file1.sdc file2.sdc file3.sdc file4.sdc
If we are in particular directory and need to search from each file in this directory, then use below command. Dot (.) represents the present working directory. If there are directories present in the present working, then grep searches into these directories as well.
grep create_clock
If you are looking for the count of particular pattern, then use below two types of command. “wc -l and -c” gives count of the pattern.
grep <Search Pattern> <File Name> | wc -l
grep <Search Pattern> <File Name> -c
If using grep command and looking for the search in all files and subdirectories present in current directory then use “-r” option along with grep, Where -r is recursive search method.
grep -r “create_clock”
One can go for cascaded grep command where first search string will be searched first and from them it will look for second search string.
grep -r “create_clock” . | grep -r “period”
If you are looking for the particular word to print rather than entire line, then use below command.
grep -r “create_clock” . -w
As we know grep command is case sensitive so when we search for particular string then it looks for same matching string, suppose we have upper and lower both cases present and need to grep out then use below command. The option “-i” ignores the cases.
grep -I “ create_clock “ file1.sdc
If you are looking for some pattern but wants to exclude a string from that particular line, then use -v.
grep -v “create_clock “ file1.sdc
If you are looking for particular pattern in a report file, then you can use direct grep command but if 1 or 2 or few lines back we are looking to print then we use “-B <number>” option. B stands for BEFORE here. For eg.
grep -B 2 “data arrival time” timing.rpt ##This command will give the line of 2 line above data arrival time.
Similar to above if you want to print few numbers of lines after the search pattern then use “-A <number>”. A stand for AFTER. For eg.
grep -A 3 “data arrival time” timing.rpt ##This command will give the line of 3 line above data arrival time.
Similar to “-B” and “-A” options if one is looking to print before and after lines then use “-C <number>” option.
grep -C 4 “data arrival time” timing.rpt ##This command will print the line of 2 line above and after data arrival time.
If you are looking to search in all the files and directories present in the current directory, then use “*” at the place of file_name. For eg.
grep -v “create_clock “ *
Many times, when we search for the string can be a sub-string of another string and will be printed while using the regular grep command, if one want to avoid the sub-string part then use “-w” option. For eg.
grep -w “create_clock” file1.sdc
Sometimes we need to look into the files where search pattern is present then we use “-l” (small L).
grep -l “create_clock” file1.sdc
If you are looking for the pattern count in a file or files or files present in sub-directories, then use “-c” option.
grep -c “create_clock” file1.sdc
EGREP
EGREP stands for Extended Global Regular Expression Print. This is same as “grep -E” command.
With grep command special characters like ( , ) , { , }, ?, +, | etc are treated as regular patterns but egrep looks at these characters as \(, \), \{, \}, \?, \+, \| which boosts the search pattern better. This is the reason I always prefer to use egrep with the grep options we have discussed above
ZGREP
Zgrep is the command we use when we need to search for a pattern from zip file. For eg.
zgrep -r “cell_area” . #To grep command from zip file.
Regular Expressions with GREP
There are regular expressions which we need sometimes to use with the grep commands.
#1. If looking for something at the starting, then use “^”.
#2. For a pattern at the end then use “$”.
egrep/grep -f file_with_patterns_to_grep
Can you add a list of commands like -r,-c,-C,-A and their usage ??
Also, what are these known as??