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…

Difference between Unix and PowerShell Commands

In this Blog, I will demonstrate the difference between Unix and PowerShell Commands. It will help all coders who works on Scripting Languages.Consider the one file in windows and one file in Linux with same data.See the below Image for the same. Find the Total Number of Records in a file . Linux uses “cat” and “wc -l” commandcat LinuxScripting.txt | wc -l Windows “Get-Content” and “Measure-Object” command. $File_count=Get-Content PowerShell_Scripting.txt |Measure-Object Read first n lines of a file Linux uses “cat” and “head” command.cat LinuxScripting.txt | head -3 Windows uses…

Examples of Awk Command

In this blog, I will explain various situations where you can use AWK command.Scenario 1: Identify the occurrence of a particular delimiter present in a variable.For example , consider a variable v_dem=”v1|100|v2|200|v3|300″Use Awk command , you can easily find the occurrence of Pipe delimiter in v_dem variable. Here, NF stands for Number of fields . LINUX COMMAND : echo $v_dem |awk -F ‘|’ ‘{print NF} ‘ Scenario 2: Print the values separated by Delimiter(Pipe in this case) using AWK command.LINUX COMMAND : echo $v_dem | awk -F “|” ‘ {for…

Check duplicate record in Hive

Today, I will discuss about ” How to automate the process where in you can check entire row duplicate record in hive”. As I have mentioned in all Automation blogs, I will share the pseudo code.STEP1: In hive , use “desc table_name” , this command will give you column names along with datatype and data length. Store the output of this command in a file , say HIVE_TABLE_DDL.txt STEP2 : Read the file HIVE_TABLE_DDL.txt using “cat” command. cat HIVE_TABLE_DDL.txt | awk ‘{print $1}’ ORS=’,’ | sed ‘s/,$//’ * awk'{print $1}’…

shell script to check whether logs have errors or not

Today, I will discuss about “how to check logs files at regular interval and identify if any errors are there or not and if yes, then send email to the concerned team members. As mentioned in my previous blogs , I will be sharing pseudo code only. STEP1 : Create a parameterized file which has all the details of log file names. For example, consider filename is (logs.config) and below is the content of the fileUSECASE_NAME|SERVER_LOGNAME_PATTERNRECHARGE_USSD|rechargeUssdMode STEP2 : Create the shell script in such a way that when you trigger…