How to split the output of a command into two and save the result in a bash variable?

Tell me if I want to execute this command:

(cat file | wc -l)/2

and save it in a variable, for example, in the middle, how would I do it?

I know this is just not the case

$middle=$(cat file | wc -l)/2

so how can i do this?

+3
source share
5 answers
middle=$((`wc -l < file` / 2))
+10
source
middle=$((`wc -l file | awk '{print $1}'`/2))
+1
source

, Bash , , .

 middle=($(wc -l file))     # create an array which looks like: middle='([0]="57" [1]="file")'
 middle=$((middle / 2))     # do the math on ${middle[0]}

:

((middle /= 2))
+1

$

:

mid=$(cat file | wc -l)
middle=$((mid/2))
echo $middle

. , , , Bash, ?

0

awk.

middle=$(awk 'END{print NR/2}' file)

"wc", .

linec(){
  i=0
  while read -r line
  do
    ((i++))
  done < "$1"
  echo $i
}

middle=$(linec "file")
echo "$middle"
0

Source: https://habr.com/ru/post/1733971/


All Articles