我以这种方式在Windows上调用数据文件列表:
gawk -f datetab.awk datetab*.csv
所有数据文件看起来与datatab1.csv基本相同。但请记住,这是典型的。重要的是在记录开始之前和之后有一个未知的数字。我们在开始后需要所有记录。这里的时间戳显示在两个不同的列中($ 2和$ 3)。 $ 2时间戳应缩短/重新格式化为DD.MM.YYYY
第一个数据文件/输入
Rec not needed Rec not needed Rec not needed
Rec not needed Rec not needed Rec not needed
start
10-12-2014 06:47:59 10-12-2014 06:47:59
11-12-2014 10:17:44 11-12-2014 10:17:44
12-12-2014 10:37:44 12-12-2014 10:37:44
13-12-2014 10:00:32 13-12-2014 10:00:32
字段由制表符分隔。
sourcefile datetab.awk是这样的:
BEGIN { FS=OFS="\t"}
FNR==1 {p=0}
$2=substr($2,1,11) # shorten date to DD-MM-YYYY
gsub(/-/,".",$2) # replace - by . --> DD.MM.YYYY
# (x=index($2," ") > 0) {
# DDMMYY = substr($2,1,x-1);
#};
p!=0{print};
/start/{p=1}
在源文件中有$ 2 = substr($ 2,1,11)行,模式匹配/ start /被破坏。 为什么呢?
输出应该是这样的:
10.12.2014 10-12-2014 06:47:59
11.12.2014 11-12-2014 10:17:44
12.12.2014 12-12-2014 10:37:44
13.12.2014 13-12-2014 10:00:32
在我使用给定代码的情况下,我得到了/ start / pattern和某种出版的否定。 sbustr和gsub动作添加了行。我发现模式匹配"从X行和#34;对于此论坛中的数据文件列表的每个文件。我不明白为什么它不起作用。请向我解释如何在模式匹配/开始/之后如何在数据文件列表上使用awk并进行一些基本的字段操作。
Rec not needed Rec not nee Rec not needed
Rec not needed Rec not nee Rec not needed
Rec not needed Rec not nee Rec not needed
Rec not needed Rec not nee Rec not needed
10-12-2014 10-12-2014 06:47:59
10.12.2014 10-12-2014 06:47:59
10.12.2014 10-12-2014 06:47:59
11-12-2014 11-12-2014 10:17:44
11.12.2014 11-12-2014 10:17:44
11.12.2014 11-12-2014 10:17:44
12-12-2014 12-12-2014 10:37:44
12.12.2014 12-12-2014 10:37:44
12.12.2014 12-12-2014 10:37:44
13-12-2014 13-12-2014 10:00:32
13.12.2014 13-12-2014 10:00:32
13.12.2014 13-12-2014 10:00:32
答案 0 :(得分:2)
awk脚本是一系列的:
<condition> { <action> }
语句。你的代码:
$2=substr($2,1,11)
gsub(/-/,".",$2)
是两个条件,当为true时调用默认操作,相当于:
$2=substr($2,1,11) { print $0 }
gsub(/-/,".",$2) { print $0 }
你可能想写:
{
$2=substr($2,1,11)
gsub(/-/,".",$2)
}
所以他们在行动部分而不是剧本的条件部分。