Tuesday, March 1, 2022

count csv lines in files of a folder power shell

 

PS C:\> (Get-ChildItem -Recurse -File | Get-Content | Measure-Object -line).Lines

39597572

PS C:\> dir -Include *.csv -Recurse |

>>    % { $_ | select name, @{n="lines";e={

>>        get-content $_ |

>>          measure-object -line |

>>              select -expa lines }

>>                                        }

>>      } | ft -AutoSize


Wednesday, February 9, 2022

Longest Common Prefix

function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "".
       
       
       

class Solution {
   public static void main(String[] args) {
    System.out.println("Should be (fl). ="+new Solution().longestCommonPrefix(new String[]{"flower","flow","flight"}));
    System.out.println("Should be (""). ="+new Solution().longestCommonPrefix(new String[]{"box","wow","cat"})); 
  }
    
    public String longestCommonPrefix(String[] strs) {
        String firstString = strs[0];
        String commonPrefix="";
      boolean common=true;
        for(int j=0; j

Roman to decimal conversion

       
       
class Solution {
   public static void main(String[] args) {
    System.out.println("Should be (3). ="+new Solution().romanToInt("III"));
     
      System.out.println("Should be (58). ="+new Solution().romanToInt("LVIII"));
      System.out.println("Should be (1994). ="+new Solution().romanToInt("MCMXCIV"));
  }
    public int romanToInt(String s) {
        Map romanValues = new HashMap();
        romanValues.put("I", "1");
        romanValues.put("V", "5");
        romanValues.put("X", "10");
        romanValues.put("L", "50");
        romanValues.put("C", "100");
        romanValues.put("D", "500");
        romanValues.put("M", "1000");
        String romanNum = s;
        int intValue=0;
        int prevValue=0;
        for(int i=0; i< s.length(); i++){
            String firstChar = romanNum.substring(0,1);
            if(firstChar==null)
                continue;
           System.out.println("firstChar= "+firstChar);
            
            int romanIntValue= Integer.valueOf((String)romanValues.get(firstChar));
           System.out.println("romanIntValue= "+romanIntValue);
          if(romanIntValue > prevValue){
            romanIntValue -=prevValue;
            intValue -=prevValue;
          }
            
           prevValue=romanIntValue;
            intValue +=romanIntValue;
           System.out.println("intValue= "+intValue);
          
          romanNum = romanNum.substring(1, romanNum.length());
           System.out.println("romanNum= "+romanNum);
        }
        return intValue;
    }
}
       

Polindrome

 Given an integer x, return true if x is palindrome integer.

An integer is a palindrome when it reads the same backward as forward.

  • For example, 121 is a palindrome while 123 is not.

class Polindrome {
    public boolean isPalindrome(int x) {
        int given =x;
        int reverse=0;
        while(given >0){
            reverse = 10*reverse +given%10;
            given = given/10;
        }
        if(reverse == x)
        return true;
        return false;
    }
}




This one only goes through half the elements


class Palindrome {
    public boolean isPalindrome(int x) {

// negative numbers are not a palindrome

//any number with zero in units. but not Zero by itself 

     if(x<0  || (x % 10 == 0 && x != 0) )
         return false;
    int numDigits = String.valueOf(x).length();
        int half = numDigits/2;
            int rev=0;
            int given = x;
        for(int i=0;i<half;i++){
            rev = 10*rev+given%10;
            given=given/10;
        }
        if(numDigits%2 != 0)
            given = given/10;
            if(rev==given)
                return true;
       return false;
    }
}

Sunday, December 27, 2020

power shell script to export only some columns from csv file

 


This power shell script goes through the csv file and exports the column into a filename+out.csv



 Get-ChildItem -Path . -Recurse -Filter '*.csv' |

   ForEach{

   $out  = $PSItem.Name+"_out.csv"

       $PSItem |

       Select-Object -Property FullName,

       @{Name = 'FirstLineOfFile';Expression = {Get-Content -Path $PSItem.FullName -First 1}}

   Import-Csv $PSItem.FullName   | select Phone,HomePhone,Home_Phone,Phone_1,'Phone Home',PhoneNumber,HomePhoneNumber,'Phone Number'| Export-Csv -Path $out -NoTypeInformation

   }



Power shell script Save the csv headers from the file


-Recurse recurses through folders.. and   write to Out_Headers.txt 


 Get-ChildItem -Path . -Recurse -Filter '*.csv' |

ForEach{

     $PSItem |

     Select-Object -Property FullName,

     @{Name = 'FirstLineOfFile';Expression = {Get-Content -Path $PSItem.FullName -First 1}}

 } |Out-File Out_Headers.txt -Append


Monday, September 14, 2020

Restarting apache2 (via systemctl): apache2.serviceJob for apache2.service failed because the control process exited with error code.

 



sudo /etc/init.d/apache2 restart    not starting



There is some syntax error in the file apache2.conf.

In a terminal, type:

cd /etc/apache2

Then:

apache2ctl configtest

Cannot load modules/mod_rewrite.so into server: /etc/apache2/modules/mod_rewrite.so: cannot open shared object file: No such file or directory


Enabling mod_rewrite

Now, we need to activate mod_rewrite.

  • sudo a2enmod rewrite

This will activate the module or alert you that the module is already in effect. To put these changes into effect, restart Apache.

  • sudo service apache2 restart


for 

LoadModule vhost_alias_module /usr/lib/apache2/modules/mod_vhost_alias.so
sudo a2enmod vhost_alias