looking for scripting solution

looking for scripting solution

am 15.01.2008 17:49:51 von EdStevens

Platform: HP-UX 11

Looking for means of tracking file system growth over time. What I
envision is redirecting the output of 'bdf', appending into a single
file, but I need to add the system date to each record. Something
like this:

Of course, bdf gives this output:

/dev/vg00/lvol3 1032192 309920 716688 30% /
/dev/vg00/lvol1 1032192 217168 808752 21% /stand
/dev/vg00/lvol8 7667712 6132568 1524152 80% /var
/dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
/dev/vg00/lvol6 3080192 17656 3038936 1% /tmp

etc .....

What I'd like to get is

20080115 /dev/vg00/lvol3 1032192 309920 716688 30% /
20080115 /dev/vg00/lvol1 1032192 217168 808752 21% /stand
20080115 /dev/vg00/lvol8 7667712 6132568 1524152 80% /var
20080115 /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
20080115 /dev/vg00/lvol6 3080192 17656 3038936 1% /tmp

where that first field is the current day, shown here in yyyymmdd
format. Of course the actual format of the date is not critical, and
I can play with it once I find a way of getting it into the output...

Thanks.

Re: looking for scripting solution

am 15.01.2008 18:09:14 von Ed Morton

On 1/15/2008 10:49 AM, EdStevens wrote:
> Platform: HP-UX 11
>
> Looking for means of tracking file system growth over time. What I
> envision is redirecting the output of 'bdf', appending into a single
> file, but I need to add the system date to each record. Something
> like this:
>
> Of course, bdf gives this output:
>
> /dev/vg00/lvol3 1032192 309920 716688 30% /
> /dev/vg00/lvol1 1032192 217168 808752 21% /stand
> /dev/vg00/lvol8 7667712 6132568 1524152 80% /var
> /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
> /dev/vg00/lvol6 3080192 17656 3038936 1% /tmp
>
> etc .....
>
> What I'd like to get is
>
> 20080115 /dev/vg00/lvol3 1032192 309920 716688 30% /
> 20080115 /dev/vg00/lvol1 1032192 217168 808752 21% /stand
> 20080115 /dev/vg00/lvol8 7667712 6132568 1524152 80% /var
> 20080115 /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
> 20080115 /dev/vg00/lvol6 3080192 17656 3038936 1% /tmp
>
> where that first field is the current day, shown here in yyyymmdd
> format. Of course the actual format of the date is not critical, and
> I can play with it once I find a way of getting it into the output...
>
> Thanks.

Try this:

date=$(date +%Y%m%d)
bdf | sed "s/^/$date /"

Ed.

Re: looking for scripting solution

am 15.01.2008 18:18:17 von Ed Morton

On 1/15/2008 11:23 AM, pk wrote:
> EdStevens wrote:
>
>
>>Platform: HP-UX 11
>>
>>Looking for means of tracking file system growth over time. What I
>>envision is redirecting the output of 'bdf', appending into a single
>>file, but I need to add the system date to each record. Something
>>like this:
>>
>>Of course, bdf gives this output:
>>
>>/dev/vg00/lvol3 1032192 309920 716688 30% /
>>/dev/vg00/lvol1 1032192 217168 808752 21% /stand
>>/dev/vg00/lvol8 7667712 6132568 1524152 80% /var
>>/dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
>>/dev/vg00/lvol6 3080192 17656 3038936 1% /tmp
>>
>>etc .....
>>
>>What I'd like to get is
>>
>>20080115 /dev/vg00/lvol3 1032192 309920 716688 30% /
>>20080115 /dev/vg00/lvol1 1032192 217168 808752 21% /stand
>>20080115 /dev/vg00/lvol8 7667712 6132568 1524152 80% /var
>>20080115 /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
>>20080115 /dev/vg00/lvol6 3080192 17656 3038936 1% /tmp
>>
>>where that first field is the current day, shown here in yyyymmdd
>>format. Of course the actual format of the date is not critical, and
>>I can play with it once I find a way of getting it into the output...
>
>
> You can run something like this periodically (works with bash):
>
> d=`date +%Y%m%d`
> bdf | awk '{ print '$d' " " $0}' >> logfile

That can lead to all sorts of problems with various contents of "d". A correct
way to pass shell variable values to awk is:

