1

How to change a file in-place using awk? (as with "sed -i")

When I follow the solution above, I get the following error on mac. How to find why it does not work on mac?

awk -i inplace '/hello/ { print "oh,", $0 }' file
awk: fatal: cannot open source file `inplace' for reading: No such file or directory
$ awk --version
GNU Awk 5.2.1, API 3.2, PMA Avon 8-g1, (GNU MPFR 4.1.0-p13, GNU MP 6.2.1)
Copyright (C) 1989, 1991-2022 Free Software Foundation.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

$ brew info gawk ==> gawk: stable 5.2.1 (bottled), HEAD GNU awk utility https://www.gnu.org/software/gawk/ Conflicts with: awk (because both install an awk executable) /usr/local/Cellar/gawk/5.2.0 (98 files, 5.7MB) Poured from bottle on 2022-10-30 at 23:05:46 /usr/local/Cellar/gawk/5.2.1 (98 files, 5.8MB) * Poured from bottle on 2022-12-28 at 23:01:26 From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/gawk.rb License: GPL-3.0-or-later ==> Dependencies Required: gettext ✔, mpfr ✔, readline ✔ ==> Options --HEAD Install HEAD version ==> Analytics install: 24,742 (30 days), 82,190 (90 days), 178,408 (365 days) install-on-request: 20,593 (30 days), 67,741 (90 days), 135,672 (365 days) build-error: 7 (30 days)

  • I can not reproduce this with gawk 5.2.1 installed from Homebrew on macOS Ventura 13.1 (-i inplace works as expected). You have some other issue. You appear to have two versions of gawk installed. You could possibly try uninstalling all versions of gawk and reinstalling the most recent one. – Kusalananda Dec 29 '22 at 06:07
  • 1
    are you using the actual binary from brew? Use it with absolute path and see if the error goes away – Alex Dec 29 '22 at 07:11
  • @Alex According to the output from awk --version, they are using the version from Homebrew. The native awk would respond with something like awk version 20200816 (it's not GNU awk). – Kusalananda Dec 29 '22 at 08:47
  • -i for gawk is an instruction to include a library, the "inplace" library. If your gawk installation doesn't have that library for some reason, or doesn't know how to find libraries, then I could see you getting that error message. First check that your AWKPATH environment variable includes the directory where your libraries live and if you don't see anything obvious there then try reinstalling gawk. – Ed Morton Dec 29 '22 at 13:36
  • AWKPATH may not be set in your shell in which case awk uses a default value so to see the value your awk is using do awk 'BEGIN{print ENVIRON["AWKPATH"]}'. That should output something like .:/usr/local/share/awk so then you can do ls /usr/local/share/awk (but use the last path output in the previous step) and there you should see several library files including inplace.awk (it may not have the .awk extension). – Ed Morton Dec 29 '22 at 13:44
  • To investigate further, also look in /usr/share/awk and try hard-coding the path as awk -i /usr/local/share/awk/inplace.awk ... (wherever you find that file), but at the end of the day, unless you discover you're manually overwriting AWKPATH somewhere, I think you'll probably end up having to reinstall gawk. – Ed Morton Dec 29 '22 at 13:49
  • @EdMorton AWKPATH was not set. $ ls -gGl /usr/local/share/awk lrwxr-xr-x 1 30 2022-12-28 23:01:26 /usr/local/share/awk -> ../Cellar/gawk/5.2.1/share/awk So it seems that brew info gawk should add such information? – user1424739 Dec 29 '22 at 16:50
  • Are you saying that AWKPATH wasn't set in your shell (so, for example echo "AWKPATH" just printed a blank line) or that when you did what I instructed, awk 'BEGIN{print ENVIRON["AWKPATH"]}' that output just a blank line? The former wouldn't necessarily be a problem, the latter would be. Do you see inplace.awk in that directory you mentioned? idk what brew info gawk does but it doesn't sound to me like a command you'd use to install gawk, maybe just print some information about your installation? – Ed Morton Dec 29 '22 at 17:50
  • I am saying the former. echo "AWKPATH" is empty. brew info gawk should have provided some notes on setting up AWKPATH appropriately when needed. – user1424739 Dec 29 '22 at 17:54
  • 1
    As I said previously, AWKPATH in your shell might be empty and that could be fine as awk has an inbuilt default value, the way to print the value of AWKPATH that awk is using is exactly what I showed previously - awk 'BEGIN{print ENVIRON["AWKPATH"]}'. What does that command output and do you see a inplace.awk in that directory? You shouldn't need to set AWKPATH, just don't write any code that overwrites it (or any other shell environment variable). – Ed Morton Dec 29 '22 at 19:05
  • I see. The default ENVIRON["AWKPATH"] is .:/usr/local/Cellar/gawk/5.2.1/share/awk when the external env var AWKPATH is empty. However, when I set the external var AWKPATH to something, then /usr/local/Cellar/gawk/5.2.1/share/awk will be deleted in ENVIRON["AWKPATH"]. Shouldn't /usr/local/Cellar/gawk/5.2.1/share/awk be preserved no matter what external AWKPATH is? – user1424739 Dec 30 '22 at 02:28
  • @user1424739 no, that's like thinking the default PATH value should somehow still be present after you do PATH=/foo/bar. AWKPATH is just a plain old variable and if you set it to some value then that's what it is, end of story. Btw if you don't tag me in your comment with @EdMorton then I won't know you left one for me so YMMV with whether I ever see it or not. – Ed Morton Dec 30 '22 at 12:44
  • So, when you do ls -l /usr/local/Cellar/gawk/5.2.1/share/awk, do you see inplace.awk (with or without the .awk extension) or not? If so, does it have read permission for you? If not, can you find it with find /usr/local/Cellar/gawk -name '*inplace*'? – Ed Morton Dec 30 '22 at 12:46

0 Answers0