awk模式匹配-substr现场操作问题 [英] awk pattern match - substr field action issue

查看:70
本文介绍了awk模式匹配-substr现场操作问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以这种方式在Windows上调用数据文件列表:

I call on Windows a list of datafiles in this way:

gawk -f datetab.awk datetab*.csv

所有数据文件的外观基本上与datatab1.csv相同.但是请记住,这是模范.重要的是在记录开始之前和之后都有一个未知的数字.开始后,我们需要所有记录.这里的时间戳显示在两个不同的列中($ 2和$ 3). $ 2的时间戳记应缩短/重新格式化为DD.MM.YYYY

All data files look mainly the same like datatab1.csv. But have in mind this is exemplary. Important is that there is an unknown number before and after the record start. We need all records after start. Here time stamps are shown in two different columns ($2 & $3). $2 timestamps shall be shortened / reformatted to 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是这样的:

sourcefile datetab.awk is this:

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/的模式已损坏. 为什么?

At the point there is the line $2=substr($2,1,11) in the sourcefile the pattern matching /start/ is broken. why?

输出应为:

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/模式的否定和某种形式的复制. sbustr和gsub操作添加行.我在此论坛中找到了与数据文件列表的每个文件匹配的模式从X行打印".我不明白为什么它不起作用.请在模式匹配/start/之后向我解释如何通过一些基本的字段操作在数据文件列表上使用awk.

In my case with the given code I got negation of the /start/ pattern and some sort of dublication. The sbustr and gsub action add lines. I found the pattern matching "print from line X" for each file of a list of datafiles here in this forum. I do not understand why it is not working. please explain to me how to work with awk on a list of datafiles with some rudimentary field manipulation after a pattern-matching /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

推荐答案

awk脚本是一系列的:

awk scripts are a series of:

<condition> { <action> }

声明.您的代码:

$2=substr($2,1,11)
gsub(/-/,".",$2)

是2个条件,当为true时将调用默认操作,等效于:

is 2 conditions invoking the default action when true, equivalent to:

$2=substr($2,1,11) { print $0 }
gsub(/-/,".",$2) { print $0 }

您可能想写:

{
    $2=substr($2,1,11)
    gsub(/-/,".",$2)
}

因此它们位于脚本的操作部分而不是条件部分.

so they're within an action part rather than a condition part of the script.

这篇关于awk模式匹配-substr现场操作问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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