【问题标题】:Bash coding return a file name with specific stringBash 编码返回具有特定字符串的文件名
【发布时间】:2013-09-12 10:17:37
【问题描述】:

我的脚本(在 bash 中)旨在完成这项工作:

  1. 从文件file_A 获取开始和停止时间。时间范围通常为 3-24 小时。

  2. 基于[start_time, stop_time]从file_A得到的这个时间窗口, 我需要在总共 10k 个日志文件中找到特定的文件(并且会随着实验运行而增加),每个记录大约 30 分钟。也就是说,我要在 10k 个日志文件中找到 6-50 个日志文件。

  3. 确认正确的日志文件后,需要打印出感兴趣的数据。

步骤 1) 和 3) 都可以,我已经做到了。 现在,我被困在第 2 步),尤其是在两个地方:

(一)。自日志文件命名为时间以来,如何通过名称有效地选择合适的文件。每个名为 log_201305280650 的日志文件表示 2013 / May 28 / 06:50。也就是说,根据从file_A获取的时间,我需要通过名字来确认对应的日志文件,这是时间的暗示。

(b)。选择文件后,从该文件中读取时间在时间​​窗口内的项目(如温度、压力等)。因为每个文件记录30分钟,这意味着这个文件中的某些条目不能满足时间窗口。

例如,

从步骤 1) 开始,我的时间窗口设置为 [201305280638, 201305290308]。

从步骤 2),我知道日志文件 (log_201305280650) 包含 201305280638 的开始时间。所以我需要读取 201305280638 以下条目的所有温度和压力。

    the log files name is log_201305280650 (= 2013 / May 28 / 06 :50)

    Time                      temperature  pressure ...
    201305280628                100,         120  ...
    201305280629                100,         120  ...

   ...              ...     ...

    201305280638                101,         121  ...
    201305280639                99,          122  ...

     ...             ...     ... 

    201305280649                101,         119  ...
    201305280650                102,         118  ...

我的假脚本正在跟踪。

get time_start from /path/file_A
get time_stop  from /path/file_A
for file in /path_to_log_files/*
do
case "$file" in
*)        
     If [[log file name within time window of (time_start, time_stop)]]; then
     loop over this file to get the entry whose time is just within (time_start, time_stop)
     read out temperature and pressure etc.
fi
esac
done

【问题讨论】:

  • 您可能想阅读formatting help 并进一步清理它。根本不清楚您的实际问题是什么。
  • 为什么你的case 语句有两次*)?
  • TMI。如果您提供 SSCCE,您将更有可能获得帮助:sscce.org。
  • 确实,还不够清楚。但不知怎的,我尽了最大努力把它说清楚。
  • 大家好:非常感谢您的关注!我对我的帖子做了很多修改。希望它看起来比以前更清晰。最好的!

标签: bash


【解决方案1】:

使用 awk 和 date 命令的 +"%s" 选项可能比使用文字日期和时间更容易。此选项将日期/时间从纪元 (01-01-1970) 转换为秒。生成的数字很容易使用。毕竟,这只是一个数字。作为示例,我制作了一个小型 bash 脚本。首先,一个模拟:

#!/bin/bash

#simulation: date and time
start_dt="2013-09-22 00:00:00"
end_dt="2013-09-22 00:00:00"
start_secs=$(date -d "start_dt" +"%s")
end_secs=$(date -d "end_dt" +"%s")
#simulation: set up table (time in secs, temperature, pressure per minute)
> logfile
for ((i=$start_secs;i<$end_secs;i=i+60)); do
    echo $i $[90+$[RANDOM %20]] $[80+$[RANDOM %30]] >> logfile
done

这是获取用户范围并打印出来的实际脚本:

echo "Enter start of range:"
read -p "Date (YYYY-MM-DD): "sdate
read -p "Time (HH:MM:SS)  : "stime
echo "Enter end of range:"
read -p "Date (YYYY-MM-DD): "edate
read -p "Time (HH:MM:SS)  : "etime
#convert to secs
rstart=$(date -d "$sdate $stime" +"%s")
rend=$(date -d "$edate $etime" +"%s")
#print it to screen
awk -v rstart=$rstart -v rend=$rend '{if($1 >= rstart && $1 <= rend)print $0}' logfile

awk 命令非常适合这种情况。它速度快,可以处理大文件。我希望这能给你一些想法。

【讨论】:

    【解决方案2】:

    也许类似的东西对你有用?我使用 $start 和 $end 作为 file_A 的开始和结束时间。我

     eval cat log_{$start..$end} 2> /dev/null | sort -k1 | sed -n "/$start/,/$end/p"
    

    这假设您的日志文件格式为

    time temperature pressure ...
    

    没有标题或其他此类文本

    【讨论】:

      【解决方案3】:

      使用 bash 做得很好。 Perl 或 python 会更容易,它们都有日期/时间模块。

      我花了一段时间进行通常的日期切片,这太可怕了,所以我作弊并改用文件时间戳。 Bash 有一些有限的时间戳检查,这就是使用它。好的,它做了一些文件 IO,但这些都是空文件,什么鬼!

      lower=201305280638
      upper=201305290308
      filename=log_201305280638
      filedate=${filename:4}
      
      if (( filedate == upper )) || (( filedate == lower ))
      then
          echo "$filename within range"
      else
          # range files
          touch -t $lower lower.$$
          touch -t $upper upper.$$
      
          # benchmark file
          touch -t $filedate file.$$
      
          if [[ file.$$ -nt $upper ]]
          then
              echo "$filename is too young"
      
          elif [[ file.$$ -ot $lower ]]
          then
              echo "$filename is too old"
          else
              echo "$filename is just right"
          fi
      
          rm lower.$$ upper.$$ file.$$
      fi
      

      -nt 是“比新”

      -ot 是“大于”

      因此在开始时检查是否相等。您可以对文件中的时间戳使用类似的检查(您的第二个问题)。但老实说,你不能用 perl 或 python 吗?

      【讨论】:

      • 嗨,@cdarke,非常感谢您的回复和第一时间!老实说,由于我的 bash 水平有限,我没有资格评估您的代码。但是,根据此链接,stackoverflow.com/questions/78493/what-does-mean-in-the-shell。大多数人不鼓励使用“$$”——我并不是说你的代码不好——我确实无法判断。但是,很明显,您实际上建议我强烈使用 python 或 perl。我会认真考虑这一点并尝试找出最佳解决方案(我在 Python 方面的背景很小)。无论如何,再次感谢你!
      • @user2740039:好的,但是使用 $$ 使代码更简单,我个人认为在这种情况下是合理的。你不必使用它。关键是您的时间戳格式完全适合touch,浪费这种巧合(如果是这样的话)似乎很可惜。
      猜你喜欢
      • 1970-01-01
      • 2021-10-07
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-19
      • 1970-01-01
      相关资源
      最近更新 更多