d=`date +%Y%m%d`
bdf | awk -v d="$d" '{print d,$0}' >> logfile

or if you have GNU awk:

bdf | awk 'BEGIN{d=strftime("%Y%m%d)} {print d,$0}' >> logfile

> or, with sed, the second line becomes
>
> bdf | sed -e 's/\(^.*$\)/'$d' \1/g' >> logfile

bdf | sed "s/^/$d /" >> logfile

Regaqrds,

Ed.

Re: looking for scripting solution

am 15.01.2008 18:23:58 von PK

EdStevens wrote:

> Platform: HP-UX 11
>
> Looking for means of tracking file system growth over time. What I
> envision is redirecting the output of 'bdf', appending into a single
> file, but I need to add the system date to each record. Something
> like this:
>
> Of course, bdf gives this output:
>
> /dev/vg00/lvol3 1032192 309920 716688 30% /
> /dev/vg00/lvol1 1032192 217168 808752 21% /stand
> /dev/vg00/lvol8 7667712 6132568 1524152 80% /var
> /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
> /dev/vg00/lvol6 3080192 17656 3038936 1% /tmp
>
> etc .....
>
> What I'd like to get is
>
> 20080115 /dev/vg00/lvol3 1032192 309920 716688 30% /
> 20080115 /dev/vg00/lvol1 1032192 217168 808752 21% /stand
> 20080115 /dev/vg00/lvol8 7667712 6132568 1524152 80% /var
> 20080115 /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
> 20080115 /dev/vg00/lvol6 3080192 17656 3038936 1% /tmp
>
> where that first field is the current day, shown here in yyyymmdd
> format. Of course the actual format of the date is not critical, and
> I can play with it once I find a way of getting it into the output...

You can run something like this periodically (works with bash):

d=`date +%Y%m%d`
bdf | awk '{ print '$d' " " $0}' >> logfile

or, with sed, the second line becomes

bdf | sed -e 's/\(^.*$\)/'$d' \1/g' >> logfile

Of course, set the date format that you like best.

Re: looking for scripting solution

am 15.01.2008 18:26:00 von PK

pk wrote:

> or, with sed, the second line becomes
>
> bdf | sed -e 's/\(^.*$\)/'$d' \1/g' >> logfile

I think

bdf | sed -e 's/\(.*\)/'$d' \1/g' >> logfile

should also work, because of greedy matching.

Re: looking for scripting solution

am 15.01.2008 18:28:15 von PK

Ed Morton wrote:

> Try this:
>
> date=$(date +%Y%m%d)
> bdf | sed "s/^/$date /"

Heh, I didn't think about just substituting the beginning of line, but of
course that is perfectly obvious. This looks like a more elegant solution
than mine, thanks.

Re: looking for scripting solution

am 15.01.2008 18:37:27 von Ed Morton

On 1/15/2008 11:39 AM, pk wrote:
> Ed Morton wrote:
>
>
>>>d=`date +%Y%m%d`
>>>bdf | awk '{ print '$d' " " $0}' >> logfile
>>
>>That can lead to all sorts of problems with various contents of "d". A
>>correct way to pass shell variable values to awk is:
>>
>>d=`date +%Y%m%d`
>>bdf | awk -v d="$d" '{print d,$0}' >> logfile
>>
>>or if you have GNU awk:
>>
>>bdf | awk 'BEGIN{d=strftime("%Y%m%d)} {print d,$0}' >> logfile
>
>
> Agreed. I wrote that thinking that $d would only contain digits, but that
> might not always be the case. You're correct, thanks for pointing that out.
>
>
>>>or, with sed, the second line becomes
>>>
>>>bdf | sed -e 's/\(^.*$\)/'$d' \1/g' >> logfile
>>
>>bdf | sed "s/^/$d /" >> logfile
>
>
> Yes, I alredy noted that in my other answer. Mine was unnecessarily
> complicated (and probably less efficient too), and still has the $d issue.
>

unfortunately I don't think there's anything you can do with sed to avoid that
(I stand to be corrected!) so I'm always a little wary of using variables in sed
scripts and only use them when I'm either certain of the contents or just don't
care if it bombs. In this particular case, I personally would just use the sed
script.

Ed.

Re: looking for scripting solution

