2016-10-19 101 views
0

我有一个输入文件的日期(说的fileA)在格式YYYY-MM-DD从列表,其是大于或等于另一个值找出最大值

e.g. 2016-10-18 

现在我有一个列表日期时间戳格式置身于另一个文件下面的(在FILEB说)

20161017120311 
20161017140317 
20161018010315 
20161018160311 
20161019020310 
20161019124015 

现在我只需要选择等于的fileA的日期之日起(从FILEB)的最大值。所以在这种情况下从fileB中选择的日期将是20161018160311

它也可能发生,日期为20161018的fileB中没有记录。说fileB看起来像下面

20161017120311 
20161017140317 
20161019020310 
20161019124015 
20161020010315 
20161021160311 

在相同的代码应该选择下一个可用日期的最大值的情况下。即下一个可用日期是20161019,并且20161019的最大值是20161019124015。所以输出应该是在这种情况下20161019124015

回答

-2

maxDate.sh:

fileA=$1 
fileB=$2 
date=`sed s/-//g $fileA` 
max=`grep $date $fileB | sort | tail -1` 
if [[ $max == '' ]]; 
then 
    date=`sed s/-//g $fileA $fileB | sort | grep $date -1 | tail -1 | egrep -o [0-9]{8}`; 
    max=`grep $date $fileB | sort | tail -1` 
fi 
echo $max 

运行:

./maxDate.sh fileA fileB 
+0

能否请你更改代码段从文件中读取它,喜欢的fileA和FILEB –

+0

当然,@KoushikChandra。完成! –

+1

为了增强可靠性,请将您的变量引用重复引用,例如'“$ fileA”'。考虑使用更现代的命令替换语法,'$(...)'而不是反引号。 由于这个问题通常被标记为'shell',最好避免使用''[[...]]'等Bashisms。使用大约15次读取输入文件以完成 任务的子进程效率非常低。 – mklement0

0

请尝试以下awk命令,这比Marcel Jacques Machado's answer更有效:

#!/bin/sh 

fileA='/path/to/file A' 
fileB='/path/to/file B' 

awk -v refDate="$(tr -d '-' < "$fileA")" ' 
    substr($0, 1, length(refDate)) < refDate { next } # skip lines before 
    substr($0, 1, length(refDate)) == refDate { lastMatch = $0; next } # save match 
    { exit } # we are done once the first greater row is reached 
    END { print (lastMatch == "" ? $0 : lastMatch); exit } # print last match or current row 
' "$fileB" 

这产生子进程 - 一个用于涉及tr命令替换,一个用于awk - 在与所估计的到子处理该马塞尔的溶液造成的对比度,这多次可能读取的输入文件。

0
$ cat program.awk 
NR==FNR {     # get the date from the first file 
    a=$0"000000"    # zeropad the end (a="2016-10-18000000") 
    gsub(/-/,"",a)   # remove dashes (a="20161018000000") 
    next     
} 
$0 >= a {     # we sorted fileB so the next bigger or equal to a is the date 
    if(b=="")    # pick whatever is the next date for reference 
     b=substr($0,1,8)  # just the date part 
    d=substr($0,1,8)   # get this records date part 
    if (d>b) {    # if this date is bigger than the reference... 
     print c; exit  # output and exit 
    } 
    c=$0      # the latest timestamp on this date 
} 

运行:

$ awk -f program.awk fileA <(sort -n fileB) 
20161019124015 
相关问题