Linux 仅从文本文件中获取日期

Linux 仅从文本文件中获取日期,linux,Linux,仅从具有日期格式的多个日志文件中获取日期 例:日志文件1 Info 10/24/2013 6:42:18 PM Core 1 Initializing appilcation Novicorp WinToFlash 0.7.0048 beta Info 10/24/2013 6:42:18 PM Core 2 Started from D:\system use\os\Win To Flash 0.7.0048 Beta\WinToFlash.exe In

仅从具有日期格式的多个日志文件中获取日期

例:日志文件1

Info    10/24/2013  6:42:18 PM  Core    1   Initializing appilcation Novicorp WinToFlash 0.7.0048 beta
Info    10/24/2013  6:42:18 PM  Core    2   Started from D:\system use\os\Win To Flash 0.7.0048 Beta\WinToFlash.exe
Info    10/24/2013  6:42:18 PM  Core    3   Loading options
Info    10/24/2013  6:42:18 PM  Options 0   Options loaded
Warning 10/24/2013  6:42:19 PM  License 0   License file doesn't exists - EULA status reset
Error   10/24/2013  6:42:30 PM  Network 0   The server does not respond
Error   10/24/2013  6:42:49 PM  Network 0   The server does not respond
Error   10/24/2013  6:42:49 PM  Network 0   The server does not respond
Info    10/24/2013  6:45:02 PM  Core    9   Application termainated
例:日志文件2

09/22/2014 08:10:24.363 [1876]: Uninstalling assembly System.Management.Automation, Version=3.0.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35
09/22/2014 08:10:24.363 [1876]: Executing command from offline queue: install "System"
09/22/2014 08:10:24.379 [1876]: Uninstalling assembly PresentationCore, Version=4.0.0.0
09/22/2014 08:10:24.379 [1876]: Executing command from offline queue: install
在上面的例子中,两个例子给出了不同的列,但我只想得到日期

我的编码:

#!/bin/sh
for file_name in /media/saraswathy/7284EA0684E9CD23/Log/data/input_files/*.log;
do
while read file_line
do
echo $file_line | date +%Y:%m:%d -d "1 day ago"

done<$file_name
done
#/垃圾箱/垃圾箱
对于/media/saraswathy/7284EA0684E9CD23/Log/data/input\u files/*.Log中的文件名;
做
在读取文件行时
做
echo$file_行|日期+%Y:%m:%d-d“1天前”

完成
grep
将帮助您。假设日期的格式为:
mm/dd/yyyy
,则可以使用:
grep'[0-9]\{2\}/[0-9]\{2\}/[0-9]\{4\}'-o

说明:
  • [0-9]
    :解析数字
  • \{2\}
    :重复2次<代码>\
用于转义括号
  • -o
    :仅打印解析的部分(而不是打印整行)
  • 例子:
    使用awk工具可以很容易地完成,例如,您可以使用许多其他选项(分隔符、正则表达式等)打印您想要的任何列。只需将列号(从1开始索引)放在$sign后面即可

    $ awk '{print $2}' example1.log
    10/24/2013
    10/24/2013
    10/24/2013
    10/24/2013
    10/24/2013
    10/24/2013
    10/24/2013
    10/24/2013
    10/24/2013
    
    $ awk '{print $1}' example2.log
    09/22/2014
    09/22/2014
    09/22/2014
    09/22/2014
    
    同样的效果也可以通过cat或瓷砖和管道实现:

    $ cat example1.log | awk '{print $2}'
    $ tail example2.log | awk '{print $1}'
    
    $ cat example1.log | awk '{print $2}'
    $ tail example2.log | awk '{print $1}'