Monday, July 11, 2016

Matching a pattern in a file and Deleting a set of lines before that pattern using SED and AWK

        Consider a scenario where we are having a huge number of lines in a text file. We want to do some pattern matching and need to delete that pattern matched line as well as some lines immediately preceding that line. In this case we can follow the below steps to achieve that using SED & AWK shell commands.

Step 1: Reverse the order of the lines of the file.
This reversal is required as we are going to use the “sed” to do pattern matching and deletion of lines. As “sed” doesn’t back track, once a line is processed it is done. We cannot delete the lines preceding the pattern matched line. By reversing the order of the lines, we can easily delete the lines following the pattern matched line instead of preceding lines.

Step 2: Use the “sed” to do pattern matching and to delete the required lines.
                In this operation, for each line deleted a blank line will get generated.

Step 3: Using “awk” we can remove the empty lines introduced due to the operation performed in step 2.

Step 4: Again reverse the order of the lines of the file so that we can get the original file with unwanted lines removed.


Steps
Command
Purpose of command
Step 1
tac input_file  > output_file1
Reversing the order of line in input file and redirecting the output to another file “output_file1”
Step 2
sed –e ‘/<pattern_to_match>/,+nd’ output_file1 > output_file2

Where n = number of lines to delete following the pattern matched line.
This command will delete the pattern matched line and ‘n’ number of lines following that in the file “output_file1”. The output is redirected to the file “output_file2”
Step 3
awk NF output_file2 > output_file3
This command will remove the blank lines from “output_file2” and output is getting redirected to “output_file3”
Step 4
tac output_file3 > output_file4
Reversing the order of lines in the file “output_file3” so that we can get the original file “input_file” with unwanted lines removed.



Illustrative Example:

Input File:

Objective: In this file, we need to remove the commands which are getting failed along with its failure message.

line 1

 mac-address-table static unicast 00:00:00:00:08:01 vlan 10 interface extreme-ethernet 0/10

 mac-address-table static unicast 00:00:00:00:08:02 vlan 10 interface extreme-ethernet 0/10

 mac-address-table static unicast 00:00:00:00:08:03 vlan 10 interface extreme-ethernet 0/10

 mac-address-table static unicast 00:00:00:00:08:04 vlan 10 interface extreme-ethernet 0/10

 mac-address-table static unicast 00:00:00:00:08:05 vlan 10 interface extreme-ethernet 0/10

 mac-address-table static unicast 00:00:00:00:08:06 vlan 10 interface extreme-ethernet 0/10

Thursday, May 26, 2016

How to exclude some files in "grep"


        When we are searching for a string in a list of files using "grep" command, we may want to exclude some of the files from grep. We can achieve that with the help of the "--exclude" option in the grep command.

Example:

        For example, we are having text files name x, y, z and we want to search for "string" in these files except z. Then we can exclude the file z from the grep as given below.

        grep -nir "string" * --exclude z

        This command will search for the token "string" in all the files except file "z".

Excluding Multiple Files:

       To exclude multiple files from grepping, we can specify the command,

        grep -nir "string"  * --exclude file1 --exclude file2

Thursday, April 14, 2016

How to fix the “deadcode” type coverity warnings in C code

    Coverity is a static analysis tool used to analyze and detect the defects in our coding. Coverity tool finds the logical errors also present in the code.


Deadcode:
                It is a type of error reported by the coverity tool. A part of code is marked as “dead” if that part of code is never getting executed. Please find below the illustration for the same.

Coverity “Deadcode” Example:

void SetRowStatus (int RowStatus)
{
                if ((RowStatus < 1) || (RowStatus > 6))
                {
                                return;
                }
                switch RowStatus:
                {
                                case 1:
                                                ……
                                                ……
                                                break;
                                case 2:
                                                ……
                                                ……
                                                break;
                                case 3:
                                                ……
                                                ……
                                                break;
                                case 4:
                                                ……
                                                ……
                                                break;
                                case 5:
                                                ……
                                                ……
                                                break;
                                case 6:
                                                ……
                                                ……
                                                break;
                                default:
                                                ……                         ----> DEADCODE
                                                return;

                }
                return;
}

    In the above code snippet, the "RowStatus" variable takes on the value of 1 to 6. If the "RowStatus" value does not lie in the range of 1 to 6, the function would return in the “if” check itself. So, it is deterministic that the “default” case would never get executed in the switch block of code. So, the part of code inside the “default” case will be marked as “deadcode” by the coverity tool.

Resolving DeadCode Error:
                We might think that, removing the “default” case from the switch block of code would resolve the issue. But it will lead to the compiler warning (default case is missing). So, the correct approach is given below.

void SetRowStatus (int RowStatus)
{
                if ((RowStatus < 1) || (RowStatus > 6))
                {
                                return;
                }

Tuesday, April 5, 2016

Leonardo & the SubString - HackrRank Challenge


Leonardo & the SubString:

  • In this post we will solve the programming challenge of "Leonardo & the substring" matching posted in HackrRank.

Problem Description:

Leonardo loves puzzles involving strings, but he's just found a problem that has him stumped! Help him solve the following challenge:
Given a binary string, , composed of only 's and 's, find and print the total number of substrings of  which do not contain a  or .
Input Format
The first line contains an integer, (the number of test cases). 
The  subsequent lines of test cases each contain a string, , composed only of 's and 's.
Constraints
Output Format
For each test case, print the total number of substrings of  having no consecutive zeroes or ones (i.e.: not containing  or ).