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)
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 ofgawk
installed. You could possibly try uninstalling all versions ofgawk
and reinstalling the most recent one. – Kusalananda Dec 29 '22 at 06:07awk --version
, they are using the version from Homebrew. The nativeawk
would respond with something likeawk version 20200816
(it's not GNUawk
). – 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:36AWKPATH
may not be set in your shell in which case awk uses a default value so to see the value your awk is using doawk 'BEGIN{print ENVIRON["AWKPATH"]}'
. That should output something like.:/usr/local/share/awk
so then you can dols /usr/local/share/awk
(but use the last path output in the previous step) and there you should see several library files includinginplace.awk
(it may not have the.awk
extension). – Ed Morton Dec 29 '22 at 13:44/usr/share/awk
and try hard-coding the path asawk -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$ 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 thatbrew info gawk
should add such information? – user1424739 Dec 29 '22 at 16:50AWKPATH
wasn't set in your shell (so, for exampleecho "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 seeinplace.awk
in that directory you mentioned? idk whatbrew 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:50brew info gawk
should have provided some notes on setting up AWKPATH appropriately when needed. – user1424739 Dec 29 '22 at 17:54AWKPATH
in your shell might be empty and that could be fine as awk has an inbuilt default value, the way to print the value ofAWKPATH
that awk is using is exactly what I showed previously -awk 'BEGIN{print ENVIRON["AWKPATH"]}'
. What does that command output and do you see ainplace.awk
in that directory? You shouldn't need to setAWKPATH
, just don't write any code that overwrites it (or any other shell environment variable). – Ed Morton Dec 29 '22 at 19:05ENVIRON["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 inENVIRON["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:28PATH
value should somehow still be present after you doPATH=/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:44ls -l /usr/local/Cellar/gawk/5.2.1/share/awk
, do you seeinplace.awk
(with or without the.awk
extension) or not? If so, does it have read permission for you? If not, can you find it withfind /usr/local/Cellar/gawk -name '*inplace*'
? – Ed Morton Dec 30 '22 at 12:46