Reputation: 15
I call on Windows a list of datafiles in this way:
gawk -f datetab.awk datetab*.csv
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
1st datafile / input
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
The fields are delimited by tab.
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}
At the point there is the line $2=substr($2,1,11) in the sourcefile the pattern matching /start/ is broken. why?
output should be this:
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
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
Upvotes: 0
Views: 165
Reputation: 203209
awk scripts are a series of:
<condition> { <action> }
statements. Your code:
$2=substr($2,1,11)
gsub(/-/,".",$2)
is 2 conditions invoking the default action when true, equivalent to:
$2=substr($2,1,11) { print $0 }
gsub(/-/,".",$2) { print $0 }
You probably mean to write:
{
$2=substr($2,1,11)
gsub(/-/,".",$2)
}
so they're within an action part rather than a condition part of the script.
Upvotes: 2