Thursday, February 14, 2019

Powershell: Sum of a column by grouping



ClientName    TaxReturnTypeName     Volume

client1             ReturnEI                            10
client2             ReturnEI                            20
client3             ReturnEI                            30
client1             ReturnEC                           15
client2             ReturnEC                           20
client2             ReturnEP                            70
client3             ReturnET                            90


Expected Output:

Type           Sum
ReturnEI       60
ReturnEC     35
ReturnEP      70
ReturnET      90


$file = "c:\test\ReportVolumes.csv" # here goes the path and name of the excel file.

$report = import-csv $file

# $report | Group-Object taxreturntypename |
#    Select-Object @{n='Type';e={$_.Group[0].taxreturntypename}},
#                  @{n='Volume';e={$_.Group[0].Volume}}


$Result = ForEach ($Type in ($report | Group taxreturntypename))
{   [PSCustomObject]@{
        Type = $Type.Name
        Sum = ($Type.Group | Measure-Object Volume -Sum).Sum
    }
}

$Result | Export-Csv "C:\test\out.csv"

Tuesday, February 12, 2019

Memcached

Memcached is an open source caching system for distributed caching used to optimize the performance of a page for fast accessibility.
It reduces the load on server by storing the frequent database calls result sets.

Memcached was developed by Brad Fitzpatrick for LiveJournal in 2003.

Benefits:

1. Open source
2. Can be implemented by various languages - PHP, .NET, JAVA
3. Cross platform. So can be implemented in Windows, Linux
4. Memcached is distributed - crucially this means that if I have a cluster of servers accessing the cache, all of them are essentially reading from and writing to the same cach


Which companies use it:

Netlog, Facebook, Flickr, Wikipedia, Twitter, and YouTube among others

See the stackshare:
https://stackshare.io/memcached


Other competitors:
There are a couple of In-Memory distributed cache engines such as Velocity, NCache and ScaleOut.

The other tool used for caching is Varnish.
Varnish is bit different to Memcache. Rather than storing data for a request, Varnish stores complete page. However Varnish is an expensive but worth option when you have lots of traffic coming to your page. 
Wikipedia uses Varnish for page caching as the content of the page is not changed so frequently.
For further information visit https://varnish-cache.org?ref=vikask


Productivity Tip - Paper clip strategy

Paper Clip Strategy

Why:


One of the famous strategy to achieve your goals.
This helps to give you a daily target


A full of paper clips. When you finish a task, you put a clip back to jar
the real goal is to finish all the clips and put them all back to jar



Monday, February 11, 2019

Powershell : delete desired columns from XLSX


$file = "c:\test\book4.xlsx" # here goes the path and name of the excel file.
$ColumnsToKeep = 1,4,6 # Specify the column numbers to delete.

# Create the com object
$excel = New-Object -comobject Excel.Application # Creating object of excel in powershell.
$excel.DisplayAlerts = $False
$excel.visible = $False

# Open the XLSX File
$workbook = $excel.Workbooks.Open($file)
$sheet = $workbook.Sheets.Item(1) # Referring to first sheet.

# Determine the number of rows in use
$maxColumns = $sheet.UsedRange.Columns.Count

$ColumnsToRemove = Compare-Object $ColumnsToKeep (1..$maxColumns) | Where-Object{$_.SideIndicator -eq "=>"} | Select-Object -ExpandProperty InputObject
0..($ColumnsToRemove.Count - 1) | %{$ColumnsToRemove[$_] = $ColumnsToRemove[$_] - $_}
$ColumnsToRemove  | ForEach-Object{
    [void]$sheet.Cells.Item(1,$_).EntireColumn.Delete()
}

# Save the edited file
$workbook.SaveAs("C:\test\newfile.csv")

# Close excel and release the com object.
$workbook.Close($true)
$excel.Quit()
[void][System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
Remove-Variable excel 

Powershell - scrits are disabled. How to enable?

Set-ExecutionPolicy Unrestricted