am 15.01.2008 18:39:10 von PK

Ed Morton wrote:

>> d=`date +%Y%m%d`
>> bdf | awk '{ print '$d' " " $0}' >> logfile
>
> That can lead to all sorts of problems with various contents of "d". A
> correct way to pass shell variable values to awk is:
>
> d=`date +%Y%m%d`
> bdf | awk -v d="$d" '{print d,$0}' >> logfile
>
> or if you have GNU awk:
>
> bdf | awk 'BEGIN{d=strftime("%Y%m%d)} {print d,$0}' >> logfile

Agreed. I wrote that thinking that $d would only contain digits, but that
might not always be the case. You're correct, thanks for pointing that out.

>> or, with sed, the second line becomes
>>
>> bdf | sed -e 's/\(^.*$\)/'$d' \1/g' >> logfile
>
> bdf | sed "s/^/$d /" >> logfile

Yes, I alredy noted that in my other answer. Mine was unnecessarily
complicated (and probably less efficient too), and still has the $d issue.

Re: looking for scripting solution

am 15.01.2008 18:46:00 von Michael Tosch

EdStevens wrote:
> Platform: HP-UX 11
>
> Looking for means of tracking file system growth over time. What I
> envision is redirecting the output of 'bdf', appending into a single
> file, but I need to add the system date to each record. Something
> like this:
>
> Of course, bdf gives this output:
>
> /dev/vg00/lvol3 1032192 309920 716688 30% /
> /dev/vg00/lvol1 1032192 217168 808752 21% /stand
> /dev/vg00/lvol8 7667712 6132568 1524152 80% /var
> /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
> /dev/vg00/lvol6 3080192 17656 3038936 1% /tmp
>
> etc .....
>
> What I'd like to get is
>
> 20080115 /dev/vg00/lvol3 1032192 309920 716688 30% /
> 20080115 /dev/vg00/lvol1 1032192 217168 808752 21% /stand
> 20080115 /dev/vg00/lvol8 7667712 6132568 1524152 80% /var
> 20080115 /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
> 20080115 /dev/vg00/lvol6 3080192 17656 3038936 1% /tmp
>
> where that first field is the current day, shown here in yyyymmdd
> format. Of course the actual format of the date is not critical, and
> I can play with it once I find a way of getting it into the output...
>
> Thanks.

d=`date +%Y%m%d`
bdf |
while read line; do
echo "$d $line"
done

--
Michael Tosch @ hp : com

Re: looking for scripting solution

am 15.01.2008 19:50:43 von EdStevens

On Jan 15, 11:23 am, pk wrote:
> EdStevens wrote:
> > Platform: HP-UX 11
>
> > Looking for means of tracking file system growth over time. What I
> > envision is redirecting the output of 'bdf', appending into a single
> > file, but I need to add the system date to each record. Something
> > like this:
>
> > Of course, bdf gives this output:
>
> > /dev/vg00/lvol3 1032192 309920 716688 30% /
> > /dev/vg00/lvol1 1032192 217168 808752 21% /stand
> > /dev/vg00/lvol8 7667712 6132568 1524152 80% /var
> > /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
> > /dev/vg00/lvol6 3080192 17656 3038936 1% /tmp
>
> > etc .....
>
> > What I'd like to get is
>
> > 20080115 /dev/vg00/lvol3 1032192 309920 716688 30% /
> > 20080115 /dev/vg00/lvol1 1032192 217168 808752 21% /stand
> > 20080115 /dev/vg00/lvol8 7667712 6132568 1524152 80% /var
> > 20080115 /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
> > 20080115 /dev/vg00/lvol6 3080192 17656 3038936 1% /tmp
>
> > where that first field is the current day, shown here in yyyymmdd
> > format. Of course the actual format of the date is not critical, and
> > I can play with it once I find a way of getting it into the output...
>
> You can run something like this periodically (works with bash):
>
> d=`date +%Y%m%d`
> bdf | awk '{ print '$d' " " $0}' >> logfile
>
> or, with sed, the second line becomes
>
> bdf | sed -e 's/\(^.*$\)/'$d' \1/g' >> logfile
>
> Of course, set the date format that you like best.

Interesting solutions all ....

FWIW, the awk solution returned the following:

