strip all from line starting after whitespace
I have a file with this content:
lines
lines
rootdev=UUID=967d8dc3-f595-4a6e-929e-cc89as5a1a2s / ext4 defaults,
lines
I am searching for one line command which should strip everything in the line rootdev
after whitespace, so the final content should be like:
lines
lines
rootdev=UUID=967d8dc3-f595-4a6e-929e-cc89as5a1a2s
lines
Any idea how to do it with one line command? (so I do not need to open nano with this file and do it manually)
I tried with sed
but it just replaces the word rootdev
sed -i 's/rootdev//g' file.txt
linux sed sh
add a comment |
I have a file with this content:
lines
lines
rootdev=UUID=967d8dc3-f595-4a6e-929e-cc89as5a1a2s / ext4 defaults,
lines
I am searching for one line command which should strip everything in the line rootdev
after whitespace, so the final content should be like:
lines
lines
rootdev=UUID=967d8dc3-f595-4a6e-929e-cc89as5a1a2s
lines
Any idea how to do it with one line command? (so I do not need to open nano with this file and do it manually)
I tried with sed
but it just replaces the word rootdev
sed -i 's/rootdev//g' file.txt
linux sed sh
add a comment |
I have a file with this content:
lines
lines
rootdev=UUID=967d8dc3-f595-4a6e-929e-cc89as5a1a2s / ext4 defaults,
lines
I am searching for one line command which should strip everything in the line rootdev
after whitespace, so the final content should be like:
lines
lines
rootdev=UUID=967d8dc3-f595-4a6e-929e-cc89as5a1a2s
lines
Any idea how to do it with one line command? (so I do not need to open nano with this file and do it manually)
I tried with sed
but it just replaces the word rootdev
sed -i 's/rootdev//g' file.txt
linux sed sh
I have a file with this content:
lines
lines
rootdev=UUID=967d8dc3-f595-4a6e-929e-cc89as5a1a2s / ext4 defaults,
lines
I am searching for one line command which should strip everything in the line rootdev
after whitespace, so the final content should be like:
lines
lines
rootdev=UUID=967d8dc3-f595-4a6e-929e-cc89as5a1a2s
lines
Any idea how to do it with one line command? (so I do not need to open nano with this file and do it manually)
I tried with sed
but it just replaces the word rootdev
sed -i 's/rootdev//g' file.txt
linux sed sh
linux sed sh
edited Nov 21 '18 at 12:24
Inian
40.8k64172
40.8k64172
asked Nov 21 '18 at 11:59
peterpeter
1,44552348
1,44552348
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Your attempt with sed
can't work because it would just remove rootdev
string with an empty string. What you need is a tool that matches a pattern and does an action on that whole line for which I think awk
is more recommended than sed
.
Using awk
to match the line to strip off the spaces using gsub()
should be sufficient,
awk '/^rootdev/{ gsub(/[[:space:]].*/,"",$0) }1' file
If you are using GNU awk
with version greater than 4.1.0, you could use its in-place edit option to make the changes dynamically
gawk -i inplace '/^rootdev/{ gsub(/[[:space:]].*/,"",$0) }1' file
for earlier versions, use a temporary file
awk '/^rootdev/{ gsub(/[[:space:]].*/,"",$0) }1' file > temp && mv temp file
or use sponge
from moreutils
. If its not available in your system do get it on RHEL using yum install moreutils
or with apt-get
in Debian.
awk '/^rootdev/{ gsub(/[[:space:]].*/,"",$0) }1' file | sponge file
hi, in my case it does not work (file content stays the same).
– peter
Nov 22 '18 at 12:20
what I tried - to use "cut" program and strip everything from char 50. But now I have no idea how to save output of cut to file.
– peter
Nov 22 '18 at 12:33
I solved it with: sudo cut --complement -c50-999 file > file 2&& sudo mv file2 file
– peter
Nov 22 '18 at 12:41
add a comment |
First, search for a line beginning with rootdev=
, and when you find it delete everything after the first whitespace until the end of line.
sed -i '/^rootdev=/ s/ .*$//' file
Using a capturing group to keep rootdev=...
part in, and replacing the whole line with it is an option too.
sed -i 's/^(rootdev=[^ ]*).*$/1/' file
hi, in my case it does not work (file content stays the same).
– peter
Nov 22 '18 at 12:20
what I tried - to use "cut" program and strip everything from char 50. But now I have no idea how to save output of cut to file.
– peter
Nov 22 '18 at 12:33
I solved it with: sudo cut --complement -c50-999 file > file 2&& sudo mv file2 file
– peter
Nov 22 '18 at 12:41
Both mine and Inian's solutions work with your example. Please see stackoverflow.com/help/mcve and edit your question accordingly if you want us to help you.
– oguzismail
Nov 22 '18 at 14:02
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53411584%2fstrip-all-from-line-starting-after-whitespace%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
Your attempt with sed
can't work because it would just remove rootdev
string with an empty string. What you need is a tool that matches a pattern and does an action on that whole line for which I think awk
is more recommended than sed
.
Using awk
to match the line to strip off the spaces using gsub()
should be sufficient,
awk '/^rootdev/{ gsub(/[[:space:]].*/,"",$0) }1' file
If you are using GNU awk
with version greater than 4.1.0, you could use its in-place edit option to make the changes dynamically
gawk -i inplace '/^rootdev/{ gsub(/[[:space:]].*/,"",$0) }1' file
for earlier versions, use a temporary file
awk '/^rootdev/{ gsub(/[[:space:]].*/,"",$0) }1' file > temp && mv temp file
or use sponge
from moreutils
. If its not available in your system do get it on RHEL using yum install moreutils
or with apt-get
in Debian.
awk '/^rootdev/{ gsub(/[[:space:]].*/,"",$0) }1' file | sponge file
hi, in my case it does not work (file content stays the same).
– peter
Nov 22 '18 at 12:20
what I tried - to use "cut" program and strip everything from char 50. But now I have no idea how to save output of cut to file.
– peter
Nov 22 '18 at 12:33
I solved it with: sudo cut --complement -c50-999 file > file 2&& sudo mv file2 file
– peter
Nov 22 '18 at 12:41
add a comment |
Your attempt with sed
can't work because it would just remove rootdev
string with an empty string. What you need is a tool that matches a pattern and does an action on that whole line for which I think awk
is more recommended than sed
.
Using awk
to match the line to strip off the spaces using gsub()
should be sufficient,
awk '/^rootdev/{ gsub(/[[:space:]].*/,"",$0) }1' file
If you are using GNU awk
with version greater than 4.1.0, you could use its in-place edit option to make the changes dynamically
gawk -i inplace '/^rootdev/{ gsub(/[[:space:]].*/,"",$0) }1' file
for earlier versions, use a temporary file
awk '/^rootdev/{ gsub(/[[:space:]].*/,"",$0) }1' file > temp && mv temp file
or use sponge
from moreutils
. If its not available in your system do get it on RHEL using yum install moreutils
or with apt-get
in Debian.
awk '/^rootdev/{ gsub(/[[:space:]].*/,"",$0) }1' file | sponge file
hi, in my case it does not work (file content stays the same).
– peter
Nov 22 '18 at 12:20
what I tried - to use "cut" program and strip everything from char 50. But now I have no idea how to save output of cut to file.
– peter
Nov 22 '18 at 12:33
I solved it with: sudo cut --complement -c50-999 file > file 2&& sudo mv file2 file
– peter
Nov 22 '18 at 12:41
add a comment |
Your attempt with sed
can't work because it would just remove rootdev
string with an empty string. What you need is a tool that matches a pattern and does an action on that whole line for which I think awk
is more recommended than sed
.
Using awk
to match the line to strip off the spaces using gsub()
should be sufficient,
awk '/^rootdev/{ gsub(/[[:space:]].*/,"",$0) }1' file
If you are using GNU awk
with version greater than 4.1.0, you could use its in-place edit option to make the changes dynamically
gawk -i inplace '/^rootdev/{ gsub(/[[:space:]].*/,"",$0) }1' file
for earlier versions, use a temporary file
awk '/^rootdev/{ gsub(/[[:space:]].*/,"",$0) }1' file > temp && mv temp file
or use sponge
from moreutils
. If its not available in your system do get it on RHEL using yum install moreutils
or with apt-get
in Debian.
awk '/^rootdev/{ gsub(/[[:space:]].*/,"",$0) }1' file | sponge file
Your attempt with sed
can't work because it would just remove rootdev
string with an empty string. What you need is a tool that matches a pattern and does an action on that whole line for which I think awk
is more recommended than sed
.
Using awk
to match the line to strip off the spaces using gsub()
should be sufficient,
awk '/^rootdev/{ gsub(/[[:space:]].*/,"",$0) }1' file
If you are using GNU awk
with version greater than 4.1.0, you could use its in-place edit option to make the changes dynamically
gawk -i inplace '/^rootdev/{ gsub(/[[:space:]].*/,"",$0) }1' file
for earlier versions, use a temporary file
awk '/^rootdev/{ gsub(/[[:space:]].*/,"",$0) }1' file > temp && mv temp file
or use sponge
from moreutils
. If its not available in your system do get it on RHEL using yum install moreutils
or with apt-get
in Debian.
awk '/^rootdev/{ gsub(/[[:space:]].*/,"",$0) }1' file | sponge file
edited Nov 21 '18 at 13:45
answered Nov 21 '18 at 12:07
InianInian
40.8k64172
40.8k64172
hi, in my case it does not work (file content stays the same).
– peter
Nov 22 '18 at 12:20
what I tried - to use "cut" program and strip everything from char 50. But now I have no idea how to save output of cut to file.
– peter
Nov 22 '18 at 12:33
I solved it with: sudo cut --complement -c50-999 file > file 2&& sudo mv file2 file
– peter
Nov 22 '18 at 12:41
add a comment |
hi, in my case it does not work (file content stays the same).
– peter
Nov 22 '18 at 12:20
what I tried - to use "cut" program and strip everything from char 50. But now I have no idea how to save output of cut to file.
– peter
Nov 22 '18 at 12:33
I solved it with: sudo cut --complement -c50-999 file > file 2&& sudo mv file2 file
– peter
Nov 22 '18 at 12:41
hi, in my case it does not work (file content stays the same).
– peter
Nov 22 '18 at 12:20
hi, in my case it does not work (file content stays the same).
– peter
Nov 22 '18 at 12:20
what I tried - to use "cut" program and strip everything from char 50. But now I have no idea how to save output of cut to file.
– peter
Nov 22 '18 at 12:33
what I tried - to use "cut" program and strip everything from char 50. But now I have no idea how to save output of cut to file.
– peter
Nov 22 '18 at 12:33
I solved it with: sudo cut --complement -c50-999 file > file 2&& sudo mv file2 file
– peter
Nov 22 '18 at 12:41
I solved it with: sudo cut --complement -c50-999 file > file 2&& sudo mv file2 file
– peter
Nov 22 '18 at 12:41
add a comment |
First, search for a line beginning with rootdev=
, and when you find it delete everything after the first whitespace until the end of line.
sed -i '/^rootdev=/ s/ .*$//' file
Using a capturing group to keep rootdev=...
part in, and replacing the whole line with it is an option too.
sed -i 's/^(rootdev=[^ ]*).*$/1/' file
hi, in my case it does not work (file content stays the same).
– peter
Nov 22 '18 at 12:20
what I tried - to use "cut" program and strip everything from char 50. But now I have no idea how to save output of cut to file.
– peter
Nov 22 '18 at 12:33
I solved it with: sudo cut --complement -c50-999 file > file 2&& sudo mv file2 file
– peter
Nov 22 '18 at 12:41
Both mine and Inian's solutions work with your example. Please see stackoverflow.com/help/mcve and edit your question accordingly if you want us to help you.
– oguzismail
Nov 22 '18 at 14:02
add a comment |
First, search for a line beginning with rootdev=
, and when you find it delete everything after the first whitespace until the end of line.
sed -i '/^rootdev=/ s/ .*$//' file
Using a capturing group to keep rootdev=...
part in, and replacing the whole line with it is an option too.
sed -i 's/^(rootdev=[^ ]*).*$/1/' file
hi, in my case it does not work (file content stays the same).
– peter
Nov 22 '18 at 12:20
what I tried - to use "cut" program and strip everything from char 50. But now I have no idea how to save output of cut to file.
– peter
Nov 22 '18 at 12:33
I solved it with: sudo cut --complement -c50-999 file > file 2&& sudo mv file2 file
– peter
Nov 22 '18 at 12:41
Both mine and Inian's solutions work with your example. Please see stackoverflow.com/help/mcve and edit your question accordingly if you want us to help you.
– oguzismail
Nov 22 '18 at 14:02
add a comment |
First, search for a line beginning with rootdev=
, and when you find it delete everything after the first whitespace until the end of line.
sed -i '/^rootdev=/ s/ .*$//' file
Using a capturing group to keep rootdev=...
part in, and replacing the whole line with it is an option too.
sed -i 's/^(rootdev=[^ ]*).*$/1/' file
First, search for a line beginning with rootdev=
, and when you find it delete everything after the first whitespace until the end of line.
sed -i '/^rootdev=/ s/ .*$//' file
Using a capturing group to keep rootdev=...
part in, and replacing the whole line with it is an option too.
sed -i 's/^(rootdev=[^ ]*).*$/1/' file
edited Nov 21 '18 at 14:23
answered Nov 21 '18 at 12:24
oguzismailoguzismail
3,76031226
3,76031226
hi, in my case it does not work (file content stays the same).
– peter
Nov 22 '18 at 12:20
what I tried - to use "cut" program and strip everything from char 50. But now I have no idea how to save output of cut to file.
– peter
Nov 22 '18 at 12:33
I solved it with: sudo cut --complement -c50-999 file > file 2&& sudo mv file2 file
– peter
Nov 22 '18 at 12:41
Both mine and Inian's solutions work with your example. Please see stackoverflow.com/help/mcve and edit your question accordingly if you want us to help you.
– oguzismail
Nov 22 '18 at 14:02
add a comment |
hi, in my case it does not work (file content stays the same).
– peter
Nov 22 '18 at 12:20
what I tried - to use "cut" program and strip everything from char 50. But now I have no idea how to save output of cut to file.
– peter
Nov 22 '18 at 12:33
I solved it with: sudo cut --complement -c50-999 file > file 2&& sudo mv file2 file
– peter
Nov 22 '18 at 12:41
Both mine and Inian's solutions work with your example. Please see stackoverflow.com/help/mcve and edit your question accordingly if you want us to help you.
– oguzismail
Nov 22 '18 at 14:02
hi, in my case it does not work (file content stays the same).
– peter
Nov 22 '18 at 12:20
hi, in my case it does not work (file content stays the same).
– peter
Nov 22 '18 at 12:20
what I tried - to use "cut" program and strip everything from char 50. But now I have no idea how to save output of cut to file.
– peter
Nov 22 '18 at 12:33
what I tried - to use "cut" program and strip everything from char 50. But now I have no idea how to save output of cut to file.
– peter
Nov 22 '18 at 12:33
I solved it with: sudo cut --complement -c50-999 file > file 2&& sudo mv file2 file
– peter
Nov 22 '18 at 12:41
I solved it with: sudo cut --complement -c50-999 file > file 2&& sudo mv file2 file
– peter
Nov 22 '18 at 12:41
Both mine and Inian's solutions work with your example. Please see stackoverflow.com/help/mcve and edit your question accordingly if you want us to help you.
– oguzismail
Nov 22 '18 at 14:02
Both mine and Inian's solutions work with your example. Please see stackoverflow.com/help/mcve and edit your question accordingly if you want us to help you.
– oguzismail
Nov 22 '18 at 14:02
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53411584%2fstrip-all-from-line-starting-after-whitespace%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