Concatenate multiple commands

hi, i am creating a script that given a file with multiple fields, take the first and return everything on a single output line: file name, number of lines, sum, average and standard deviation ....
I have the commands to calculate them but I don't know which command to use to concatenate them and put everything on the same output line

#!/bin/bash



if  [ $# -eq 1 ] ;     
        
        then 
              
filename="/home/marco/$1"
base=`basename "$filename"`
noext=${base%.*}

echo -n " $noext  " ;
wc -l $1 | awk '{print $1}' ;  
awk '{SUM+=$1}END{print SUM}' $1 
awk '{sum+=$1} END {print sum/NR}' $1 
awk  'NR>2 {sum+=$1; array[NR]=$1} END {for(x=1;x<=NR;x++){sumsq+=((array[x]-(sum/NR))^2);}print sqrt(sumsq/NR)}' $1

                   
fi

In general you would use VAR=$( command ) to capture the output of each command and then echo your result at the end like this:

#!/bin/bash

if [ $# -eq 1 ]
then 
    filename="/home/marco/$1"
    base=`basename "$filename"`
    noext=${base%.*}

    SZ=$(wc -l $1 | awk '{print $1}' )
    AVG=$(awk '{sum+=$1} END {print sum/NR}' $1)
    STD=$(
       awk 'NR>2 {sum+=$1; array[NR]=$1}
           END {for(x=1;x<=NR;x++){sumsq+=((array[x]-(sum/NR))^2);}print sqrt(sumsq/NR)}' $1)

    echo " $noext $SZ $SUM $AVG $STD"
fi

However in this instance one pass thru the file with awk will allow you to capture all the values needed like this:

if [ $# -eq 1 ]
then 
    awk '
        {SUM+=$1; array[NR]=$1}
        END {
           for(x=1;x<=NR;x++) {
              sumsq+=((array[x]-(SUM/NR))^2)
           }
           noext=FILENAME
           gsub(/\..*$/, "",noext)
           print noext,NR,SUM,SUM/NR,sqrt(sumsq/NR)}' $1
fi
2 Likes