What does sed -i '1d' do?
I understand that sed
is a command to manipulate text file.
From my Googling, it seems -i
means perform the operation on the file itself, is this correct?
What about '1d'
?
sed
add a comment |
I understand that sed
is a command to manipulate text file.
From my Googling, it seems -i
means perform the operation on the file itself, is this correct?
What about '1d'
?
sed
add a comment |
I understand that sed
is a command to manipulate text file.
From my Googling, it seems -i
means perform the operation on the file itself, is this correct?
What about '1d'
?
sed
I understand that sed
is a command to manipulate text file.
From my Googling, it seems -i
means perform the operation on the file itself, is this correct?
What about '1d'
?
sed
sed
asked Jan 20 '16 at 10:13
Jérôme VerstryngeJérôme Verstrynge
56941019
56941019
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
In sed
:
-i
option will edit the input file in-place'1d'
will remove the first line of the input file
Example:
% cat file.txt
foo
bar
% sed -i '1d' file.txt
% cat file.txt
bar
Note that, most of the time it's a good idea to take a backup while using the -i
option so that you have the original file backed up in case of any unexpected change.
For example, if you do:
sed -i.orig '1d' file.txt
the original file will be kept as file.txt.orig
and the modified file will be file.txt
.
4
You can also do a "dry run" without the-i
to see what happens first, then use-i
to actually change the file.
– Baard Kopperud
Jan 20 '16 at 13:21
add a comment |
1. a)
sed '1d' file.txt
Prints the contents of file.txt
; excluding the first line; to the standard output.
2. a)
sed -i '1d' file.txt # GNU, NetBSD, OpenBSD
sed -i '' '1d' file.txt # FreeBSD, macOS
Prints the contents of file.txt
; excluding the first line; back into file.txt
; overwriting the original.
2. b)
sed -i.back '1d' file.txt
Creates a backup of the original (as file.txt.back
), before making changes. Except with FreeBSD sed
, the suffix (here .back
) must be attached to the -i
option (in the same argument, no space between -i
and .back
).
3. a)
sed '2d' file.txt
Prints the contents of file.txt
; excluding the second line; to the standard output.
(Specifying any number will remove the corresponding line).
Also compatible with the -i
flag.
3. b)
sed '1!d' file.txt
Prints the contents of file.txt
; excluding all but the first line; to the standard output.
(In other words; only the first line gets printed).
Also compatible with the -i
flag.
3. c)
sed '$d' file.txt
Prints the contents of file.txt
; excluding the last line; to the standard output.
Also compatible with the -i
flag.
FYI: The BSD version (i.e. macOS de facto standard) typically won't cooperate unless you create a backup (2. b), or use the backup bypass method (2. c). The GNU version won't prompt you for this. It will destructively edit, and overwrite existing files without hesitation.
– tjt263
Oct 19 '17 at 22:36
add a comment |
In sed -h
have:
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)
and 'perform the operation on the file itself.' absolute it'is.
And man
said: 'Sed is a stream editor. A stream editor is used to perform basic text
transformations on an input stream (a file or input from a pipeline).'
as your question,
sed -i '1d' file_name
means: delete the first line in file "file_name"
at place and backup to file.
(just like edit file and delete first line directly. )
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%2f256494%2fwhat-does-sed-i-1d-do%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
In sed
:
-i
option will edit the input file in-place'1d'
will remove the first line of the input file
Example:
% cat file.txt
foo
bar
% sed -i '1d' file.txt
% cat file.txt
bar
Note that, most of the time it's a good idea to take a backup while using the -i
option so that you have the original file backed up in case of any unexpected change.
For example, if you do:
sed -i.orig '1d' file.txt
the original file will be kept as file.txt.orig
and the modified file will be file.txt
.
4
You can also do a "dry run" without the-i
to see what happens first, then use-i
to actually change the file.
– Baard Kopperud
Jan 20 '16 at 13:21
add a comment |
In sed
:
-i
option will edit the input file in-place'1d'
will remove the first line of the input file
Example:
% cat file.txt
foo
bar
% sed -i '1d' file.txt
% cat file.txt
bar
Note that, most of the time it's a good idea to take a backup while using the -i
option so that you have the original file backed up in case of any unexpected change.
For example, if you do:
sed -i.orig '1d' file.txt
the original file will be kept as file.txt.orig
and the modified file will be file.txt
.
4
You can also do a "dry run" without the-i
to see what happens first, then use-i
to actually change the file.
– Baard Kopperud
Jan 20 '16 at 13:21
add a comment |
In sed
:
-i
option will edit the input file in-place'1d'
will remove the first line of the input file
Example:
% cat file.txt
foo
bar
% sed -i '1d' file.txt
% cat file.txt
bar
Note that, most of the time it's a good idea to take a backup while using the -i
option so that you have the original file backed up in case of any unexpected change.
For example, if you do:
sed -i.orig '1d' file.txt
the original file will be kept as file.txt.orig
and the modified file will be file.txt
.
In sed
:
-i
option will edit the input file in-place'1d'
will remove the first line of the input file
Example:
% cat file.txt
foo
bar
% sed -i '1d' file.txt
% cat file.txt
bar
Note that, most of the time it's a good idea to take a backup while using the -i
option so that you have the original file backed up in case of any unexpected change.
For example, if you do:
sed -i.orig '1d' file.txt
the original file will be kept as file.txt.orig
and the modified file will be file.txt
.
edited Feb 18 at 13:36
answered Jan 20 '16 at 10:16
heemaylheemayl
35.8k376106
35.8k376106
4
You can also do a "dry run" without the-i
to see what happens first, then use-i
to actually change the file.
– Baard Kopperud
Jan 20 '16 at 13:21
add a comment |
4
You can also do a "dry run" without the-i
to see what happens first, then use-i
to actually change the file.
– Baard Kopperud
Jan 20 '16 at 13:21
4
4
You can also do a "dry run" without the
-i
to see what happens first, then use -i
to actually change the file.– Baard Kopperud
Jan 20 '16 at 13:21
You can also do a "dry run" without the
-i
to see what happens first, then use -i
to actually change the file.– Baard Kopperud
Jan 20 '16 at 13:21
add a comment |
1. a)
sed '1d' file.txt
Prints the contents of file.txt
; excluding the first line; to the standard output.
2. a)
sed -i '1d' file.txt # GNU, NetBSD, OpenBSD
sed -i '' '1d' file.txt # FreeBSD, macOS
Prints the contents of file.txt
; excluding the first line; back into file.txt
; overwriting the original.
2. b)
sed -i.back '1d' file.txt
Creates a backup of the original (as file.txt.back
), before making changes. Except with FreeBSD sed
, the suffix (here .back
) must be attached to the -i
option (in the same argument, no space between -i
and .back
).
3. a)
sed '2d' file.txt
Prints the contents of file.txt
; excluding the second line; to the standard output.
(Specifying any number will remove the corresponding line).
Also compatible with the -i
flag.
3. b)
sed '1!d' file.txt
Prints the contents of file.txt
; excluding all but the first line; to the standard output.
(In other words; only the first line gets printed).
Also compatible with the -i
flag.
3. c)
sed '$d' file.txt
Prints the contents of file.txt
; excluding the last line; to the standard output.
Also compatible with the -i
flag.
FYI: The BSD version (i.e. macOS de facto standard) typically won't cooperate unless you create a backup (2. b), or use the backup bypass method (2. c). The GNU version won't prompt you for this. It will destructively edit, and overwrite existing files without hesitation.
– tjt263
Oct 19 '17 at 22:36
add a comment |
1. a)
sed '1d' file.txt
Prints the contents of file.txt
; excluding the first line; to the standard output.
2. a)
sed -i '1d' file.txt # GNU, NetBSD, OpenBSD
sed -i '' '1d' file.txt # FreeBSD, macOS
Prints the contents of file.txt
; excluding the first line; back into file.txt
; overwriting the original.
2. b)
sed -i.back '1d' file.txt
Creates a backup of the original (as file.txt.back
), before making changes. Except with FreeBSD sed
, the suffix (here .back
) must be attached to the -i
option (in the same argument, no space between -i
and .back
).
3. a)
sed '2d' file.txt
Prints the contents of file.txt
; excluding the second line; to the standard output.
(Specifying any number will remove the corresponding line).
Also compatible with the -i
flag.
3. b)
sed '1!d' file.txt
Prints the contents of file.txt
; excluding all but the first line; to the standard output.
(In other words; only the first line gets printed).
Also compatible with the -i
flag.
3. c)
sed '$d' file.txt
Prints the contents of file.txt
; excluding the last line; to the standard output.
Also compatible with the -i
flag.
FYI: The BSD version (i.e. macOS de facto standard) typically won't cooperate unless you create a backup (2. b), or use the backup bypass method (2. c). The GNU version won't prompt you for this. It will destructively edit, and overwrite existing files without hesitation.
– tjt263
Oct 19 '17 at 22:36
add a comment |
1. a)
sed '1d' file.txt
Prints the contents of file.txt
; excluding the first line; to the standard output.
2. a)
sed -i '1d' file.txt # GNU, NetBSD, OpenBSD
sed -i '' '1d' file.txt # FreeBSD, macOS
Prints the contents of file.txt
; excluding the first line; back into file.txt
; overwriting the original.
2. b)
sed -i.back '1d' file.txt
Creates a backup of the original (as file.txt.back
), before making changes. Except with FreeBSD sed
, the suffix (here .back
) must be attached to the -i
option (in the same argument, no space between -i
and .back
).
3. a)
sed '2d' file.txt
Prints the contents of file.txt
; excluding the second line; to the standard output.
(Specifying any number will remove the corresponding line).
Also compatible with the -i
flag.
3. b)
sed '1!d' file.txt
Prints the contents of file.txt
; excluding all but the first line; to the standard output.
(In other words; only the first line gets printed).
Also compatible with the -i
flag.
3. c)
sed '$d' file.txt
Prints the contents of file.txt
; excluding the last line; to the standard output.
Also compatible with the -i
flag.
1. a)
sed '1d' file.txt
Prints the contents of file.txt
; excluding the first line; to the standard output.
2. a)
sed -i '1d' file.txt # GNU, NetBSD, OpenBSD
sed -i '' '1d' file.txt # FreeBSD, macOS
Prints the contents of file.txt
; excluding the first line; back into file.txt
; overwriting the original.
2. b)
sed -i.back '1d' file.txt
Creates a backup of the original (as file.txt.back
), before making changes. Except with FreeBSD sed
, the suffix (here .back
) must be attached to the -i
option (in the same argument, no space between -i
and .back
).
3. a)
sed '2d' file.txt
Prints the contents of file.txt
; excluding the second line; to the standard output.
(Specifying any number will remove the corresponding line).
Also compatible with the -i
flag.
3. b)
sed '1!d' file.txt
Prints the contents of file.txt
; excluding all but the first line; to the standard output.
(In other words; only the first line gets printed).
Also compatible with the -i
flag.
3. c)
sed '$d' file.txt
Prints the contents of file.txt
; excluding the last line; to the standard output.
Also compatible with the -i
flag.
edited Jun 14 '18 at 12:46
answered Jan 20 '16 at 19:09
tjt263tjt263
5571520
5571520
FYI: The BSD version (i.e. macOS de facto standard) typically won't cooperate unless you create a backup (2. b), or use the backup bypass method (2. c). The GNU version won't prompt you for this. It will destructively edit, and overwrite existing files without hesitation.
– tjt263
Oct 19 '17 at 22:36
add a comment |
FYI: The BSD version (i.e. macOS de facto standard) typically won't cooperate unless you create a backup (2. b), or use the backup bypass method (2. c). The GNU version won't prompt you for this. It will destructively edit, and overwrite existing files without hesitation.
– tjt263
Oct 19 '17 at 22:36
FYI: The BSD version (i.e. macOS de facto standard) typically won't cooperate unless you create a backup (2. b), or use the backup bypass method (2. c). The GNU version won't prompt you for this. It will destructively edit, and overwrite existing files without hesitation.
– tjt263
Oct 19 '17 at 22:36
FYI: The BSD version (i.e. macOS de facto standard) typically won't cooperate unless you create a backup (2. b), or use the backup bypass method (2. c). The GNU version won't prompt you for this. It will destructively edit, and overwrite existing files without hesitation.
– tjt263
Oct 19 '17 at 22:36
add a comment |
In sed -h
have:
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)
and 'perform the operation on the file itself.' absolute it'is.
And man
said: 'Sed is a stream editor. A stream editor is used to perform basic text
transformations on an input stream (a file or input from a pipeline).'
as your question,
sed -i '1d' file_name
means: delete the first line in file "file_name"
at place and backup to file.
(just like edit file and delete first line directly. )
add a comment |
In sed -h
have:
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)
and 'perform the operation on the file itself.' absolute it'is.
And man
said: 'Sed is a stream editor. A stream editor is used to perform basic text
transformations on an input stream (a file or input from a pipeline).'
as your question,
sed -i '1d' file_name
means: delete the first line in file "file_name"
at place and backup to file.
(just like edit file and delete first line directly. )
add a comment |
In sed -h
have:
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)
and 'perform the operation on the file itself.' absolute it'is.
And man
said: 'Sed is a stream editor. A stream editor is used to perform basic text
transformations on an input stream (a file or input from a pipeline).'
as your question,
sed -i '1d' file_name
means: delete the first line in file "file_name"
at place and backup to file.
(just like edit file and delete first line directly. )
In sed -h
have:
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)
and 'perform the operation on the file itself.' absolute it'is.
And man
said: 'Sed is a stream editor. A stream editor is used to perform basic text
transformations on an input stream (a file or input from a pipeline).'
as your question,
sed -i '1d' file_name
means: delete the first line in file "file_name"
at place and backup to file.
(just like edit file and delete first line directly. )
answered Jan 20 '16 at 10:31
Se venSe ven
1349
1349
add a comment |
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%2f256494%2fwhat-does-sed-i-1d-do%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