每110行循环改变一次

每110行循环改变一次

我想使用文件的每 110 行的当前日期更改时间戳。我在文件上使用 cat 并通过管道输入 sed 进行替换,然后写入文件。有没有办法只更改每 110 行的日期,以便每 110 行都有更改时的当前日期?下面是我当前如何更改时间戳的示例。这工作正常但改变了整个文件。

cat /path/to/file | sed "s/[a-zA-Z]\{3\}\s[0-9]\{1,2\}\s[0-9]\{2\}\:[0-9]\{2\}\:[0-9]\{2\}/$(date +"%b %d %H:%M:%S")/g" >> /path/to/newfile

我冒险进入尾部和头部寻找解决方案,但下面的循环只完成了前 110

i=0
jump=110
while [ $i -le 1100 ]
do
tail -n $i | head -n $jump /path/to/file | sed "s/[a-zA-Z]\{3\}\s[0-9]\{1,2\}\s[0-9]\{2\}\:[0-9]\{2\}\:[0-9]\{2\}/$(date +"%b %d %H:%M:%S")/g" >> /path/to/newfile
i=$((%i + $jump))
done

这是输入的示例

Apr 15 16:29:19 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159

输出应该与更改的日期相同。

Apr 1 16:29:19 generic.com WindowsLog Computer=COMP-user-0001   User=user-0001  TimeGenerated=1554103759    TimeWritten=1554103759

答案1

我不太明白你为什么要这样做,但如果你有 GNU 版本,split你可以使用它的--filter选项通过现有命令处理文件块sed

split -l 110 file --filter='
  sed "s/[a-zA-Z]\{3\}\s[0-9]\{1,2\}\s[0-9]\{2\}\:[0-9]\{2\}\:[0-9]\{2\}/$(date +"%b %d %H:%M:%S")/"
' > path/to/newfile

为了说明这一点,我使用了一个较短的(25 行)输入文件,每 5 行分割一次,并在时间戳中添加了纳秒,以确保结果明显不同:

$ split -l 5 file --filter='
    sed "s/[a-zA-Z]\{3\}\s[0-9]\{1,2\}\s[0-9]\{2\}\:[0-9]\{2\}\:[0-9]\{2\}/$(date +"%b %d %H:%M:%S.%N")/"
  '
Apr 01 13:35:36.817804684 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159
Apr 01 13:35:36.817804684 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159
Apr 01 13:35:36.817804684 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159
Apr 01 13:35:36.817804684 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159
Apr 01 13:35:36.817804684 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159
Apr 01 13:35:36.824106329 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159
Apr 01 13:35:36.824106329 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159
Apr 01 13:35:36.824106329 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159
Apr 01 13:35:36.824106329 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159
Apr 01 13:35:36.824106329 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159
Apr 01 13:35:36.831402445 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159
Apr 01 13:35:36.831402445 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159
Apr 01 13:35:36.831402445 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159
Apr 01 13:35:36.831402445 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159
Apr 01 13:35:36.831402445 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159
Apr 01 13:35:36.840339832 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159
Apr 01 13:35:36.840339832 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159
Apr 01 13:35:36.840339832 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159
Apr 01 13:35:36.840339832 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159
Apr 01 13:35:36.840339832 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159
Apr 01 13:35:36.844512992 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159
Apr 01 13:35:36.844512992 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159
Apr 01 13:35:36.844512992 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159
Apr 01 13:35:36.844512992 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159
Apr 01 13:35:36.844512992 generic.com WindowsLog Computer=COMP-user-0001  User=user-0001  TimeGenerated=1523806159   TimeWritten=1523806159

答案2

尝试这个,

lineNo=0
jump=110
while [ "$lineNo" -le 1100 ] && [ "$lineNo" -gt 0 ]
do
        sed -i "${lineNo}s/[a-zA-Z]\{3\}\s[0-9]\{1,2\}\s[0-9]\{2\}\:[0-9]\{2\}\:[0-9]\{2\}/$(date +"%b %d %H:%M:%S")/g" kk
lineNo=$((lineNo + jump))
done

我们可以使用单个 sed 命令来完成此操作。

  • sed -i "${lineNo}....将仅在我们传递的指定行号中编辑文件。
  • [ "$lineNo" -gt 0 ]忽略变量以将值传递为零以避免错误。

答案3

awk '{$1=$2=$3="";$0=strftime("%b %d %H:%M:%S")" "$0;print;if(NR%110){system("sleep 1")}}' filename

这只会删除前三列并替换为当前时间戳。然后稍等一下。然后用当前时间戳更新下一个 110 行..

相关内容