Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何在while read循环结束时进行多次“echo$count”?_Bash - Fatal编程技术网

Bash 如何在while read循环结束时进行多次“echo$count”?

Bash 如何在while read循环结束时进行多次“echo$count”?,bash,Bash,我在shell脚本中使用了while read循环来计算file.txt文件在12:00:00到18:00:00之间的行数,该脚本运行得非常好。现在,我想扩展此过程以计算以下时间步之间的行数: 00:00:00 and 05:59:59; 06:00:00 and 11:59:59; 12:00:00 and 17:59:59; 18:00:00 and 23:59:59; 查看第一个脚本及其结果 #!/bin/bash let count=0 while read cdat

我在shell脚本中使用了while read循环来计算file.txt文件在12:00:00到18:00:00之间的行数,该脚本运行得非常好。现在,我想扩展此过程以计算以下时间步之间的行数:

00:00:00 and 05:59:59;
06:00:00 and 11:59:59;
12:00:00 and 17:59:59;
18:00:00 and 23:59:59;
查看第一个脚本及其结果

  #!/bin/bash

  let count=0

  while read cdat ctim clat clon
       do
          h=${ctim:0:2};    # substring hours from ctim
          m=${ctim:3:2};
          s=${ctim:6:2};
           if [[ $h -ge 12 && $h -le 17 ]] || [[ $ctim == "18:00:00" ]]; then
             let count=$count+1
             echo $count $cdat $ctim $clat $clon          
           fi

       done  <  cloud.txt 
       echo $count
  exit

我想做同样的事情,根据相应的时间步长得到不同的结果,但是在同一个脚本中。我想一个接一个地打印出上述不同的结果。

我会编辑您的初始脚本,将开始时间和结束时间作为参数输入,然后编写第二个脚本,为您的第一个时间值提供您想要的开始时间和结束时间列表,如果您希望计数分开,可能还需要一个输出文件。您需要编辑第一个脚本以忽略所有内容,直到开始时间,然后只需拉出18:00:00并用endtime变量替换它


这样,您就可以轻松地在任意时间段运行报告,并且您已经设置了计算必要数据的逻辑。

嗨,Jackman先生,我认为您建议的脚本可以帮助我完成我的工作,尽管我仍然会遇到一些错误,而且我没有获得每个时间段的单独行号,但对于全球而言。查看结果:./fileproc5.sh:line 6:[[:00:00:00:表达式中的语法错误错误错误标记为:00:00./fileproc5.sh:line 8:[[:06:00:00:00:表达式中的语法错误错误错误错误标记为:00:00 16 2014/04/06 04:53:00 36.11 07.21计数介于0和6之间:计数介于6和12之间:计数介于12和18之间:计数介于18和24之间:16总计数:16您能帮我改进吗?啊,对了。对于字符串比较,没有Hi。谢谢Jackman先生。现在没有错误。但是现在,如果我想分别得到每个时间步的行数,我该怎么做?例如,0到6之间的行数=?3可能等等。就像我的总数=16一样,但是单个时间步{[0-6],[6-12],[12-18]}呢?查看我的结果:计数介于0和6之间:计数介于6和12之间:计数介于12和18之间:1计数介于18和24之间:15总计数:16将${count[index]}更改为${count[index]-0}在for循环内的echo命令中。是的,它可以工作,但不是预期的数字。请参阅结果:计数在0-6:0之间计数在6-12:0之间计数在12-18:1之间计数在18-24:12之间总数:13请参阅数据内容:2014/04/00 10:44:05 26.12-23.22 2014/11/21 16:05:00 19.56-05.30 2014/01/31 13:55:00 02.00 31.10 2014/04/00 14:20:00 12.2014/07/25 15:30:00 35.25 05.90 2014/05/15 12:07:00 23.95 07.11 2014/07/29 17:34:00 44.00 17.43 2014/12/31 01:30:00 -14.00 09.43 2014/02/11 19:00:00 30.44 06.65 2014/03/20 18:00:00 -11.12 -22.05 2014/09/21 12:00:00 06.44 41.55 2014/03/19 11:44:00 16.41 -25.58 2014/04/06 04:53:00 36.11 07.21
count=()
total=0
while read cdat ctim rest; do
    if   [[ $ctim == "00:00:00" ]] || [[ "00:00:00" < $ctim && $ctim < "06:00:00" ]]; then
        index=0
    elif [[ $ctim == "06:00:00" ]] || [[ "06:00:00" < $ctim && $ctim < "12:00:00" ]]; then
        index=6
    elif [[ $ctim == "12:00:00" ]] || [[ "12:00:00" < $ctim && $ctim < "18:00:00" ]]; then
        index=12
    else
        index=18
    fi
    (( count[index]++ ))
    (( total ++ ))
    echo ${count[index]} $cdat $ctim $rest
done < file

for index in 0 6 12 18; do
    echo "count between $index and $((index+6)): ${count[index]}"
done
echo total count: $total
count=()
total=0
while read cdat ctim rest; do
    if   [[ $ctim == "00:00:00" ]] || [[ "00:00:00" < $ctim && $ctim < "06:00:00" ]]; then
        index=0
    elif [[ $ctim == "06:00:00" ]] || [[ "06:00:00" < $ctim && $ctim < "12:00:00" ]]; then
        index=6
    elif [[ $ctim == "12:00:00" ]] || [[ "12:00:00" < $ctim && $ctim < "18:00:00" ]]; then
        index=12
    else
        index=18
    fi
    (( count[index]++ ))
    (( total ++ ))
    echo ${count[index]} $cdat $ctim $rest
done < file

for index in 0 6 12 18; do
    echo "count between $index and $((index+6)): ${count[index]}"
done
echo total count: $total