webstackdev
webstackdev

Reputation: 939

GAWK Print string to STDOUT with -i inplace option

I would like to run a GNU AWK script that is editing files in-place (e.g. with the -i inplace option) and have it print the filenames it is working on to STDOUT. Is there a way to do this? The following just adds the filename as the first line in the modified file, rather than printing the filename on the command line:

BEGINFILE {
  print FILENAME
}

Upvotes: 1

Views: 319

Answers (1)

oguz ismail
oguz ismail

Reputation: 50795

Here is a workaround; drop -i inplace from the command line (not an obligatory though, see -e/-f) and place following at the very beginning of your script. Before starting to process a file's content, this will disable inplace temporarily and print FILENAME. Then inplace's BEGINFILE rule will enable itself again.

BEGINFILE {
    if (inplace::filename != "") {
        inplace::end(inplace::filename, inplace::suffix)
        inplace::filename = ""
    }
    print FILENAME
}

@include "inplace"

See how inplace is implemented for a better understanding.

Upvotes: 3

Related Questions