I am learning awk and I would like to know if there is an option to write changes to file, similar to sed where I would use -i option to save modifications to a file.

I do understand that I could use redirection to write changes. However is there an option in awk to do that?

share|improve this question
    
Also see serverfault.com/a/547331/313521 for the more general answer to "editing a file in place with redirection". – Wildcard Nov 4 '15 at 3:29
up vote 66 down vote accepted

In latest GNU Awk (since 4.1.0 released), it has the option of "inplace" file editing:

[...] The "inplace" extension, built using the new facility, can be used to simulate the GNU "sed -i" feature. [...]

Example usage:

$ gawk -i inplace '{ gsub(/foo/, "bar") }; { print }' file1 file2 file3

To keep the backup:

$ gawk -i inplace -v INPLACE_SUFFIX=.bak '{ gsub(/foo/, "bar") }
> { print }' file1 file2 file3
share|improve this answer
1  
@sudo_O - Thanks for the "inplace" demonstration. Upvoted your answer ! – lind May 14 '13 at 19:03
    
Looks like the option may have been removed? With 4.1.3, I have "-i includefile --include=includefile" – Keith Hughitt Jun 18 '16 at 11:46
    
@Keith I had the same question. I just tried it and it works on my 4.1.3. inplace is actually a library included with gawk according to iiSeymour's answer, so inplace is something that can be included as an includefile. – cxw Jun 23 '16 at 20:16

Unless you have GNU awk 4.1.0 or later...

You won't have such an option as sed's -i option so instead do:

$ awk '{print $0}' file > tmp && mv tmp file

Note: the -i is not magic, it is also creating a temporary file sed just handles it for you.


As of GNU awk 4.1.0...

GNU awk added this functionality in version 4.1.0 (released 10/05/2013). It is no as straight forwards as just giving the -i option as described in the released notes:

The new -i option (from xgawk) is used for loading awk library files. This differs from -f in that the first non-option argument is treated as a script.

You need to use the bundled inplace.awk include file to invoke the extension properly like so:

$ cat file
123 abc
456 def
789 hij

$ gawk -i inplace '{print $1}' file

$ cat file
123
456
789

The variable INPLACE_SUFFIX can be used to specify the extension for a backup file:

$ gawk -i inplace -v INPLACE_SUFFIX=.bak '{print $1}' file

$ cat file
123
456
789

$ cat file.bak
123 abc
456 def
789 hij

I am happy this feature has been added but to me the implementation isn't very awkish as the power is with the conciseness of the language and -i inplace is 8 characters to long I.M.O.

Here is a link to the manual for the official word.

share|improve this answer

@sudo_O has the right answer.

This can't work:

someprocess < file > file

The shell performs the redirections before handing control over to someprocess (redirections). The > redirection will truncate the file to zero size (redirecting output). Therefore, by the time someprocess gets launched and wants to read from the file, there is no data for it to read.

share|improve this answer

following won't work

echo $(awk '{awk code}' file) > file

this should work

echo "$(awk '{awk code}' file)" > file
share|improve this answer

just a little hack that works

echo "$(awk '{awk code}' file)" > file
share|improve this answer
4  
Won't work: newlines. – Lloeki Jan 14 '16 at 13:48
1  
@Lloeki thanks, I've fixed the answer :) – Yuri G. Sep 5 '16 at 6:56

In case you want an awk-only solution without creating a temporary file and usable with version!=(gawk 4.1.0):

awk '{a[b++]=$0} END {for(c=0;c<=b;c++)print a[c]>ARGV[1]}' file
share|improve this answer
    
But does this buffer the entire file to memory? Consider a 20GB file. – Amit Naidu Nov 29 '16 at 23:52

An alternative is to use sponge:

awk '{print $0}' your_file | sponge your_file

Where you replace '{print $0}' by your awk script and your_file by the name of the file you want to edit in place.

sponge absorbs entirely the input before saving it to the file.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.