2.00801e+07 /dev/vg00/lvol3 1032192 310056 716552 30% /
2.00801e+07 /dev/vg00/lvol1 1032192 217168 808752 21% /stand
2.00801e+07 /dev/vg00/lvol8 7667712 6114568 1542016 80% /var
2.00801e+07 /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
2.00801e+07 /dev/vg00/lvol6 3080192 17680 3038912 1% /tmp

..... etc.



I tried this with sh, bash, and ksh and got the same results with
each. All of the sed based solutions worked equally well, though I
didn't play around with various formatting from date to see how that
would impact.

Thanks to all who contributed - both to my immediate problem and my
learning.

- Ed Stevens

Re: looking for scripting solution

am 15.01.2008 21:34:05 von Ed Morton

On 1/15/2008 12:50 PM, EdStevens wrote:
> On Jan 15, 11:23 am, pk wrote:
>
>>EdStevens wrote:
>>
>>>Platform: HP-UX 11
>>
>>>Looking for means of tracking file system growth over time. What I
>>>envision is redirecting the output of 'bdf', appending into a single
>>>file, but I need to add the system date to each record. Something
>>>like this:
>>
>>>Of course, bdf gives this output:
>>
>>>/dev/vg00/lvol3 1032192 309920 716688 30% /
>>>/dev/vg00/lvol1 1032192 217168 808752 21% /stand
>>>/dev/vg00/lvol8 7667712 6132568 1524152 80% /var
>>>/dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
>>>/dev/vg00/lvol6 3080192 17656 3038936 1% /tmp
>>
>>>etc .....
>>
>>>What I'd like to get is
>>
>>>20080115 /dev/vg00/lvol3 1032192 309920 716688 30% /
>>>20080115 /dev/vg00/lvol1 1032192 217168 808752 21% /stand
>>>20080115 /dev/vg00/lvol8 7667712 6132568 1524152 80% /var
>>>20080115 /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
>>>20080115 /dev/vg00/lvol6 3080192 17656 3038936 1% /tmp
>>
>>>where that first field is the current day, shown here in yyyymmdd
>>>format. Of course the actual format of the date is not critical, and
>>>I can play with it once I find a way of getting it into the output...
>>
>>You can run something like this periodically (works with bash):
>>
>>d=`date +%Y%m%d`
>>bdf | awk '{ print '$d' " " $0}' >> logfile
>>
>>or, with sed, the second line becomes
>>
>>bdf | sed -e 's/\(^.*$\)/'$d' \1/g' >> logfile
>>
>>Of course, set the date format that you like best.
>
>
> Interesting solutions all ....
>
> FWIW, the awk solution returned the following:
>
> 2.00801e+07 /dev/vg00/lvol3 1032192 310056 716552 30% /
> 2.00801e+07 /dev/vg00/lvol1 1032192 217168 808752 21% /stand
> 2.00801e+07 /dev/vg00/lvol8 7667712 6114568 1542016 80% /var
> 2.00801e+07 /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
> 2.00801e+07 /dev/vg00/lvol6 3080192 17680 3038912 1% /tmp
>
> .... etc.

Which awk solution? 3 have been discussed:

1) d=`date +%Y%m%d`
bdf | awk '{ print '$d' " " $0}'

2) d=`date +%Y%m%d`
bdf | awk -v d="$d" '{print d,$0}'

3) bdf | gawk 'BEGIN{d=strftime("%Y%m%d)} {print d,$0}'

Also, which version of awk are you using (awk --version)?

The format awk uses to output numbers via print is controlled by the "OFMT"
variable, so you could also do this:

awk 'BEGIN{print OFMT; exit}'

to see what that's set to, or use printf instead of print to manually specify an
output format.

Ed.

Re: looking for scripting solution

am 16.01.2008 09:53:25 von Michael Tosch

