This is the approach i needed.
Any other method to do that please suggest with all your simple logic i tried on my own to create script
I have two files
lets me tell you about first file :
file1.txt
The first file contain only one single line data [only one record]
The data is :
1234|23.23|45.34|34.34|2019-23-19|12:00:90
The data from file1.txt - indicates like below
value in position[2] = sum of SAL
value in position[3] = sum of COL
value in position[4] = sum of PER
Now lets talk about second file. Which is file2.txt
The second file contain below data
ID|SAL|COL|PER|TAG|GER
1"|"0.04"|"8.08"|"16.16"|"2.2"|D"
2"|"0.04"|"8.08"|"16.16"|"2.34"|"B"
3"|"0.04"|"8.08"|"16.16"|"2.34"|"A"
Note all need to be generic code and that to function so that i should work with any scenario.
file1.txt logic imolemented
myscriptfile1.sh file1.txt '2|3|4|5'
when i run this it will give me an output i am storing into an array1
array1={23.23 45.34 34.34}
file2.txt logic implemented
myscriptfile2.sh file2.txt 'SAL|COL|PER'
for column SAL calculating sum and same followed for remaining two column COL PER
when i run this it will give me an output i am storing into an array2
array2={12.12 13.24 34.34}
Now final logic the output of array1 and array2 need to be compared index to index like below
value of array1[0] = array2[0]
value of array1[1] = array2[1]
value of array1[2] = array2[2]
if matches then [0]=[0] , [1]=[1] , [2]=[2] then should display SUCCESS or FAIL
SUCCESS when all values are matched according to respective indexed
FAIL when one of the value does not match
The approach should be like :
myscriptfile.sh file1.txt '2|3|4'
in future instead of '2 3 4 ' -> it might be 5 parameter '2|3|4|5|6' the code should be able to cut those data from file. The file will contain that many column record no need to worry
1234|23.23|45.34|34.34|23.4|34.111|2019-23-19|12:00:90
Same applied for file2.txt. how many parameters we are cutting in file1.txt
that many column name "sum" need to be cut from file2.txt. In this case data may be different no need to worry
ID|SAL|COL|PER|TAG|DIL|MILL|
myscriptfile2.txt file2.txt 'SAL|COL|PER|TAG|DIL'
My entire code :
#!/bin/bash
FILE1="$1"
# position : example - '$2|$3|$4'
FILE1_POSITION="$2"
# Checking file exist or not
if [ ! -f "$FILE1" ]
then
echo "$0: File '$FILE1' not found."
else
echo "$0: File '$FILE1' found."
fi
# Initialising the counter to zero
n=0
# # Cutting the FILE1_POSITION data separated with delimiter '|' and storing into array
# Declaring th dynamic array
declare -a ABC=()
# Assigning the position to array
ABC=($(awk '{n = split($0, t, "|")
for(i = 0; ++i <= n;) print t[i]}' <<<$FILE1_POSITION))
echo "${ABC[@]}"
# Looping multiple position values in an array and storing values into counter
for col in "${ABC[@]}"
do
file1array[n]=`cut -d'|' -f$col $FILE1`
((n=n+1))
done
############### FILE 2 [SUM of COLUMNS] #########################################################################
FILE2="$3"
# Column name for which sum required [format : 'SAL|COL|PER']
FILE2_COLUMN_NAME="$4"
# Checking file exist or not
if [ ! -f "$FILE2" ]
then
echo "$0: File '$FILE2' not found."
else
echo "$0: File '$FILE2' found."
fi
# Getting position of column name and storing it into an array
FILE2_POSITION=( $(awk -F"|" 'NR==1{for(i=1;i<=NF;i++){if ($i ~ /'$FILE2_COLUMN_NAME'/){print i;}}}' $FILE2) )
# Initialising the counter to zero
m=0
# Looping multiple position values in an array and storing values into counter
for each in "${FILE2_POSITION[@]}"
do
file2array[m]="$(awk -F'"?\\|"?' '{T+=$('$each')} END { printf "%.2f\n", T }' $FILE2)"
((m=m+1))
done
#printing number of values in array
echo "COUNT OF BALANCE COLUMN FOUND"
echo "${#file1array[@]}"
file1count=${#file1array[@]}
echo $file1count
# printing all values in array
echo "PRINTING ALL BALANCE COLUMN DATA"
echo "${file1array[@]}"
#printing number of values in array
echo "COUNT OF COLUMN SUM FOUND"
echo "${#file2array[@]}"
file2count=${#file2array[@]}
echo $file2count
# printing all values in array
echo "PRINTING ALL SUM OF COLUMN"
echo "${file2array[@]}"
########################### CHECKING ARRAY TO ARRAY INDEX #####################################################################
# # comparing count of array
if [ $file1count -eq $file2count ]
then
echo "Count matches = $file1count:$file2count"
else
echo "Count Does not matches = $file1count:$file2count"
fi
# # comapring array values by taking it as string
if [[ "${file1array[*]}" == "${file2array[*]}" ]]; then
echo SUCCESS
else
echo FAIL
fi
###############################################################################################################################
awk. Not once have you let us know what the overall purpose of your exercises are. It may be that you are even not needingawkat all.awk? The elements of the array are there. You could even just doecho '$2 $3 $4'... Consider adding context to you questions describing wty you want to do this.cutrather thanawk(ormapfileinto an array and then pick out the wanted entries). If you have multiple lines of input data, then it's unclear what the end result should be.