awk-adding a column to a file

Hello Friends,

i used awk to sum up total size of files under a directory (with the help of examples, threads here).

ls -l | awk '/^-/ {total += $5} END {printf "%15.0f\n",total}' >> total.txt

After each execution of the script total result is appended into a text file:

7010
7794
8890 

i would like to add the execution date of the script into first column and then the results to the second column, but i couldnt do it after some tries. Do you know how to modify the script to add the date (date format is not important)?

expected:

Aug 27 7010
Sep 27 7794
Oct 27 8890

thanks by advance

not sure what you meant by 'execution date of the script', but...

ls -l | awk '/^-/ {a[$6 OFS $7]+=$5} END {for(i in a) printf "%s %15.0f\n",i,a}'

OR

ls -l | awk '/^-/ {a[$6 OFS sprintf("%2d",$7)]+=$5} END {for(i in a) printf "%s %15d\n",i,a}'
ls -l | awk '/^-/ {total +=$5} END {"date \"+%b %_d\""|getline d;printf "%s %15.0f\n",d,total}' >> total.txt

Hi Vgersh,

with "execution date of script" i meant the date when crontab run the script (monthly). Sorry for my bad explanation.. Let me try the scripts, thnx all

#!/bin/ksh
ls -l | awk '/^-/ {total+=$5} END {printf "%s %15d\n",d, total}' d=$(date '+%b %d')

Vgersh i tried it and AWK tries to open 27 of "Aug 27" and give error like below:

#!/bin/ksh -x
ls -l | awk '/^-/ {total+=$5} END {printf "%s %15d\n",d, total}' d=$(date '+%b %d')
server1{root}>./vgersh.sh
+ ls -l
+ date +%b %d
+ awk /^-/ {total+=$5} END {printf "%s %15d\n",d,total} d=Aug 27
awk: can't open 27

and besides i tried Danmero's script, it resulted with this (all script is 1 line so how can i trace it? ):

awk: syntax error near line 1
awk: illegal statement near line 1

sorry....

#!/bin/ksh
ls -l | awk '/^-/ {total+=$5} END {printf "%s %15d\n",d, total}' d="$(date '+%b %d')"

Thanks a lot it works well, the second script in your first post is great too. i will calculate the results with another awk that i wrote (i wish i could write such long lovely scripts like yours :slight_smile: )

#!/bin/ksh
ls -l | awk '/^-/ {total+=$5} END {printf "%s %15d\n",d, total}' d="$(date '+%b %d')" >> vv.txt

awk '{a+=$3}END{print a}' vv.txt

regards