Mahmoud Odeh
Mahmoud Odeh

Reputation: 950

How to change an awk script to work on Solaris

the below script is bin-packing First-fit algorithm,the script is running normally on ubuntu Linux and i can call bin_packing.awk, but when I try to run it on unix solaris I'm getting errors

bin_packing.awk:

function first_fit(v, file) {
    # find first bin that can accomodate the volume
    for (i=1; i<=n; ++i) {
        if (b[i] > v) {
            b[i] -= v
            bc[i]++
            cmd="mv "file" subdir_" i
            print cmd
            # system(cmd)
            return
        }
    }
    # no bin found, create new bin
    if (i > n) {
        b[++n] = c - v
        bc[n]++
        cmd="mkdir subdir_"n
        print cmd
        # system(cmd)
        cmd="mv "file" subdir_"n
        print cmd
        # system(cmd)
    }
    return
}
BEGIN{ if( (c+0) == 0) exit }
{ first_fit($1,$2) }
END { print "REPORT:"
    print "Created",n,"directories"
    for(i=1;i<=n;++i) print "- subdir_"i,":", c-b[i],"bytes",bc[i],"files"
}

and to call it:

$ find . -type f -iname '*pdf' -printf "%s %p\n" \
  | awk -v c=100000 -f bin_packing.awk

This will create a list of files with the file size in bytes in front of it., value c to be the maximum size a directory can have in bytes. The above value c=100000 is only an example, This will create output like:

...
mv file_47 subdir_6
mv file_48 subdir_6
mv file_49 subdir_5
mv file_50 subdir_6
REPORT:
Created 6 directories
- subdir_1 : 49 bytes 12 files
- subdir_2 : 49 bytes 9 files
- subdir_3 : 49 bytes 8 files
- subdir_4 : 49 bytes 8 files
- subdir_5 : 48 bytes 8 files
- subdir_6 : 37 bytes 5 files

it shows the below erros if i try to run it on Solaris, and based on feedback -printf is a GNU feature, so it isn't available in non-GNU versions of find

find: bad option -printf
find: [-H | -L] path-list predicate-list
awk: syntax error near line 1
awk: bailing out near line 1

Upvotes: 1

Views: 405

Answers (2)

PaulRM
PaulRM

Reputation: 409

to save the problem with the missing --printf find feature, you can try with:

find . -type f -iname '*pdf' -exec stat --printf="%s %n\n" {} \; \
| awk -v c=100000 -f bin_packing.awk

Upvotes: 0

Mahmoud Odeh
Mahmoud Odeh

Reputation: 950

using nawk (new awk) or /usr/xpg4/bin/awk (POSIX awk) with Solaris. awk is the original legacy version with Perl to glean the same info as find's -printf:

Here is the soluation:

$ find . -type f -name '*.pdf' -print | perl -lne '$,=" "; @s=stat $_; print $s[7],$_, $s[2]' | nawk -v c=5000000 -f bin_packing.awk

Upvotes: 3

Related Questions