Ed Morton wrote:
>
> On 1/15/2008 12:50 PM, EdStevens wrote:
>> On Jan 15, 11:23 am, pk wrote:
>>
>>> EdStevens wrote:
>>>
>>>> Platform: HP-UX 11
>>>> Looking for means of tracking file system growth over time. What I
>>>> envision is redirecting the output of 'bdf', appending into a single
>>>> file, but I need to add the system date to each record. Something
>>>> like this:
>>>> Of course, bdf gives this output:
>>>> /dev/vg00/lvol3 1032192 309920 716688 30% /
>>>> /dev/vg00/lvol1 1032192 217168 808752 21% /stand
>>>> /dev/vg00/lvol8 7667712 6132568 1524152 80% /var
>>>> /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
>>>> /dev/vg00/lvol6 3080192 17656 3038936 1% /tmp
>>>> etc .....
>>>> What I'd like to get is
>>>> 20080115 /dev/vg00/lvol3 1032192 309920 716688 30% /
>>>> 20080115 /dev/vg00/lvol1 1032192 217168 808752 21% /stand
>>>> 20080115 /dev/vg00/lvol8 7667712 6132568 1524152 80% /var
>>>> 20080115 /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
>>>> 20080115 /dev/vg00/lvol6 3080192 17656 3038936 1% /tmp
>>>> where that first field is the current day, shown here in yyyymmdd
>>>> format. Of course the actual format of the date is not critical, and
>>>> I can play with it once I find a way of getting it into the output...
>>> You can run something like this periodically (works with bash):
>>>
>>> d=`date +%Y%m%d`
>>> bdf | awk '{ print '$d' " " $0}' >> logfile
>>>
>>> or, with sed, the second line becomes
>>>
>>> bdf | sed -e 's/\(^.*$\)/'$d' \1/g' >> logfile
>>>
>>> Of course, set the date format that you like best.
>>
>> Interesting solutions all ....
>>
>> FWIW, the awk solution returned the following:
>>
>> 2.00801e+07 /dev/vg00/lvol3 1032192 310056 716552 30% /
>> 2.00801e+07 /dev/vg00/lvol1 1032192 217168 808752 21% /stand
>> 2.00801e+07 /dev/vg00/lvol8 7667712 6114568 1542016 80% /var
>> 2.00801e+07 /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
>> 2.00801e+07 /dev/vg00/lvol6 3080192 17680 3038912 1% /tmp
>>
>> .... etc.
>
> Which awk solution? 3 have been discussed:
>

I doubt this is helpful.

what /usr/bin/awk
/usr/bin/awk:
$Revision: 92453-07 linker linker crt0.o B.11.16.01 030415 $
$Revision: B11.23.0409LR

The following works with all awk versions, and avoids to re-print
the date when bdf decides to split into two lines.

bdf | awk '$1~/\//{printf "%s ",d}{print}' d=`date +%Y%m%d`


--
Michael Tosch @ hp : com

Re: looking for scripting solution

am 16.01.2008 15:20:25 von EdStevens

On Jan 15, 2:34 pm, Ed Morton wrote:
> On 1/15/2008 12:50 PM, EdStevens wrote:
>
>
>
> > On Jan 15, 11:23 am, pk wrote:
>
> >>EdStevens wrote:
>
> >>>Platform: HP-UX 11
>
> >>>Looking for means of tracking file system growth over time. What I
> >>>envision is redirecting the output of 'bdf', appending into a single
> >>>file, but I need to add the system date to each record. Something
> >>>like this:
>
> >>>Of course, bdf gives this output:
>
> >>>/dev/vg00/lvol3 1032192 309920 716688 30% /
> >>>/dev/vg00/lvol1 1032192 217168 808752 21% /stand
> >>>/dev/vg00/lvol8 7667712 6132568 1524152 80% /var
> >>>/dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
> >>>/dev/vg00/lvol6 3080192 17656 3038936 1% /tmp
>
> >>>etc .....
>
> >>>What I'd like to get is
>
> >>>20080115 /dev/vg00/lvol3 1032192 309920 716688 30% /
> >>>20080115 /dev/vg00/lvol1 1032192 217168 808752 21% /stand
> >>>20080115 /dev/vg00/lvol8 7667712 6132568 1524152 80% /var
> >>>20080115 /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
> >>>20080115 /dev/vg00/lvol6 3080192 17656 3038936 1% /tmp
>
> >>>where that first field is the current day, shown here in yyyymmdd
> >>>format. Of course the actual format of the date is not critical, and
> >>>I can play with it once I find a way of getting it into the output...
>
> >>You can run something like this periodically (works with bash):
>
> >>d=`date +%Y%m%d`
> >>bdf | awk '{ print '$d' " " $0}' >> logfile
>
> >>or, with sed, the second line becomes
>
> >>bdf | sed -e 's/\(^.*$\)/'$d' \1/g' >> logfile
>
> >>Of course, set the date format that you like best.
>
> > Interesting solutions all ....
>
> > FWIW, the awk solution returned the following:
>
> > 2.00801e+07 /dev/vg00/lvol3 1032192 310056 716552 30% /
> > 2.00801e+07 /dev/vg00/lvol1 1032192 217168 808752 21% /stand
> > 2.00801e+07 /dev/vg00/lvol8 7667712 6114568 1542016 80% /var
> > 2.00801e+07 /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
> > 2.00801e+07 /dev/vg00/lvol6 3080192 17680 3038912 1% /tmp
>
> > .... etc.
>
> Which awk solution? 3 have been discussed:
>
> 1) d=`date +%Y%m%d`
> bdf | awk '{ print '$d' " " $0}'
>
> 2) d=`date +%Y%m%d`
> bdf | awk -v d="$d" '{print d,$0}'
>
> 3) bdf | gawk 'BEGIN{d=strftime("%Y%m%d)} {print d,$0}'
>
> Also, which version of awk are you using (awk --version)?
>
> The format awk uses to output numbers via print is controlled by the "OFMT"
> variable, so you could also do this:
>
> awk 'BEGIN{print OFMT; exit}'
>
> to see what that's set to, or use printf instead of print to manually specify an
> output format.
>
> Ed.

