Check whether length of two array is same or not in Linux

In this blog, sharing code to check whether length of two arrays is same or not. arr=(4 3 2 1) ## First Array of 4 elementsarr_2=(4 3 1 10 1) ## Second Array of 5 elementslen_arr=${#arr[@]}len_arr_2=${#arr_2[@]}if [ $len_arr -eq $len_arr_2 ]thenecho “Length of both Array is same”fiif [ $len_arr -ne $len_arr_2 ]thenecho “Length of both Array is not same”fi

SORT ELEMENTS OF AN ARRAY IN LINUX

In this Blog, sharing code to sort the elements of an array in ascending and descending order as well.ASCENDING ORDER arr=(4 3 2 1) ## Array of 4 Elementslen=${#arr[@]} ## It gives the length of an Array arrk=0lngthminusone=$(( $len – 1))while [ $k -lt $len ] ##Outer Loop which will run len-1 times where len is length of Arraydoi=0 ## This variable set to 0 every time inner loop startswhile [ $i -lt $lngthminusone ] ## Inner loop which will arrange an element of an array in ascending order. Outer…

Reverse elements of an Array in Linux

In this blog, sharing code to reverse the array elements. arr=(1 2 3 4) ## Array of 4 Elementsarr_2=() ## Another Array of zero Elements or you can say blank Arraylen=${#arr[@]} ## It gives the length of an Array arrwhile [ $len -gt “0” ]dolen=$(( $len – 1))arr_2+=(${arr[len]})doneecho ${arr_2[*]} ##It will print (4 3 2 1)

Append elements of an Array to another Array

In this blog, sharing code of appending elements of one Array to another. arr=(1 2 3 4) ## Array of 4 Elementsarr_2=(5 10 12 1) ## Another Array of 4 Elementsi=0len=${#arr_2[@]} ## It gives the length of an Array arr_2while [ $i -lt $len ]doarr+=(${arr_2[i]}) ## Appending elements of arr_2 into array arr.i=$(( $i + 1))doneecho ${arr[*]}

Sum of all Array Elements in Linux

In this Blog, Sharing the code to add all elements of an Array using shell scripting. a =(1 2 3 4) ## Array of 4 Elementsi=0len=${#a[@]} ## It gives the length of an Arraysum=0 ## Setting sum variable to value 0while [ $i -lt $len ]dosum=$((${a[i]}+$sum))i=$(( $i + 1))doneecho $sum

Search Element In An Array in Linux

In this blog, Sharing the code to Search an Element in an Array using Shell scripting. a =(1 2 3 4) ## Array of 4 Elementsi=0x=2 ##x is the element which need to be searched in an Arraylen=${#a[@]} ## It gives the length of an Arraylen1=$(( $len – 1 )) ##len1 is used to display if element is not present in an array.while [ $i -lt $len ]doif [[ ${a[i]} -eq $x ]]thenecho $x is presentbreakfiif [[ $i -eq $len1 ]]then echo $x is not presentfii=$(( $i + 1))done

Generate GBs of file using one small file in linux

In this blog, I will share the shell script which will generate/create a big file (GBs of Data). Consider a small file which has very less records (say 3 records). See the below SS of the sample Data. Using above file we will write code to append data of this file to another file multiple times which results in a big output file. echo “COL1|COL2″ > linux_copy_.txtfor i in {1..100}docat linux.txt |tail -n+2 >> linux_copy.txtdone First line of code is about creating another file “linux_copy.txt” and adding header to this…

Perform Arithmetic Operations On Two Files

In this Blog , we will cover how to apply sum ,multiply or subtract operator on two files each having one field and same number of records. Consider the data in each file as below. Below is the Shell script which will multiply row by row from above two files and put the output in another file. arr1=()for line in cat linux.txt | tail -n+2doarr1+=(“$line”)donearr1_1=()for line in cat linux_1.txt | tail -n+2doarr1_1+=(“$line”)doneLenOfArr= ${#arr1[@]}touch newfile.txtecho “COL1” > newfile.txta=0while [ $a -le $((LenOfArr-1)) ]; do let ans=${arr1[a]}*${arr1_1[a]}; echo $ans >> newfile.txt; a=$((…

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 fileWindows 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 ‘|’ |…

Difference Between PowerShell and Shell Scripting – PART2

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 5 more different commands and will show the difference between them.Consider one file in windows and one file in Linux with same Data. Clear the content of a fileWindows use “Clear-Content”Clear-Content .\PowerShell_Scripting.txt Linux uses “>” symbol to truncate or removing content of a file.>Linuxfile.txt Copy a file from one Location to anotherWindows uses “Copy-Item” command to remove…