说我有:
> id|lastName|firstName|gender|birthday|creationDate|locationIP|browserUsed
>
> 13194139534963|Berty|Jean|male|1988-08-02|2012-04-02T08:33:15.012+0000|41.216.190.153|Google
> Chrome
>
> 13194139535544|Oliveira|Manuel|male|1984-10-31|2012-03-14T16:00:12.287+0000|109.71.166.230|Internet
> Explorer
>
> 13194139537327|Wei|Lei|male|1987-01-06|2012-03-13T03:07:51.899+0000|27.99.188.150|Internet
> Explorer
>
> 13194139539118|Alvarez|Monica|male|1989-10-17|2012-02-25T19:18:54.137+0000|190.169.213.242|Internet
> Explorer
>
> 13194139539746|Xu|Wei|female|1986-11-30|2012-03-19T23:16:12.495+0000|27.103.77.193|Firefox
我想用这些参数创建一个命令:./tool.sh - born-since dateAborn- until-until dateB -f file
1)如果出生日期和出生日期已经给出,我想在两个特定日期(年 - 月 - 日)之间打印所有出生的人(整行) 示例 ./tool.sh-born-自1988-08-02以来 - 出生于2012-09-13 -f档案 的输出:
13194139534963|Berty|Jean|male|1988-08-02|2012-04-02T08:33:15.012+0000|41.216.190.153|Google
13194139539118|Alvarez|Monica|male|1989-10-17|2012-02-25T19:18:54.137+0000|190.169.213.242|Internet
Explorer
2)如果只出生 - 从给出日期开始,我想列出所有人(全线)的出生日期和之后。 示例: ./tool.sh-born-自1988-08-02 -f档案 的输出: 与1)相同
3)如果只出生 - 直到日期,我想列出所有出生的人直到那个日期(再次关于他们的整行)。 ./tool.sh --born-直到1988-08-02 -f文件 的输出:
13194139535544|Oliveira|Manuel|male|1984-10-31|2012-03-14T16:00:12.287+0000|109.71.166.230|Internet Explorer
13194139537327|Wei|Lei|male|1987-01-06|2012-03-13T03:07:51.899+0000|27.99.188.150|Internet Explorer
13194139539746|Xu|Wei|female|1986-11-30|2012-03-19T23:16:12.495+0000|27.103.77.193|Firefox
我的代码是:
while [ $# -gt 0 ];do #Get and store Dates (Since-Until)
if [ "$1" = --born-since ];then
if [[ "$2" =~ $re ]];then #re='[0-9]-*' # Check if $2 is number
BSDate=$2
BSYear=$(echo "$BSDate" | awk -F '-' '{print $1}') # Get BSYear
BSMonth=$(echo "$BSDate" | awk -F '-' '{print $2}') # Get BSMonth
BSDay=$(echo "$BSDate" | awk -F '-' '{print $3}') # Get BSDay
fi
elif [ "$1" = --born-until ];then
if [[ "$2" =~ $re ]];then
BUDate=$2
BUYear=$(echo "$BUDate" | awk -F '-' '{print $1}') # Get BUYear
BUMonth=$(echo "$BUDate" | awk -F '-' '{print $2}') # Get BUMonth
BUDay=$(echo "$BUDate" | awk -F '-' '{print $3}') # Get BUDay
fi
fi
shift
done
if [ "$BSDate" ] && [ "$BUDate" ];then #If both date arguments exist
elif [ "$BSDate" ];then
elif [ "$BUDate" ];then
fi
如果我从1998-10-30进入--born-参数在awk中正确传递以进行评估,1998 = BSYear,10 = BSMonth,30 = BSDay。有人可以帮我实现awk部分吗?
答案 0 :(得分:1)
对于awk部分:
cat ./tool.sh
awk -F'|' -vs="$1" -ve="$2" '
BEGIN{if(!s)s="0000-00-00";if(!e)e="9999-99-99"}
NR>1 && $5>=s && $5<=e' infile
你称之为
./tool.sh '1987-01-06' '1988-08-02'
或
./tool.sh '' '1988-08-02'
或
./tool.sh '1987-01-06' ''
答案 1 :(得分:0)
我修复了:awk -F'|' '{if($ 5&gt; =“'$ BSDate'”&amp;&amp;&amp; $ 5&lt; =“'$ BUDate'”)