Ah, so there were .... sorry ...

Not to belabor the point, as I have my solution and learned much in
the process, but here are the results of the three awk based
solutions:

ngtndp02:stevense> bdf | awk '{ print '$d' " " $0}'
2.00801e+07 Filesystem kbytes used avail %used Mounted
on
2.00801e+07 /dev/vg00/lvol3 1032192 310056 716552 30% /
2.00801e+07 /dev/vg00/lvol1 1032192 217168 808752 21% /stand
2.00801e+07 /dev/vg00/lvol8 7667712 6144368 1512456 80% /var
2.00801e+07 /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
2.00801e+07 /dev/vg00/lvol6 3080192 17712 3038880 1% /tmp


ngtndp02:stevense> d=`date +%Y%m%d`
ngtndp02:stevense> bdf | awk -v d="$d" '{print d,$0}'
20080116 Filesystem kbytes used avail %used Mounted on
20080116 /dev/vg00/lvol3 1032192 310056 716552 30% /
20080116 /dev/vg00/lvol1 1032192 217168 808752 21% /stand
20080116 /dev/vg00/lvol8 7667712 6144368 1512456 80% /var
20080116 /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
20080116 /dev/vg00/lvol6 3080192 17712 3038880 1% /tmp


ngtndp02:stevense> bdf | awk 'BEGIN{d=strftime("%Y%m%d)} {print d,$0}'
syntax error The source line is 1.
The error context is
BEGIN{d=strftime("%Y%m%d)} {print >>> d,$0} <<<
awk: The statement cannot be correctly parsed.
The source line is 1.
awk: There is a missing } character.
awk: There is a missing ) character.

ngtndp02:stevense> awk --version
Usage: awk [-F fs][-v Assignment][-f Progfile|Program][Assignment|
File] ...

Re: looking for scripting solution

am 16.01.2008 15:46:51 von Ed Morton

