awk - How to evaluate in bash a sum of numbers -
i have naive bash question.
export a=`wc file.txt` export b=`echo $a[1] - 1 | bc` the problem cannot evaluate first element in $a. awk
echo $a | awk '{print $1}' but not work if insert in previous equation.
maybe has idea?
you setting variable $a output of wc. since show $a[1] looks want 2nd value, is, number of words, use $1 in awk, think want number of lines, -l parameter in wc.
so on supposing want use lines. if not, change solution -l instead of -w.
the thing wc file outputs many parameters. if specify -w or -l, gets value file name. if indirection wc -l < file.txt, number of lines, don't have clean output.
this way, can do:
a=$(wc -l < file.txt) b=$(echo "$a" -1 | bc) all together, may want use directly, without need store intermediate value:
b=$(echo "$(wc -l <file.txt)" -1 | bc) or if want use awk, can say:
awk -v lines="$(wc -l < file.txt)" 'begin {print lines-1}' or use $(( )) perform calculations, suggested jid:
b=$(($(wc -l <file.txt) - 1 )) or
((b=$(wc -l <file.txt)-1))
Comments
Post a Comment