Bash Awk:打印特定值范围
假设我有:Bash Awk:打印特定值范围,bash,awk,Bash,Awk,假设我有: > 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|19
> 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-dateA--born-until-dateB-f文件
1) 如果给出了出生日期和出生日期,我想打印两个特定日期(年-月-日)之间的所有出生日期(整行)
示例
./tool.sh——自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
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
2) 如果只出生日期是给定的,我想列出所有的人(整行)与该日期和之后的出生日期。
示例:
./tool.sh——自1988-08-02年出生-f文件
输出:
同1)
3) 如果只在指定日期之前出生,我想列出所有在该日期之前出生的人(同样是关于他们的整行)。
./tool.sh--1988-08-02年出生-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
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
如果我输入--born since 1998-10-30,参数将正确通过awk中的计算,1998=BSYear,10=BSMonth,30=BSDay。有人能帮我实现awk部分吗?对于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
或
或
我用:awk-F'|'{if($5>=“$BSDate')修复了它&&&$5您输入的
1998-10-30
生日中没有任何日期等于或晚于1998-10-30
生日。您编写的它工作正常-但它应该不会输出我想要的任何内容。您的观点是?此程序将在任何.dat文件上运行。如果我输入./tool.sh--bor,您能显示/tool.sh--1987-01-01出生的预期结果吗n-自1987-01-01以来,它输出无效的命令,因为它不是有效的格式(需要如我所述)。但是,如果我正确地输出它,我什么也得不到,只是在变量上传递了日期(born-since),如代码所示)我想在awk中分别检查它们的值。这是我的主意。我需要帮助。我不是问你当前的输出,而是问你预期的结果。它必须被称为./tool.sh--born since dateA--born since dateB-f文件。希望我能这样做。我试过awk-f'|'{如果($5>=$BSDate&&$5将$1替换为$2,将$2替换为$4,将infle替换为$6,并这样称呼:./tool.sh--自'1987-01-06'出生--出生至'1988-08-02'-f infle
./tool.sh '1987-01-06' ''