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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Stamp

Status
Not open for further replies.

mrn

MIS
Apr 27, 2001
3,993
GB
I have a file in the following format

11:36:08 hdisk7 3 0.0 7 104
hdisk20 9 0.0 14 143
hdisk6 0 0.0 0 26
hdisk3 1 0.0 2 35
hdisk1 0 0.0 1 29
hdisk12 0 0.0 0 7

11:37:09 hdisk7 .....

I would like the format to be

11:36:08 hdisk7 3 0.0 7 104
11:36:08 hdisk20 9 0.0 14 143
11:36:08 hdisk6 0 0.0 0 26
11:36:08 hdisk3 1 0.0 2 35
11:36:08 hdisk1 0 0.0 1 29
11:36:08 hdisk12 0 0.0 0 7
11:37:08 hdisk7 3 0.0 7 104
11:37:08 hdisk20 9 0.0 14 143
11:37:08 hdisk6 0 0.0 0 26
11:37:08 hdisk3 1 0.0 2 35
11:37:08 hdisk1 0 0.0 1 29
11:37:08 hdisk12 0 0.0 0 7

How would I go about this using awk?


--
| Mike Nixon
| Unix Admin
| ----------------------------
 
Have you tried something like this?

awk ' {
if ($0 ~ /^[0-9]+:.*/) {
stamp = substr($0,1,9)
}
printf "%s %s\n", stamp, $0
}'
 
just noticed a couple of things

11:35:08 11:35:08 device %busy avque r+w/s blks/s avwait avs
erv
11:35:08
11:36:08 11:36:08 hdisk7 3 0.0 7 104 0.0 0.
0
11:36:08 hdisk20 9 0.0 14 143 0.0 0.
0
11:36:08 hdisk6 0 0.0 0 26 0.0 0.
0
11:36:08 hdisk3 1 0.0 2 35 0.0 0.
0
11:36:08 hdisk1

If line already has date stamp, it's being repeated, is there a way of stopping this? --
| Mike Nixon
| Unix Admin
| ----------------------------
 
#! /bin/awk -f
{
if ($0 ~ /^[0-9]+:.* /){stamp=substr($0,1,9);print}
else if (NF < 1){next}
else {printf &quot;%s %s\n&quot;, stamp, $0}
}


# skip the else if lines if there aren't blank lines in the data file

-jim
 
Well now it's starts to look a little ugly, but...

awk ' {
if ($0 ~ /^[0-9]+:.*/) {
stamp = substr($0,1,9)
$1 = &quot;&quot; ; OFS = &quot;\t&quot;
}
if (length($0)) { print stamp, $0 }
}'

Here's what I get from my sample:
11:36:08 hdisk7 3 0.0 7 104
11:36:08 hdisk20 9 0.0 14 143
11:36:08 hdisk6 0 0.0 0 26
11:36:08 hdisk3 1 0.0 2 35
11:36:08 hdisk1 0 0.0 1 29
11:36:08 hdisk12 0 0.0 0 7
11:39:08 hdisk7 3 0.0 7 104
11:39:08 hdisk20 9 0.0 14 143
11:39:08 hdisk6 0 0.0 0 26
11:39:08 hdisk3 1 0.0 2 35
11:39:08 hdisk1 0 0.0 1 29
11:39:08 hdisk12 0 0.0 0 7
11:51:08 hdisk7 3 0.0 7 104
11:51:08 hdisk20 9 0.0 14 143
11:51:08 hdisk6 0 0.0 0 26
11:51:08 hdisk3 1 0.0 2 35
11:51:08 hdisk1 0 0.0 1 29
11:51:08 hdisk12 0 0.0 0 7
 
This lines up the columns better than the 1st one:

#! /bin/awk -f
{
if ($0 ~ /^[0-9]+:.* /){stamp=substr($0,1,8);print}
else if (NF < 1){print}
else {printf &quot;%s %s\n&quot;, stamp, substr($0,11,60)}
}

# Change 'else if (NF < 1){print}' to:
# 'else if (NF < 1){next}' to skip printing
# of blank lines

-jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top