Get-Content largefile.txt -totalcount 1
http://windows-powershell-scripts.blogspot.com/2009/06/awk-equivalent-in-windows-powershell.html
ps: Get-Content .\test.csv | %{ $_.Split(',')[1]; }
awk:cat test.csv | awk -F, '{print $2}'
cat test.csv | awk -F, '{total+=$3} END {print "Total: "total}'
Get-Content .\test.csv | %{ $_.Split(',')[1]; }
cat test.csv | awk -F, '{print "No of fields in record "$1" = "NF }
Get-Content .\test.csv | %{ $a=$_.Split(','); Write-Host "No of fields in record"$a[0]"="$a.length; }
cat test.csv | awk -F, '{if ($NF ~ "[a-c]") print}'
Get-Content .\test.csv | %{ if ($_.Split(',')[-1] -match "[a-c]") { $_; } }
cat test.csv | awk -F, '{total+=$3} END {print "Total: "total}'
Linux grep equivalent: select-string
select-string -pattern “somestring” foo.txt | foreach { $_.ToString().split(” “)[2] }
ForEach-Object
http://technet.microsoft.com/en-us/library/hh849731.aspx
Parameter Set: ScriptBlockSet
ForEach-Object [-Process] <ScriptBlock[]> [-Begin <ScriptBlock> ] [-End <ScriptBlock> ] [-InputObject <PSObject> ] [-RemainingScripts <ScriptBlock[]> ] [-Confirm] [-WhatIf] [ <CommonParameters>]
Parameter Set: PropertyAndMethodSet
ForEach-Object [-MemberName] <String> [-ArgumentList <Object[]> ] [-InputObject <PSObject> ] [-Confirm] [-WhatIf] [ <CommonParameters>]
Script block. You can use a script block to specify the operation. Within the script block, use the $_ variable to represent the current object. The script block is the value of the Process parameter. The script block can contain any Windows PowerShell script.
For example, the following command gets the value of the ProcessName property of each process on the computer.
Get-Process | ForEach-Object {$_.ProcessName}
30000, 56798, 12432 | ForEach-Object -Process {$_/1024}
Get-ChildItem $pshome | ForEach-Object -Process {if (!$_.PSIsContainer) {$_.Name; $_.Length / 1024; "" }}
PS C:\> $Events = Get-EventLog -LogName System -Newest 1000
PS C:\>$events | ForEach-Object -Begin {Get-Date} -Process {Out-File -Filepath Events.txt -Append -InputObject $_.Message} -End {Get-Date}
Because Windows PowerShell treats null as an explicit placeholder, the ForEach-Object cmdlet generates a value for $null, just as it does for other objects that you pipe to it.
1, 2, $null, 4 | ForEach-Object {"Hello"}
Get-Module -List | ForEach-Object -MemberName Path
"Microsoft.PowerShell.Core", "Microsoft.PowerShell.Host" | ForEach-Object {$_.Split(".")}
PS C:\>"Microsoft.PowerShell.Core", "Microsoft.PowerShell.Host" | ForEach-Object -MemberName Split -ArgumentList "."
PS C:\>"Microsoft.PowerShell.Core", "Microsoft.PowerShell.Host" | Foreach Split "."
http://ss64.com/ps/foreach-object.html
get-childitem C:\ | foreach-object -process { $_.length / 1024 }
ForEach (loop statement)
$trees = @("Alder","Ash","Birch","Cedar","Chestnut","Elm")
foreach ($tree in $trees) {
"$tree = " + $tree.length
}
foreach ($num in 1,2,3,4,5) {
if ($num -eq 2) { continue } ; $num
}
Loop through a collection of .txt files:
foreach ($file in get-ChildItem *.txt) {
Echo $file.name
}
cat test.txt | foreach-object {$null = $_ -match '<FancyPants>(?<x>.*)<.FancyPants>'; $matches.x}
http://www.hanselman.com/blog/UnixFightSedGrepAwkCutAndPullingGroupsOutOfAPowerShellRegularExpressionCapture.aspx
cat test.txt | foreach-object {$null = $_ -match '<FancyPants>(?<x>.*)<.FancyPants>'; $matches.x} | sort | get-unique
cat test.txt | % {$null = $_ -match '<FancyPants>(?<x>.*)<.FancyPants>';$matches.x} | sort | gu
http://technet.microsoft.com/en-us/library/hh849731.aspx
Get-Process | ForEach-Object {$_.ProcessName}
Split is just one of many useful methods of strings. To see all of the properties and methods of strings, pipe a string to the Get-Member cmdlet.
Get-Content .\1c* -TotalCount 10 | %{ if($_.StartsWith("INFO")) {a=$_.Split()[3]; $a.Substring(0,$a.Length-1); }
Get-Content .\1c* -TotalCount 10 | %{ if($_.StartsWith("INFO")) {$_.Split()[3];} else {$_} }
Get-Content .\1c* -TotalCount 10 | %{ if $_.Split()[3]; }
http://ss64.com/ps/if.html
if (condition) {commands_to_execute}
[ elseif (condition2) {commands_to_execute} ]
else {commands_to_execute}
Part-4: Text Manipulation in PowerShell using .Replace(), and .Remove() methods.
http://newdelhipowershellusergroup.blogspot.com/2012/02/part-4-text-manipulation-in-powershell.html
$text.Replace("."," ")
The syntax for remove method is to define to keep the number of characters and remove everything else.
$text.Remove(4)
$text.Remove(4,7)
$string = $string.Substring(0,$string.Length-1)
http://windows-powershell-scripts.blogspot.com/2009/06/awk-equivalent-in-windows-powershell.html
ps: Get-Content .\test.csv | %{ $_.Split(',')[1]; }
awk:cat test.csv | awk -F, '{print $2}'
cat test.csv | awk -F, '{total+=$3} END {print "Total: "total}'
Get-Content .\test.csv | %{ $_.Split(',')[1]; }
cat test.csv | awk -F, '{print "No of fields in record "$1" = "NF }
Get-Content .\test.csv | %{ $a=$_.Split(','); Write-Host "No of fields in record"$a[0]"="$a.length; }
cat test.csv | awk -F, '{if ($NF ~ "[a-c]") print}'
Get-Content .\test.csv | %{ if ($_.Split(',')[-1] -match "[a-c]") { $_; } }
cat test.csv | awk -F, '{total+=$3} END {print "Total: "total}'
Linux grep equivalent: select-string
select-string -pattern “somestring” foo.txt | foreach { $_.ToString().split(” “)[2] }
ForEach-Object
http://technet.microsoft.com/en-us/library/hh849731.aspx
Parameter Set: ScriptBlockSet
ForEach-Object [-Process] <ScriptBlock[]> [-Begin <ScriptBlock> ] [-End <ScriptBlock> ] [-InputObject <PSObject> ] [-RemainingScripts <ScriptBlock[]> ] [-Confirm] [-WhatIf] [ <CommonParameters>]
Parameter Set: PropertyAndMethodSet
ForEach-Object [-MemberName] <String> [-ArgumentList <Object[]> ] [-InputObject <PSObject> ] [-Confirm] [-WhatIf] [ <CommonParameters>]
Script block. You can use a script block to specify the operation. Within the script block, use the $_ variable to represent the current object. The script block is the value of the Process parameter. The script block can contain any Windows PowerShell script.
For example, the following command gets the value of the ProcessName property of each process on the computer.
Get-Process | ForEach-Object {$_.ProcessName}
30000, 56798, 12432 | ForEach-Object -Process {$_/1024}
Get-ChildItem $pshome | ForEach-Object -Process {if (!$_.PSIsContainer) {$_.Name; $_.Length / 1024; "" }}
PS C:\> $Events = Get-EventLog -LogName System -Newest 1000
PS C:\>$events | ForEach-Object -Begin {Get-Date} -Process {Out-File -Filepath Events.txt -Append -InputObject $_.Message} -End {Get-Date}
Because Windows PowerShell treats null as an explicit placeholder, the ForEach-Object cmdlet generates a value for $null, just as it does for other objects that you pipe to it.
1, 2, $null, 4 | ForEach-Object {"Hello"}
Get-Module -List | ForEach-Object -MemberName Path
"Microsoft.PowerShell.Core", "Microsoft.PowerShell.Host" | ForEach-Object {$_.Split(".")}
PS C:\>"Microsoft.PowerShell.Core", "Microsoft.PowerShell.Host" | ForEach-Object -MemberName Split -ArgumentList "."
PS C:\>"Microsoft.PowerShell.Core", "Microsoft.PowerShell.Host" | Foreach Split "."
http://ss64.com/ps/foreach-object.html
get-childitem C:\ | foreach-object -process { $_.length / 1024 }
ForEach (loop statement)
$trees = @("Alder","Ash","Birch","Cedar","Chestnut","Elm")
foreach ($tree in $trees) {
"$tree = " + $tree.length
}
foreach ($num in 1,2,3,4,5) {
if ($num -eq 2) { continue } ; $num
}
Loop through a collection of .txt files:
foreach ($file in get-ChildItem *.txt) {
Echo $file.name
}
cat test.txt | foreach-object {$null = $_ -match '<FancyPants>(?<x>.*)<.FancyPants>'; $matches.x}
http://www.hanselman.com/blog/UnixFightSedGrepAwkCutAndPullingGroupsOutOfAPowerShellRegularExpressionCapture.aspx
cat test.txt | foreach-object {$null = $_ -match '<FancyPants>(?<x>.*)<.FancyPants>'; $matches.x} | sort | get-unique
cat test.txt | % {$null = $_ -match '<FancyPants>(?<x>.*)<.FancyPants>';$matches.x} | sort | gu
http://technet.microsoft.com/en-us/library/hh849731.aspx
Get-Process | ForEach-Object {$_.ProcessName}
Split is just one of many useful methods of strings. To see all of the properties and methods of strings, pipe a string to the Get-Member cmdlet.
Get-Content .\1c* -TotalCount 10 | %{ if($_.StartsWith("INFO")) {a=$_.Split()[3]; $a.Substring(0,$a.Length-1); }
Get-Content .\1c* -TotalCount 10 | %{ if($_.StartsWith("INFO")) {$_.Split()[3];} else {$_} }
Get-Content .\1c* -TotalCount 10 | %{ if $_.Split()[3]; }
http://ss64.com/ps/if.html
if (condition) {commands_to_execute}
[ elseif (condition2) {commands_to_execute} ]
else {commands_to_execute}
Part-4: Text Manipulation in PowerShell using .Replace(), and .Remove() methods.
http://newdelhipowershellusergroup.blogspot.com/2012/02/part-4-text-manipulation-in-powershell.html
$text.Replace("."," ")
The syntax for remove method is to define to keep the number of characters and remove everything else.
$text.Remove(4)
$text.Remove(4,7)
$string = $string.Substring(0,$string.Length-1)
No comments:
Post a Comment