On 1/16/2008 8:20 AM, EdStevens wrote:
> On Jan 15, 2:34 pm, Ed Morton wrote:
>
>>On 1/15/2008 12:50 PM, EdStevens wrote:
>>
>>
>>
>>
>>>On Jan 15, 11:23 am, pk wrote:
>>
>>>>EdStevens wrote:
>>>
>>>>>Platform: HP-UX 11
>>>>
>>>>>Looking for means of tracking file system growth over time. What I
>>>>>envision is redirecting the output of 'bdf', appending into a single
>>>>>file, but I need to add the system date to each record. Something
>>>>>like this:
>>>>
>>>>>Of course, bdf gives this output:
>>>>
>>>>>/dev/vg00/lvol3 1032192 309920 716688 30% /
>>>>>/dev/vg00/lvol1 1032192 217168 808752 21% /stand
>>>>>/dev/vg00/lvol8 7667712 6132568 1524152 80% /var
>>>>>/dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
>>>>>/dev/vg00/lvol6 3080192 17656 3038936 1% /tmp
>>>>
>>>>>etc .....
>>>>
>>>>>What I'd like to get is
>>>>
>>>>>20080115 /dev/vg00/lvol3 1032192 309920 716688 30% /
>>>>>20080115 /dev/vg00/lvol1 1032192 217168 808752 21% /stand
>>>>>20080115 /dev/vg00/lvol8 7667712 6132568 1524152 80% /var
>>>>>20080115 /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
>>>>>20080115 /dev/vg00/lvol6 3080192 17656 3038936 1% /tmp
>>>>
>>>>>where that first field is the current day, shown here in yyyymmdd
>>>>>format. Of course the actual format of the date is not critical, and
>>>>>I can play with it once I find a way of getting it into the output...
>>>>
>>>>You can run something like this periodically (works with bash):
>>>
>>>>d=`date +%Y%m%d`
>>>>bdf | awk '{ print '$d' " " $0}' >> logfile
>>>
>>>>or, with sed, the second line becomes
>>>
>>>>bdf | sed -e 's/\(^.*$\)/'$d' \1/g' >> logfile
>>>
>>>>Of course, set the date format that you like best.
>>>
>>>Interesting solutions all ....
>>
>>>FWIW, the awk solution returned the following:
>>
>>>2.00801e+07 /dev/vg00/lvol3 1032192 310056 716552 30% /
>>>2.00801e+07 /dev/vg00/lvol1 1032192 217168 808752 21% /stand
>>>2.00801e+07 /dev/vg00/lvol8 7667712 6114568 1542016 80% /var
>>>2.00801e+07 /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
>>>2.00801e+07 /dev/vg00/lvol6 3080192 17680 3038912 1% /tmp
>>
>>>.... etc.
>>
>>Which awk solution? 3 have been discussed:
>>
>>1) d=`date +%Y%m%d`
>> bdf | awk '{ print '$d' " " $0}'
>>
>>2) d=`date +%Y%m%d`
>> bdf | awk -v d="$d" '{print d,$0}'
>>
>>3) bdf | gawk 'BEGIN{d=strftime("%Y%m%d)} {print d,$0}'
>>
>>Also, which version of awk are you using (awk --version)?
>>
>>The format awk uses to output numbers via print is controlled by the "OFMT"
>>variable, so you could also do this:
>>
>> awk 'BEGIN{print OFMT; exit}'
>>
>>to see what that's set to, or use printf instead of print to manually specify an
>>output format.
>>
>> Ed.
>
>
> Ah, so there were .... sorry ...
>
> Not to belabor the point, as I have my solution and learned much in
> the process, but here are the results of the three awk based
> solutions:
>
> ngtndp02:stevense> bdf | awk '{ print '$d' " " $0}'
> 2.00801e+07 Filesystem kbytes used avail %used Mounted
> on
> 2.00801e+07 /dev/vg00/lvol3 1032192 310056 716552 30% /
> 2.00801e+07 /dev/vg00/lvol1 1032192 217168 808752 21% /stand
> 2.00801e+07 /dev/vg00/lvol8 7667712 6144368 1512456 80% /var
> 2.00801e+07 /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
> 2.00801e+07 /dev/vg00/lvol6 3080192 17712 3038880 1% /tmp
>

Ah, because you're jumping back and forth between awk and shell, this awk sees
the value of $d as a literal number and so applies the OFMT conversion to format
it as %.6g when printing. If you set OFMT=%d or append a null string to the
value or do any of several other things, you should see the actual date, but
jumping back and forth between awk and shell is the wrong approach anyway. GNU
awk doesn't behave that way, even in POSIX mode, so while this doesn't SEEM
unreasonable off the top of my head, it may be a side-effect of the bug
mentioned in the HP-UP awk man page:

" BUGS
There are no explicit conversions between numbers and strings. To
force an expression to be treated as a number add 0 to it; to force it
to be treated as a string, concatenate "" "" to it. "

