0

I have below script

size=$1

big_files_list=$(find . -name \*.bytes -type f -printf "%b %h/%f\n" |

                awk -v size="$size" '

                                function basename(file) {

                                                sub(".bytes$", ".properties", file)

                                                return file

                                                }

                                {if ($1 > size) {

                                                print basename($2) }}')

for i in ${big_files_list}; do
                echo "\t"
                grep -E 'size|repo-name|creationTime|ob-name|sha' $i | tr "\n" ","

done

which prints below output

size=992198
repo-name=testupload
creationTime=1603278566981
ob-name=cargotracker-master.zip
sha1=ca8bb4e3ddb802a228e945a8fe32c308b2e3d555
size=992198
repo-name=testupload
creationTime=1603278566981
ob-name=cargotracker-master.zip
sha1=ca8bb4e3ddb802a228e945a8fe32c308b2e3d555
size=992198
repo-name=testupload
creationTime=1603278566981
ob-name=cargotracker-master.zip
sha1=ca8bb4e3ddb802a228e945a8fe32c308b2e3d555
size=992198
repo-name=testupload
creationTime=1603278566981
ob-name=cargotracker-master.zip
sha1=ca8bb4e3ddb802a228e945a8fe32c308b2e3d555

which grep and prints date from file where creationTime is in unix timestamp I know we can use date -d @1603278566981 to convert it to readable but records are in many numbers ,

any way we can convert that before final output. so final output should be like below

size=992198
repo-name=testupload
creationTime=Sun Nov 16 22:29:41 CET 52775
ob-name=cargotracker-master.zip
sha1=ca8bb4e3ddb802a228e945a8fe32c308b2e3d555
size=992198
repo-name=testupload
creationTime=Sun Nov 16 22:29:41 CET 52775
ob-name=cargotracker-master.zip
sha1=ca8bb4e3ddb802a228e945a8fe32c308b2e3d555
size=992198
repo-name=testupload
creationTime=Sun Nov 16 22:29:41 CET 52775
ob-name=cargotracker-master.zip
sha1=ca8bb4e3ddb802a228e945a8fe32c308b2e3d555
size=992198
repo-name=testupload
creationTime=Sun Nov 16 22:29:41 CET 52775
ob-name=cargotracker-master.zip
sha1=ca8bb4e3ddb802a228e945a8fe32c308b2e3d555

Please help,

Samurai


EDIT: here is the above code better formatted for readability:

size=$1

big_files_list=$(
    find . -name \*.bytes -type f -printf "%b %h/%f\n" |
    awk -v size="$size" '
        function basename(file) {
            sub(".bytes$", ".properties", file)
            return file
        }
        {
            if ($1 > size) {
                print basename($2)
            }
        }
    '
)

for i in ${big_files_list}; do
    echo "\t"
    grep -E 'size|repo-name|creationTime|ob-name|sha' $i | tr "\n" ","
done
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
Samurai
  • 121
  • 1
  • 4
  • 15
  • These records are from the year 52775 ?!? :) I think your `date` invocation misunderstands the units of those timestamps. – pilcrow Dec 02 '20 at 15:11
  • 1
    @Samurai your code formatting in terms of white space hurts rather than helps readability so I posted code that's better formatted at the bottom of your question so you can see the difference and maybe employ a similar style in your code in future. – Ed Morton Dec 02 '20 at 15:19
  • Why not process all the files found from find through awk printing the relevant data. You can also use awk's strftime function to process the unix timestamp as well. – Raman Sailopal Dec 02 '20 at 15:23
  • Your timestamps are evidently _milliseconds_ since the 1970-01-01 epoch, not seconds as is the Unix standard. The millisecond style is used by Java. Divide by 1000 -- which awk can easily do @RamanSailopal – dave_thompson_085 Dec 02 '20 at 15:24

1 Answers1

1

How about the following using GNU awk:

awk '/size|repo-name|creationTime|ob-name|sha/ {
                 print
     }
     /creationTime/ {
                 split($0,map,"-");
                 dat=(map[2]/10000000)-11644473600;
                 print "creationTime="strftime("%c",dat)
     } $(find <fulldirectorypath> -name "*.bytes" -size="+$size" -type f -printf "%h/%f\n" | awk '{ split($0,map,".");print map[1]".properties" }')

First, let find do the size processing with the -size parameter and pipe the output to awk to change the extention from bytes to properties. Then process these file with awk. For all entries apart from creationTime, simply print the lines. For creation time, process the unix timestamp and print in locale form.

This hasn't been tested so there maybe errors.

Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18
  • I tried below thing awk 'BEGIN {FS=OFS=" "}{$3=strftime("%c",$3)} {print}' outputfile and able to convert timestamp size repo-name Thu 01 Jan 1970 01:00:00 AM CET ob-name sha1 992198 testupload Sun 16 Nov 52775 10:29:41 PM CET cargotracker-master.zip ca8bb4e3ddb802a228e945a8fe32c308b2e3d555 992198 testupload Sun 16 Nov 52775 10:29:41 PM CET cargotracker-master.zip ca8bb4e3ddb802a228e945a8fe32c308b2e3d555 but I guess i need to trim last three digit to get exact time also how i can skip first line can you guide how I can do that – Samurai Dec 04 '20 at 09:44
  • $3=substr($3,length($3)-3) – Raman Sailopal Dec 04 '20 at 09:48
  • If the solution meets your needs, please take the time to accept it as an answer – Raman Sailopal Mar 11 '21 at 14:05