【发布时间】:2017-07-12 14:54:26
【问题描述】:
我以这种方式在 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
字段由制表符分隔。
源文件 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/ 模式的否定和某种重复。 sbusr 和 gsub 操作添加行。我在这个论坛中为数据文件列表中的每个文件找到了匹配“从第 X 行打印”的模式。我不明白为什么它不起作用。请向我解释如何在数据文件列表上使用 awk,并在模式匹配 /start/ 之后进行一些基本的字段操作。
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
【问题讨论】:
-
这些行
Rec not needed是否存在于所有文件中? -
Rec not needed 是匹配 /start/ 前未知数量记录的示例。这些记录不是必需的,并且存在于所有数据文件中。
-
start行和后续行之后是否有任何行?start模式之后总是有 4 行吗? -
Roman,这里给出的只是一个尽可能简短的例子。真正的数据文件在开始行之前和之后的行数未知。
-
目前我不能,因为我在 Windows 上。我将在虚拟机上安装 Ubuntu/Debian 以克服可能的不兼容问题。无论如何,我将 LibreOffice 中的 datetab1.csv 保存为制表符分隔的 csv/txt 文件。