> ngtndp02:stevense> d=`date +%Y%m%d`
> ngtndp02:stevense> bdf | awk -v d="$d" '{print d,$0}'
> 20080116 Filesystem kbytes used avail %used Mounted on
> 20080116 /dev/vg00/lvol3 1032192 310056 716552 30% /
> 20080116 /dev/vg00/lvol1 1032192 217168 808752 21% /stand
> 20080116 /dev/vg00/lvol8 7667712 6144368 1512456 80% /var
> 20080116 /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
> 20080116 /dev/vg00/lvol6 3080192 17712 3038880 1% /tmp
>

In this case it's treating d as a variable that contains a string and so
behaving sensibly. One more reason to prefer this approach :-).

> ngtndp02:stevense> bdf | awk 'BEGIN{d=strftime("%Y%m%d)} {print d,$0}'
> syntax error The source line is 1.
> The error context is
> BEGIN{d=strftime("%Y%m%d)} {print >>> d,$0} <<<
> awk: The statement cannot be correctly parsed.
> The source line is 1.
> awk: There is a missing } character.
> awk: There is a missing ) character.

That statement was missing a quote:

bdf | gawk 'BEGIN{d=strftime("%Y%m%d")} {print d,$0}'

but as I mentioned it's a GNU awk only approach anyway as only GNU awk has the
time functions like strftime().

> ngtndp02:stevense> awk --version
> Usage: awk [-F fs][-v Assignment][-f Progfile|Program][Assignment|
> File] ...

Thanks!

Ed.

Re: looking for scripting solution

am 16.01.2008 15:49:15 von PK

EdStevens wrote:

> Not to belabor the point, as I have my solution and learned much in
> the process, but here are the results of the three awk based
> solutions:
>
> ngtndp02:stevense> bdf | awk '{ print '$d' " " $0}'
> 2.00801e+07 Filesystem kbytes used avail %used Mounted
> on
> 2.00801e+07 /dev/vg00/lvol3 1032192 310056 716552 30% /
> 2.00801e+07 /dev/vg00/lvol1 1032192 217168 808752 21% /stand
> 2.00801e+07 /dev/vg00/lvol8 7667712 6144368 1512456 80% /var
> 2.00801e+07 /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
> 2.00801e+07 /dev/vg00/lvol6 3080192 17712 3038880 1% /tmp
>
>
> ngtndp02:stevense> d=`date +%Y%m%d`
> ngtndp02:stevense> bdf | awk -v d="$d" '{print d,$0}'
> 20080116 Filesystem kbytes used avail %used Mounted on
> 20080116 /dev/vg00/lvol3 1032192 310056 716552 30% /
> 20080116 /dev/vg00/lvol1 1032192 217168 808752 21% /stand
> 20080116 /dev/vg00/lvol8 7667712 6144368 1512456 80% /var
> 20080116 /dev/vg00/lvol7 7176192 2711680 4429640 38% /usr
> 20080116 /dev/vg00/lvol6 3080192 17712 3038880 1% /tmp
>

A good reason to not use my solution and use Ed's instead :-)

> ngtndp02:stevense> bdf | awk 'BEGIN{d=strftime("%Y%m%d)} {print d,$0}'
> syntax error The source line is 1.
> The error context is
> BEGIN{d=strftime("%Y%m%d)} {print >>> d,$0} <<<
> awk: The statement cannot be correctly parsed.
> The source line is 1.
> awk: There is a missing } character.
> awk: There is a missing ) character.

Not sure, but this probably means you don't have GNU awk.

> ngtndp02:stevense> awk --version
> Usage: awk [-F fs][-v Assignment][-f Progfile|Program][Assignment|
> File] ...

Re: looking for scripting solution

am 16.01.2008 15:58:12 von PK

pk wrote:

>> ngtndp02:stevense> bdf | awk 'BEGIN{d=strftime("%Y%m%d)} {print d,$0}'
>> syntax error The source line is 1.
>> The error context is
>> BEGIN{d=strftime("%Y%m%d)} {print >>> d,$0} <<<
>> awk: The statement cannot be correctly parsed.
>> The source line is 1.
>> awk: There is a missing } character.
>> awk: There is a missing ) character.
>
> Not sure, but this probably means you don't have GNU awk.

No, that was probably a typo (missing closing double quote). Try this

bdf | awk 'BEGIN{d=strftime("%Y%m%d")} {print d,$0}'

(note the " after the %d). It might not work anyway, if you don't have GNU
awk.