Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk! Did they forget the H? How can I make it do... 2

Status
Not open for further replies.

DCCoolBreeze

Programmer
Jul 25, 2001
208
US
OK. I have never used awk so please be patient. I want to extract some data from 300+ files. The files are in the same format. I need to extract parts of lines within each file. The two lines I need to extract data from are listed below:

"Starting Processing on Sat Feb 1 19:00:41 EST 2003"
I need to extract the date in mm/dd/yy hh:mm:ss

"Stage n Success 02/01/2003 19.16.53"
I need to extract the 3 columns: Stage, Status and Date

I will be importing this data into a database once I extract it from each file so I would like to write it to a file in comma delimited form. For example:

Stage 0,Start,02/01/2003 19:00:41
Stage 1,Success,02/01/2003 19:16:53
...
Stage n,Status,mm/dd/yy hh:mm:ss

How would I do this???
 
This should get you started.

/^Starting/ {
mm=mo[$5]
printf ("Stage 0,Start,%02d/%02d/%04d %s\n",mm,$6,$9,$7)
}
/^Stage/ {
gsub (/\./,":",$5)
print $1 " " $2,$3,$4 " " $5
}

BTW, awk was named from the initials of its creators, Aho, Weinberger and Kernigan.

CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
CaKiwi, deliberating no BEGIN section with some split to fill the mo array ?
 
Well spotted PHV, cut and paste error.

BEGIN {
mo["Jan"]= 1;mo["Feb"]= 2;mo["Mar"]= 3;mo["Apr"]= 4;
mo["May"]= 5;mo["Jun"]= 6;mo["Jul"]= 7;mo["Aug"]= 8;
mo["Sep"]= 9;mo["Oct"]=10;mo["Nov"]=11;mo["Dec"]=12;
OFS=","
}
/^Starting/ {
mm=mo[$5]
printf ("Stage 0,Start,%02d/%02d/%04d %s\n",mm,$6,$9,$7)
}
/^Stage/ {
gsub (/\./,":",$5)
print $1 " " $2,$3,$4 " " $5
}

CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
Ok, now I'll turn off TGML

BEGIN {
mo["Jan"]= 1;mo["Feb"]= 2;mo["Mar"]= 3;mo["Apr"]= 4;
mo["May"]= 5;mo["Jun"]= 6;mo["Jul"]= 7;mo["Aug"]= 8;
mo["Sep"]= 9;mo["Oct"]=10;mo["Nov"]=11;mo["Dec"]=12;
OFS=","
}
/^Starting/ {
mm=mo[$5]
printf ("Stage 0,Start,%02d/%02d/%04d %s\n",mm,$6,$9,$7)
}
/^Stage/ {
gsub (/\./,":",$5)
print $1 " " $2,$3,$4 " " $5
}

CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
you could always do:
Code:
BEGIN{
months="Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec";
split(months,mo,",");
}

which i think looks nicer :)
 
Jad,

Your split method does not do what I want in this case. It is the equivalent of

mo[1] = "Jan"; ...

I need

mo["Jan"] = 1; ...

CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
gotcha, hmm ...

yeah, i can't think of anyway to do that :) yours works well.
 
split and 'inverse' the array making it 'associative' indexed by month names.

Involves a 'split' and an 'inversing' loop, but..... just an idea.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
i had thought of that, but it didn't look as pretty :)
Code:
BEGIN{
months=&quot;Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec&quot;;
num=split(months,mon,&quot;,&quot;);
for (i=0;i<=num;i++){mo[mon[i]]=i}
}

actually it's not that bad.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top