DIFFERENCE BETWEEN POWERSHELL AND SHELL SCRIPTING – PART3

This blog is the continuation of my previous blog , where I have explained the difference between PowerShell and Shell Scripting through 5 different scenarios. Here , in this Blog , I will take few more commands and will show the difference between them. Consider one file in windows and one file in Linux with same Data.

Print selected column of a file
Windows use below commands.
Need to mention what all columns are required.
$headers=@(
"COL1"
"COL2"
)

Use import csv command to display data for above mentioned headers.
Import-Csv -Path .\PowerShell_Scripting.txt -Delimiter '|' | Select $headers

Linux uses Awk Command.
cat linux.txt | awk -F "|" '{print $1 "|" $2}'

Record Count in each file
Windows uses “Get-Content” and “get-Children”. Created same file with different name. See the below SS.


$GetFileNames= get-childitem -name "*.txt"
foreach ($a in $GetFileNames) { $a
(Get-Content $a).Length
}



Linux use “for” and “Awk” command to get total number of records in each file. Here, I have replicated same file twice. See the below SS.

Below command to use to fetch total count in each file.

for file in ls -lrt *.txt | awk '{print $NF}'; do fileCount=cat $file | wc -l ; echo "Name of the file: " $file " Record Count is :" $fileCount; done

Sum of Particular Field in each file & for Loop
Windows use “Measure-Object” to get sum of particular field.
$GetFileName=get-childitem -name "*.txt"
foreach ($a in $GetFileName) {$sum=Import-Csv $a -Delimiter '|' |Measure-Object 'COL3' -Sum
'FileName is '+$a + ' & sum of COL3 is : {0:n2}' -f $sum.Sum
}



Linux using “sum” and “Awk” commands. Here , It will sum up the third field COL3.

for file in ls -lrt *.txt | awk '{print $NF}'; do sum_col3=cat $file | awk -F "|" '{ sum += $3 } END { print sum }'; echo "Name of the file: " $file " Sum of COL3 is :" $sum_col3 ; done

Get Current Location or Path
Windows use Get-Location command.

Linux use “pwd” command.

Related posts