I want to replace all include('./
in a set of files with include('
. I am trying to use awk as follows:
awk '{gsub("include\('"'"'./", "include\('"'"'", $0); print > FILENAME}' *.php
It throws me this error.
awk: (FILENAME=xyz.php FNR=1) fatal: Unmatched ( or \(: /include('.//
Any help would be appreciated.
This works (without the I/O redirection on the 'print'):
It maps this input:
to:
Empirically, it seems that the regular expression must be inside slashes; the replacement string must be a regular string. You will need to map the '
.
' to '\.
' to stop the second replacement.I'm not very happy with this explanation. The man page for 'awk' on MacOS X says:
So, in theory, the string form you used should work. Empirically, it didn't; I got substantially the same error message as you did with your code. And you had got the shell quotes correct, which is non-trivial.
There are times when Perl might be easier (because you can choose an arbitrary delimiter to mark the regex boundaries):
You don't need to use
awk
if all you want to do is this. :) Also, writing to a file as you're reading from it, in the way that you did, will lead to data loss or corruption, try not to do it.Just to clarify the data loss aspect: when
awk
(orsed
) start processing a file and you ask them to read the first line, they will actually perform a buffered read, that is, they will read from the filesystem (let's simplify and say "from disk") a block of data as large as their internal read buffer (e.g. 4-65KB) in order to get better performance (by reducing disk I/O.) Assume that the file you're working with is larger than the buffer size. Further reads will continue to come from the buffer until the buffer is exhausted, at which point a second block of data will be loaded from disk into the buffer etc.However, just after you read the first line, i.e. after the first block of data is read from disk into the buffer, your
awk
script opensFILENAME
, the input file itself, for writing with truncation, i.e. the file's size on disk is reset to 0. At this point all that remains of your original file are the first few kilobytes of data inawk
's memory.Awk
will merrily continue to read line after line from the in-memory buffer and produce output until the buffer is exhausted, at which pointawk
will probably stop and leave you with a 4-65k file.As a side note, if you are actually using awk to expand (e.g.
print "PREFIX: " $0
), not shrink (gsub(/.../, "")
), data, then you'll almost certainly end up with a non-responsiveawk
and a perpetually growing file. :)Try this :
you misplaced backslash
or this :
how about this ?
Did you try without esacping anything
@OP, you can try using octal code for the single quote(
\047
) and forward slash(\057
), eg