Saturday, July 28, 2012

Simple Parsing Scripts

-------------------------------------------------------------------
1) Simple perl script to extract ip address from ipconfig command:


#!/usr/bin/perl

use File::Find;
use File::Copy;
use File::Path;

system ("ipconfig > test.txt");

$filename = "test.txt";
$strToFind = "IPv4";

open(INFILE, $filename) || die "Cannot open file";

$matchFound = 0;

while ($line = )
{
   if ($line =~ /$strToFind/)
    {
      print "$line";
      $matchFound = 1;
    }
}

close(INFILE);

print "\n";

if ($matchFound eq 1)
{
    print "$strToFind was found: $matchFound \n";
}
else
{
    print "$strToFind not found in $filename \n";
}


-----> Simple Ruby Script to parse IP address from ipconfig command <------------


--------------------------------------------------------------------------------------
2) Simple VB Script to parse MAC address and ip address from ipconfig command:

' VBScript Source File 
'
' NAME: myIPfunc.vbs
'
' AUTHOR: Swati Kher
'
' COMMENT: How to parse IPConfig command with vbscript 
'======================================================

Option Explicit


wscript.echo "Mac Address = ["& GetMac() & "]"

wscript.quit(0)

' ********************************************************************************************
' ******** GetMac ********
' ********************************************************************************************

Function GetMac()

   Dim WshShell, oExec , physaddr, macaddr, ipaddr
   Dim iLocation, name, strInputFile, iLoc2, name2

   Dim FSO, objFile
   Dim strLine

   Set WshShell = CreateObject("WScript.Shell") 

   set oExec = WshShell.Exec("cmd.exe /c ipconfig /all")

   physaddr = oExec.StdOut.ReadAll()
   name    = "Physical Address"
   name2   = "IPv4 Address"
   macaddr = "tbd"
  

   iLocation = InStr (physaddr, name)
   iLoc2 = InStr(physaddr, name2)
   
   
   WScript.Echo iLocation
   
   if (iLocation) then

       macaddr = Right(physaddr, Len(physaddr)  - iLocation)
       ipaddr = Right(physaddr, Len(physaddr) - iLoc2)
       
       'WScript.Echo "First Pass: " & macaddr
       iLocation = InStr (1, macaddr, ":")
       iLoc2 = InStr(1, ipaddr, ":")
       
       macaddr = Right(macaddr, Len(macaddr) - iLocation)
       ipaddr = Right(ipaddr, Len(ipaddr) - iLoc2)
       
       Trim(macaddr)
       Trim(ipaddr)
       
       'WScript.Echo "Second : " & macaddr
       'macaddr = Right(macaddr, Len(macaddr) - 17)
       iLocation = InStr (1, macaddr, vbNewLine)
       iLoc2 = InStr(1, ipaddr, vbNewLine)
       
       macaddr = Left(macaddr, iLocation)
       ipaddr = Left(ipaddr, iLoc2)
       
       macaddr = replace(macaddr, "-", "")
       GetMac = macaddr
       'WScript.Echo "Mac address " & macaddr
       WScript.Echo "IP ADDR: " & ipaddr

   end If

   GetMac = macaddr
   set WshShell = Nothing
   Set oExec = Nothing
End Function

------------------------------------------------------
3)  C++ functions to check palindromes and matching strings:


bool isItPali(char *a)
{
int length, mid, j;
    bool match = true;


if (a==NULL)
{
cout<<"Got a NULL String!!"<
return false;
}

length = strlen(a)-1;
mid = length/2;
j = 0;

if (strlen(a)==1)
{
cout<<"Only 1 char!!"<
return true;
}

if (length<=0)
{
cout<<"String was blank!!"<
return false;
}


    while(j<= mid)
{
if (*(a+j) != *(a+length-j))
match = false;
j++;
}


  return match;
}


bool ExpressionMatch(char *a, char *b)
{
int lenA, lenB, i, j, indWld;
bool match = false, done = false;

lenA = strlen(a);
lenB = strlen(b);

i = j = indWld = 0;

    if ((lenA == 0) || (lenB == 0))
return false;


while (!done)
{
if (*(b + j) == '*')
{
j++;
indWld = j;
}
else if (*(b + j) == *(a+i))
{
match = true;
j++;
i++;
}
else if(*(b+j) != *(a+i))
{
i++;
match = false;
if (indWld == 0)
i = lenA;
else if (i< lenA)
j= indWld;
}
if (indWld == lenB)
{
done = true;
if (indWld == 1)
match = true;
}
if (i == lenA)
done = true;
}

return match;
}
-----------------------------------------------------------
4) Java code to compute string LCM

import java.util.*;
import java.util.regex.*;

public class LCMstr {

/**
* @param args
*/
public static String lcm(String a, String b) {
int c = a.length();
int d = b.length();
if (a.charAt(c-1)==b.charAt(0)) {
return(a.concat(b.substring(1))); 
}
if (c >= d) {
    if (a.contains(b)) return a;
}
if (d > c) {
if (b.contains(a)) return b;
}
return a.concat(b);
//System.out.println(a.charAt(0));
//System.out.println("len" + c + a.charAt(c-1));
   //return "err";
}
public static void main(String[] args) {

LCMstr l = new LCMstr();
        System.out.println( l.lcm("ab", "bacd"));
        System.out.println( l.lcm("abcc", "cd"));
        System.out.println( l.lcm("ab", "bad"));
        System.out.println( l.lcm("aba", "abacd"));
        System.out.println(l.lcm("x1", "1x"));
        System.out.println(l.lcm("swati", "1swati"));
        System.out.println(l.lcm("swati1", "swati"));
        System.out.println(l.lcm("x1", "x2"));
        System.out.println(l.lcm("ab", "ab"));
}

}
--------------------------------------------------------
5) java code to compute LCM of two numbers:

public class myLCM {
private void calculateLCM(int x, int y) {

        int lcm = x*y/getGCD(x, y);

        System.out.println(lcm);

 }


private int getGCD(int x, int y) {

       // Make sure, n1 is the bigger number

       int n1 = x > y ? x : y;

       int n2 = x > y ? y : x;

       while(n1%n2 != 0) {

              int remainder = n1%n2;

              n1 = n2;

              n2 = remainder;
       }

       return n2;

}


    public static void main(String[] argv) {

        myLCM x = new myLCM();
        
        x.calculateLCM(2, 3);
        x.calculateLCM(8, 16);
        x.calculateLCM(3,15);
        //x.calculateLCM(10,0);
        x.calculateLCM(11,12);
        x.calculateLCM(15, 20);
        x.calculateLCM(17, 51);

 }
}
---------------------------------------------




No comments: