2015-02-23 40 views
0

例如,我从2015年2月16日至20日有以下日志文​​件。现在,我要创建一个名为,mainentrywatcherReport_2015-02-16_2015-02-20.log。换言之,我想从周的第一个和最后一个文件(周一到周五)提取日期格式,并在每个星期六创建一个输出文件。我将在每个星期六使用cron触发脚本。将所有文件追加到unix中的一个文件中,并用第一个和最后一个文件名的一部分重命名输出文件

$ ls -l 
mainentrywatcher_2015-02-16.log 
mainentrywatcher_2015-02-17.log 
mainentrywatcher_2015-02-18.log  
mainentrywatcher_2015-02-19.log 
mainentrywatcher_2015-02-20.log 

$ cat *.log >> mainentrywatcherReport_2015-02-16_2015-02-20.log 
$ mv *.log archive/ 

任何人都可以帮助如何将输出文件重命名为上述格式?

+0

您可以使用'head'和'tail'来获取目录中的第一个和最后一个文件名。 – dimo414 2015-02-23 18:28:15

+0

'head','tail'和一些字符串操作('cut' /'grep',http://tldp.org/LDP/abs/html/string-manipulation.html,...) – 2015-02-23 18:28:16

回答

0

也许尝试这样:

parta=`ls -l | head -n1 | cut -d'_' -f2 | cut -d'.' -f1` 
partb=`ls -l | head -n5 | cut -d'_' -f2 | cut -d'.' -f1` 
filename=mainentrywatcherReport_${parta}_${partb}.log 
cat *.log >> ${filename} 
  • 的 “ls -l” 输出中的问题进行说明
  • “头-NX” 需要输出
  • 的第X个行“切-d'_'-f2“取第一个下划线后的所有内容(保留)
  • ”cut -d'。' -f1“倍第一个时间段前的所有内容(保持不变)
  • 两个命令都被`标记(以上代字符〜)包围以捕获变量的命令输出
  • 文件名汇编剥离的两个日期对于最终文件名所需的其他格式是不必要的。
  • cat命令演示了使用产生的文件名
一种可能的方式

编码愉快!如果您有任何问题,请留下评论。

+0

这也很好,但对于partb变量,我们需要尾部-n1而不是头部-n5。谢谢你的帮助 – gtaware 2015-02-24 11:30:48

0

事情是这样的:

#!/bin/sh 
LOGS="`echo mainentrywatcher_2[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9].log`" 
HEAD= 
TAIL= 
for logs in $LOGS 
do 
    TAIL=`echo $logs | sed -e 's/^.*mainentrywatcher_//' -e 's/\.log$//'` 
    test -z "$HEAD" && HEAD=$TAIL 
done 
cat $LOGS >mainentrywatcherReport_${HEAD}_${TAIL}.log 
mv $LOGS archive/ 

即:

  • 在变量$日志中获取的现有日志(碰巧排序)的列表
  • 穿行列表,根据示例获得日期
  • 将第一个日期保存为$ HEAD
  • 将最后日期a s $ TAIL
  • 循环后,将所有这些文件都转换为新的输出文件
  • 将用完的日志文件移动到归档目录中。
+0

更完整脚本会检查是否有*有任何文件。 – 2015-02-23 21:46:55

+0

嗨,谢谢,你的解决方案工作,但输出文件的名称是作为mainentrywatcherReport_mainentrywatcher_2015-02-16_mainentrywatcher_2015-02-20.log,但我想要的名称为mainentrywatcherReport_2015-02-16_2015-02-20.log,请你建议编辑为这个?我不想要第二个mainentrywatcher尾部变量 – gtaware 2015-02-24 10:06:31

+0

这是一个错字 - 固定。当我检查发现错别字的时候,我注意到一个流浪的“^。*”,这看起来不正确,并将它“纠正”为一个整数。这与* sed *一致。 – 2015-02-24 10:41:05

0

,如果你要为大家介绍简单的循环你可以试试这个...

FROM = ls -lrt mainentrywatcher_* | awk '{print $9}' | head -1 | cut -d"_" -f2 | cut -d"." -f1

TO = ls -lrt mainentrywatcher_* | awk '{print $9}' | tail -1 | cut -d"_" -f2 | cut -d"." -f1

FINAL_LOG = mainentrywatcherReport _ $ {FROM} _ $ {TO}。登录

因为我在ls -lrt mainentrywatcher_* | awk '{print $9}' 做 猫$ I >> $ FINAL_LOG 做

呼应

0

另一种方法给您的日常文件和测试内容为 “在$ FINAL_LOG存储的所有日志”:

mainentrywatcher_2015-02-16.log -> a 
mainentrywatcher_2015-02-17.log -> b 
mainentrywatcher_2015-02-18.log -> c 
mainentrywatcher_2015-02-19.log -> d 
mainentrywatcher_2015-02-20.log -> e 

利用的bash 参数扩展/子串提取将是一个简单的循环:

#!/bin/bash 

declare -i cnt=0        # simple counter to determine begin 
for i in mainentrywatcher_2015-02-*; do   # loop through each matching file 
    tmp=${i//*_/}        # isolate date 
    tmp=${tmp//.*/} 
    [ $cnt -eq 0 ] && begin=$tmp || end=$tmp # assign first to begin, last to end 
    ((cnt++))         # increment counter 
done 

cmbfname="${i//_*/}_${begin}_${end}.log"  # form the combined logfile name 

cat ${i//_*/}* > $cmbfname      # cat all into combined name 

## print out begin/end/cmbfname & contents to verify 
printf "\nbegin: %s\nend : %s\nfname: %s\n\n" $begin $end $cmbfname 

printf "contents: %s\n\n" $cmbfname 

cat $cmbfname 

exit 0 

使用/输出:

alchemy:~/scr/tmp/stack/tmp> bash weekly.sh 

begin: 2015-02-16 
end : 2015-02-20 
fname: mainentrywatcher_2015-02-16_2015-02-20.log 

contents: mainentrywatcher_2015-02-16_2015-02-20.log 

a 
b 
c 
d 
e 

可以,当然,修改for loop接受含有部分文件名的位置参数和通过命令行传递的部分的文件名。

相关问题