0

DATA SET file name "demo.txt"

ID|SAL|COL|PER|TAG|GER
1"|"1.11"|"2.22"|"1.1"|"2.2"|D"
2"|"1.234"|"3.234"|"2.2222"|"2.34"|"B"
3"|"1.234"|"35.23"|"3.2"|"2.34"|"A"

I have created a function which will automatically calculate decimal column sum below is the function I am calling that function in main.sh script

demo()
{
FILE_NAME="$1"
COLUMN_NAME="$2"

alpha=( $(awk -F"|" 'NR==1{for(i=1;i<=NF;i++){if ($i ~ /'$COLUMN_NAME'/){print i;}}}' $FILE_NAME) )

for each in "${alpha[@]}"
do
   awk -F'"?\\|"?' '{T+=$('$each')} END { printf "%.2f\n", T }' $FILE_NAME  
done

}

Script : main.sh

#function called in main.sh script

demo demo.txt 'SAL|COL|PER|TAG'

issue is how do i get the output into an array

below is the output i am getting need to store into array1

3.57 40.68 6.52 6.88

second array is this below output getting from another program

array2={3.57 40.68 6.52 6.88}

**so that I can match the array1 with array 2 position value

array1[0] with array2[0] 
array1[1] with array2[1] 
array1[2] with array2[2] 
array1[3] with array2[3] 

if all succeed should display success status as YES

i have solution which is partially working ... help me with some solution so that i can store each value of an array to array1

FILE_NAME="$1"
COLUMN_NAME="$2"

alpha=( $(awk -F"|" 'NR==1{for(i=1;i<=NF;i++){if ($i ~ /'$COLUMN_NAME'/){print i;}}}' $FILE_NAME) )

declare -a array=();

for each in "${alpha[@]}"
do
        #var=($(awk -F'"?\\|"?' '{T+=$('$each')} END { printf "%.2f\n", T }' $FILE_NAME))
        mapfile -t array <<< "$(awk -F'"?\\|"?' '{T+=$('$each')} END { printf "%.2f\n", T }' $FILE_NAME)"
        #awk -F'"?\\|"?' '{T+=$('$each')} END { printf "%.2f\n", T }' $FILE_NAME
done

echo "${array[@]}"
8
  • Does your input file not contain the " at the end and at the beginning of each row? – aborruso Jan 2 '20 at 18:12
  • @aborruso ... specifically i am targeting on specific column sum where in my case the file data is in that format which is fixed ..my code is able to print sum of SAL COST PER TAG ... but the problem is the printed output i want to store into array 1 so that i can match array1 first with first value of array2 ..and so on ... if all value in array1 match with array 2 then depending on success my another script should run otherwise it should exit by saying data does not match – genip26057 Jan 2 '20 at 18:23
  • @aborruso .. this entire process should be done without creating a third file that why i am targeting to compare output of demo function with output of array2 ... is their any another approach to do kindly share without creating file can be compare on success my another script should run or exit by saying data does not match – genip26057 Jan 2 '20 at 18:25
  • @Kusalananda can you look into this above post ... and provide me your solution how to do it ? – genip26057 Jan 2 '20 at 18:38
  • @aborruso .. updated my question with new data set .. please provide me your solution – genip26057 Jan 2 '20 at 18:41
0

The problem with your partly working solution is you store the single value from the sum of one column into the entire array every time, which deletes the previous values.

The solution is to stuff the values into the array one by one, rather than using the mapfile command which requires all the values on one input line:

FILE_NAME="$1"
COLUMN_NAME="$2"

alpha=( $(awk -F"|" 'NR==1{for(i=1;i<=NF;i++){if ($i ~ /'$COLUMN_NAME'/){print i;}}}' $FILE_NAME) )

declare -a array=()

n=0

for each in "${alpha[@]}"
do
        array[n]="$(awk -F'"?\\|"?' '{T+=$('$each')} END { printf "%.2f\n", T }' $FILE_NAME)"
        ((n=n+1))
done

echo "${array[@]}"

Running this gives:

$ bash test.sh demo.txt 'SAL|COL|PER|TAG'
3.58 40.68 6.52 6.88
2
  • thank you !!! bro you are awesome – genip26057 Jan 3 '20 at 9:34
  • 1
    bash knows how to append to an array: array+=( "$( awk ... )" ). Note also that the awk code contains a serious code injection vulnerability. Just set your $COLUMN_NAME value to something that ends the regular expression, and then inject whatever code you want. – Kusalananda Jan 3 '20 at 10:19
0

To get the output of demo into an array you could use Command Substitution

declare -a array1=( $(demo) )

and replace the call to demo within your script with the line above.

PS: If the input file has a fixed (and pre-defined) number of columns it might be easier to sum the columns directly into the array elements (and probably faster because you won't need to read $FILENAME #columns + 1 times).

3
  • i want to store each ouput of an array which is running to array1 – genip26057 Jan 2 '20 at 19:25
  • i have updated the partially running solution will you able to add some working solution to it – genip26057 Jan 2 '20 at 19:25
  • 1
    @genip26057 My answer solves the "can you tell me how do i get demo function values and store in array1" problem you mention in the comments above, just replace the current call to demo within the script with the line from my answer. If this is not what you want please update the question, especially the "below is the output i am getting need to store into array1" part. – nohillside Jan 2 '20 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.