如何编写shell脚本将grep数据导入基于日期的文件名?

时间:2021-11-02 15:07:04

I basically want to do this:

我基本上想要这样做:

grep 'example.com' www_log > example.com.YYYY-MM-DD-H:i:S.log

...with of course the filename being example.com.2008-09-27-11:21:30.log

...当然文件名为example.com.2008-09-27-11:21:30.log

I'd then put this in crontab to run daily.

然后我将它放在crontab中以便每天运行。

3 个解决方案

#1


9  

The verbose method:

详细方法:

grep 'example.com' www_log > `date +example.com.%Y-%m-%d-%H:%M:%S.log`

The terse method:

简洁的方法:

grep 'example.com' www_log > `date +example.com.%F-%T.log`

#2


5  

grep 'example.com' www_log > example.com.$(date +%F-%T).log

#3


2  

Here is another way, that I usually use:

这是我通常使用的另一种方式:

grep 'example.com' www_log > example.com.`date +%F-%T`.log

Backticks are a form of command substitution. Another form is to use $():

反引号是命令替换的一种形式。另一种形式是使用$():

$(command)

which is the same as:

这与:

`command`

#1


9  

The verbose method:

详细方法:

grep 'example.com' www_log > `date +example.com.%Y-%m-%d-%H:%M:%S.log`

The terse method:

简洁的方法:

grep 'example.com' www_log > `date +example.com.%F-%T.log`

#2


5  

grep 'example.com' www_log > example.com.$(date +%F-%T).log

#3


2  

Here is another way, that I usually use:

这是我通常使用的另一种方式:

grep 'example.com' www_log > example.com.`date +%F-%T`.log

Backticks are a form of command substitution. Another form is to use $():

反引号是命令替换的一种形式。另一种形式是使用$():

$(command)

which is the same as:

这与:

`command`