How to permanently change a file using awk? (“in-place” edits, as with “sed -i”)
I have an awk file new.awk
BEGIN { FS=OFS="," }
NR==1 {
for (i=1; i<=NF; i++) {
f[$i] = i
}
}
NR > 1 {
begSecs= mktime(gensub(/[":-]/," ","g",$(f["DateTime"])))
endSecs = begSecs + $(f["TotalDuration"])
$(f["CallEndTime"]) = strftime("%Y-%m-%d %H:%M:%S", endSecs)
}
{ print }
I am calling this file in shell
awk new.awk sample.csv
But I can see the changes in the terminal, but how to make the change permanent to the file like using -i in sed.
linux shell awk
add a comment |
I have an awk file new.awk
BEGIN { FS=OFS="," }
NR==1 {
for (i=1; i<=NF; i++) {
f[$i] = i
}
}
NR > 1 {
begSecs= mktime(gensub(/[":-]/," ","g",$(f["DateTime"])))
endSecs = begSecs + $(f["TotalDuration"])
$(f["CallEndTime"]) = strftime("%Y-%m-%d %H:%M:%S", endSecs)
}
{ print }
I am calling this file in shell
awk new.awk sample.csv
But I can see the changes in the terminal, but how to make the change permanent to the file like using -i in sed.
linux shell awk
add a comment |
I have an awk file new.awk
BEGIN { FS=OFS="," }
NR==1 {
for (i=1; i<=NF; i++) {
f[$i] = i
}
}
NR > 1 {
begSecs= mktime(gensub(/[":-]/," ","g",$(f["DateTime"])))
endSecs = begSecs + $(f["TotalDuration"])
$(f["CallEndTime"]) = strftime("%Y-%m-%d %H:%M:%S", endSecs)
}
{ print }
I am calling this file in shell
awk new.awk sample.csv
But I can see the changes in the terminal, but how to make the change permanent to the file like using -i in sed.
linux shell awk
I have an awk file new.awk
BEGIN { FS=OFS="," }
NR==1 {
for (i=1; i<=NF; i++) {
f[$i] = i
}
}
NR > 1 {
begSecs= mktime(gensub(/[":-]/," ","g",$(f["DateTime"])))
endSecs = begSecs + $(f["TotalDuration"])
$(f["CallEndTime"]) = strftime("%Y-%m-%d %H:%M:%S", endSecs)
}
{ print }
I am calling this file in shell
awk new.awk sample.csv
But I can see the changes in the terminal, but how to make the change permanent to the file like using -i in sed.
linux shell awk
linux shell awk
edited Jan 23 at 11:18
msp9011
4,37044066
4,37044066
asked Jan 23 at 10:44
mittumittu
475
475
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
GNU awk
(commonly found on Linux systems and which you're likely using already as those mktime()
and strftime()
calls are GNU extensions, also implemented by mawk
, but it's less likely that this is your awk
), since version 4.1.0, is able to include an "awk
source library" with -i
or --include
on the command line. One of the source libraries that is distributed with GNU awk
is one called inplace
:
$ cat file
hello
there
$ awk -i inplace '/hello/ { print "oh,", $0 }' file
$ cat file
oh, hello
As you can see, this makes the output of the awk
code replace the input file. The line saying there
is not kept as it's not outputted by the program.
With an awk
script in a file, you would use it like
awk -i inplace -f script.awk datafile
If the awk
variable INPLACE_SUFFIX
is set to a string, then the library would make a backup of the original file with that as a filename suffix.
awk -i inplace -v INPLACE_SUFFIX=.bak -f script.awk datafile
If you have several input files, then each file with be individually in-place edited. But you can turn in-place editing off for a file (or a set of files) by using inplace=0
on the command line before that file:
awk -i inplace -f script.awk file1 file2 inplace=0 file3 inplace=1 file4
In the above command, file3
would not be edited in-place.
For a more portable "in-place edit" of a single file, use
tmpfile=$(mktemp)
cp file "$tmpfile" &&
awk '...some program here...' "$tmpfile" >file
rm "$tmpfile"
This would copy the input file to a temporary location, then apply the awk
code on the temporary file while redirecting to the original filename.
Doing it this way ensures that file meta-data (permissions and ownership) of the original file is not modified.
This works perfectly, Thanks a lot
– mittu
Jan 23 at 11:56
add a comment |
Try this.
awk new.awk sample.csv > tmp.csv && mv -f tmp.csv sample.csv
- redirect the output to a temp file.
- then move content of temp file to original file.
Thanks, It works
– mittu
Jan 23 at 11:07
1
If you look at the awk inplace source code this is exactly what it does: creates a temporary file, redirects stdout there and at the end renames it to the input file.
– chx
Jan 23 at 12:18
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496179%2fhow-to-permanently-change-a-file-using-awk-in-place-edits-as-with-sed-i%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
GNU awk
(commonly found on Linux systems and which you're likely using already as those mktime()
and strftime()
calls are GNU extensions, also implemented by mawk
, but it's less likely that this is your awk
), since version 4.1.0, is able to include an "awk
source library" with -i
or --include
on the command line. One of the source libraries that is distributed with GNU awk
is one called inplace
:
$ cat file
hello
there
$ awk -i inplace '/hello/ { print "oh,", $0 }' file
$ cat file
oh, hello
As you can see, this makes the output of the awk
code replace the input file. The line saying there
is not kept as it's not outputted by the program.
With an awk
script in a file, you would use it like
awk -i inplace -f script.awk datafile
If the awk
variable INPLACE_SUFFIX
is set to a string, then the library would make a backup of the original file with that as a filename suffix.
awk -i inplace -v INPLACE_SUFFIX=.bak -f script.awk datafile
If you have several input files, then each file with be individually in-place edited. But you can turn in-place editing off for a file (or a set of files) by using inplace=0
on the command line before that file:
awk -i inplace -f script.awk file1 file2 inplace=0 file3 inplace=1 file4
In the above command, file3
would not be edited in-place.
For a more portable "in-place edit" of a single file, use
tmpfile=$(mktemp)
cp file "$tmpfile" &&
awk '...some program here...' "$tmpfile" >file
rm "$tmpfile"
This would copy the input file to a temporary location, then apply the awk
code on the temporary file while redirecting to the original filename.
Doing it this way ensures that file meta-data (permissions and ownership) of the original file is not modified.
This works perfectly, Thanks a lot
– mittu
Jan 23 at 11:56
add a comment |
GNU awk
(commonly found on Linux systems and which you're likely using already as those mktime()
and strftime()
calls are GNU extensions, also implemented by mawk
, but it's less likely that this is your awk
), since version 4.1.0, is able to include an "awk
source library" with -i
or --include
on the command line. One of the source libraries that is distributed with GNU awk
is one called inplace
:
$ cat file
hello
there
$ awk -i inplace '/hello/ { print "oh,", $0 }' file
$ cat file
oh, hello
As you can see, this makes the output of the awk
code replace the input file. The line saying there
is not kept as it's not outputted by the program.
With an awk
script in a file, you would use it like
awk -i inplace -f script.awk datafile
If the awk
variable INPLACE_SUFFIX
is set to a string, then the library would make a backup of the original file with that as a filename suffix.
awk -i inplace -v INPLACE_SUFFIX=.bak -f script.awk datafile
If you have several input files, then each file with be individually in-place edited. But you can turn in-place editing off for a file (or a set of files) by using inplace=0
on the command line before that file:
awk -i inplace -f script.awk file1 file2 inplace=0 file3 inplace=1 file4
In the above command, file3
would not be edited in-place.
For a more portable "in-place edit" of a single file, use
tmpfile=$(mktemp)
cp file "$tmpfile" &&
awk '...some program here...' "$tmpfile" >file
rm "$tmpfile"
This would copy the input file to a temporary location, then apply the awk
code on the temporary file while redirecting to the original filename.
Doing it this way ensures that file meta-data (permissions and ownership) of the original file is not modified.
This works perfectly, Thanks a lot
– mittu
Jan 23 at 11:56
add a comment |
GNU awk
(commonly found on Linux systems and which you're likely using already as those mktime()
and strftime()
calls are GNU extensions, also implemented by mawk
, but it's less likely that this is your awk
), since version 4.1.0, is able to include an "awk
source library" with -i
or --include
on the command line. One of the source libraries that is distributed with GNU awk
is one called inplace
:
$ cat file
hello
there
$ awk -i inplace '/hello/ { print "oh,", $0 }' file
$ cat file
oh, hello
As you can see, this makes the output of the awk
code replace the input file. The line saying there
is not kept as it's not outputted by the program.
With an awk
script in a file, you would use it like
awk -i inplace -f script.awk datafile
If the awk
variable INPLACE_SUFFIX
is set to a string, then the library would make a backup of the original file with that as a filename suffix.
awk -i inplace -v INPLACE_SUFFIX=.bak -f script.awk datafile
If you have several input files, then each file with be individually in-place edited. But you can turn in-place editing off for a file (or a set of files) by using inplace=0
on the command line before that file:
awk -i inplace -f script.awk file1 file2 inplace=0 file3 inplace=1 file4
In the above command, file3
would not be edited in-place.
For a more portable "in-place edit" of a single file, use
tmpfile=$(mktemp)
cp file "$tmpfile" &&
awk '...some program here...' "$tmpfile" >file
rm "$tmpfile"
This would copy the input file to a temporary location, then apply the awk
code on the temporary file while redirecting to the original filename.
Doing it this way ensures that file meta-data (permissions and ownership) of the original file is not modified.
GNU awk
(commonly found on Linux systems and which you're likely using already as those mktime()
and strftime()
calls are GNU extensions, also implemented by mawk
, but it's less likely that this is your awk
), since version 4.1.0, is able to include an "awk
source library" with -i
or --include
on the command line. One of the source libraries that is distributed with GNU awk
is one called inplace
:
$ cat file
hello
there
$ awk -i inplace '/hello/ { print "oh,", $0 }' file
$ cat file
oh, hello
As you can see, this makes the output of the awk
code replace the input file. The line saying there
is not kept as it's not outputted by the program.
With an awk
script in a file, you would use it like
awk -i inplace -f script.awk datafile
If the awk
variable INPLACE_SUFFIX
is set to a string, then the library would make a backup of the original file with that as a filename suffix.
awk -i inplace -v INPLACE_SUFFIX=.bak -f script.awk datafile
If you have several input files, then each file with be individually in-place edited. But you can turn in-place editing off for a file (or a set of files) by using inplace=0
on the command line before that file:
awk -i inplace -f script.awk file1 file2 inplace=0 file3 inplace=1 file4
In the above command, file3
would not be edited in-place.
For a more portable "in-place edit" of a single file, use
tmpfile=$(mktemp)
cp file "$tmpfile" &&
awk '...some program here...' "$tmpfile" >file
rm "$tmpfile"
This would copy the input file to a temporary location, then apply the awk
code on the temporary file while redirecting to the original filename.
Doing it this way ensures that file meta-data (permissions and ownership) of the original file is not modified.
edited Jan 23 at 13:06
answered Jan 23 at 11:00
KusalanandaKusalananda
133k17254417
133k17254417
This works perfectly, Thanks a lot
– mittu
Jan 23 at 11:56
add a comment |
This works perfectly, Thanks a lot
– mittu
Jan 23 at 11:56
This works perfectly, Thanks a lot
– mittu
Jan 23 at 11:56
This works perfectly, Thanks a lot
– mittu
Jan 23 at 11:56
add a comment |
Try this.
awk new.awk sample.csv > tmp.csv && mv -f tmp.csv sample.csv
- redirect the output to a temp file.
- then move content of temp file to original file.
Thanks, It works
– mittu
Jan 23 at 11:07
1
If you look at the awk inplace source code this is exactly what it does: creates a temporary file, redirects stdout there and at the end renames it to the input file.
– chx
Jan 23 at 12:18
add a comment |
Try this.
awk new.awk sample.csv > tmp.csv && mv -f tmp.csv sample.csv
- redirect the output to a temp file.
- then move content of temp file to original file.
Thanks, It works
– mittu
Jan 23 at 11:07
1
If you look at the awk inplace source code this is exactly what it does: creates a temporary file, redirects stdout there and at the end renames it to the input file.
– chx
Jan 23 at 12:18
add a comment |
Try this.
awk new.awk sample.csv > tmp.csv && mv -f tmp.csv sample.csv
- redirect the output to a temp file.
- then move content of temp file to original file.
Try this.
awk new.awk sample.csv > tmp.csv && mv -f tmp.csv sample.csv
- redirect the output to a temp file.
- then move content of temp file to original file.
answered Jan 23 at 10:46
msp9011msp9011
4,37044066
4,37044066
Thanks, It works
– mittu
Jan 23 at 11:07
1
If you look at the awk inplace source code this is exactly what it does: creates a temporary file, redirects stdout there and at the end renames it to the input file.
– chx
Jan 23 at 12:18
add a comment |
Thanks, It works
– mittu
Jan 23 at 11:07
1
If you look at the awk inplace source code this is exactly what it does: creates a temporary file, redirects stdout there and at the end renames it to the input file.
– chx
Jan 23 at 12:18
Thanks, It works
– mittu
Jan 23 at 11:07
Thanks, It works
– mittu
Jan 23 at 11:07
1
1
If you look at the awk inplace source code this is exactly what it does: creates a temporary file, redirects stdout there and at the end renames it to the input file.
– chx
Jan 23 at 12:18
If you look at the awk inplace source code this is exactly what it does: creates a temporary file, redirects stdout there and at the end renames it to the input file.
– chx
Jan 23 at 12:18
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496179%2fhow-to-permanently-change-a-file-using-awk-in-place-edits-as-with-sed-i%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown