改变从bash脚本调用在一个月到一个星期 [英] change the call from a bash script in a month to a week

查看:119
本文介绍了改变从bash脚本调用在一个月到一个星期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在bash脚本2,我有一些文件:

I have 2 script in bash, and i have some files:

transaction-2012-01-01.csv.bz2
transaction-2012-01-02.csv.bz2
transaction-2012-01-03.csv.bz2
transaction-2012-01-04.csv.bz2
             .
             .
transaction-2012-01-31.csv.bz2
transaction-2012-02-01.csv.bz2
             .
             .
transaction-2012-02-28.csv.bz2

我有一个脚本调用script.sh

I have a script called script.sh

cat script.sh

YEAR_MONTH=$1
FILEPATH="transaction-$YEAR_MONTH*.csv.bz2"
bzcat $FILEPATH|strings|grep -v "code" >> output

如果你需要调用脚本也可以使用其他脚本

And if you need call the script you can use other script

cat script2.sh

LAST_MONTH=$(date -d -1month +%Y"-"%m)
if [ $# -eq 1 ]; then
  DATE=$1
else
  DATE=$LAST_MONTH  
fi

script.sh $DATE 1>output$DATE.csv 2>> log.txt

和它做猫的文件中一个月,但现在我需要调用一个特定星期脚本一年:

And it do cat the files in a month, but now i need call the script with a specific week in a year:

bash script2.sh 2012-01

,其中2012年和01月份

where 2012 is the year and 01 is the month

现在我需要调用脚本:

bash script2.sh 2012 13

,其中2012年和13周在一年

where 2012 is the year and 13 is the week in a year

现在我需要的猫只向用户指定的年份和星期的文件,没有按每周每月

Now i need the cat only to the files in the year and week that the user specified, no per month per week

但文件的格式不帮我!!!!因为这个名字是交易年 - 月 - day.csv.bz2,而不是交易年week.csv.bz2

But the format of the files do not help me!!!! because the name is transaction-year-month-day.csv.bz2, not transaction-year-week.csv.bz2

推荐答案

看看联机帮助页的的strftime 。这些日期格式codeS。例如:

Take a look at the manpage for strftime. These are date format codes. For example:

$ date +"%A, %B %e, %Y at %I:%m:%S %p"

将打印出像日:

Thursday, May 30, 2013 at 02:05:31 PM

试着明白为什么这个工程。

Try to see why this works.

部分的系统中,日期命令将有一个 -j 开关。这意味着,不设置日期,但重新格式化给定的日期。这可以让你一个日期转换为另一种:

On some systems, the date command will have a -j switch. This means, don't set the date, but reformat the given date. This allows you to convert one date to another:

$ date -f"$input_format" "$string_date" +"$output_format"

$ input_format 是输入日期的格式。 $ string_date 是字符串重新在 $ input_format 日期presentation。而且, $ OUTPUT_FORMAT 的格式是你想在你的日期。

The $input_format is the format of your input date. $string_date is the string representation of the date in your $input_format. And, $output_format is the format you want your date in.

前两个字段很容易。您的日期是 YY-MM-DD 格式:

The first two fields are easy. Your date is in YY-MM-DD format:

$ date -f"%Y-%m-%d" "$my_date_string"

问题是你能为最后的格式做。幸运的是,在一年中每周的格式。 %V 从而重新presents周在01-53和%的W 从而重新presents的周为 00-53

The question is what can you do for the final format. Fortunately, there is a format for the week in the year. %V which represents the weeks at 01-53 and %W which represents the weeks as 00-53.

您需要做的就是找​​到你的文件名,日期字符串,然后将其转换成年份和周数。如果是相同的输入,你需要连接这个文件。

What you need to do is find the date string on your file name, then convert that to the year and week number. If that's the same as the input, you need to concatenate this file.

find $dir -type f | while read transaction_file
do
    file_date=${transaction_file#transaction-}  #Removes the prefix
    file_date=${file_date%.csv.bz2}             #Removes the suffix
    weekdate=$(date -j -f"%Y-%m-%d" "$file_date" +"%Y %W")
    [ "$weekdate" -eq "$desired_date" ] || continue
    ...
done

例如,有人提出在 2013 05 作为所需的日期,你将通过您所有的文件,找到那些与你想要的日期范围。 注::这一年中的一周中填充零。您可能需要的填零的周数的输入相匹配。

For example, someone puts in 2013 05 as the desired date, you will go through all of your files and find ones with dates in the range you want. NOTE: That the week of the year is zero filled. You may need to zero fill the input of the week number to match.

这篇关于改变从bash脚本调用在一个月到一个星期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