How can I remove rest of file from string for all files?Remove all text before last space in text file from CLIHow to remove domain part of each line from text file containing email addressesFilter consecutive identical characters using Sed and GrepHow to find logs that contain certain string in lots of gz/regular files and save it as one/more txt files?command to remove specific string from multiple filesRemove all non-numeric characters from text filesDelete all lines from middle of a line matching a string until the second string match is foundHow sed can be used to replace multiple patterns within a string for different patternsBash, get values from json file, all in one line
Why don't all States switch to all postal voting?
Do I have to start a new career after installing Breaking Grounds expansion to Kerbal Space Program?
Why does China have so few nuclear weapons?
Possible to read "lines" from a variable?
Why is casting a DATE field to VARCHAR datatype non-deterministic and is there a way to make it deterministic?
Left and right brace around different number of rows without tikzmark
Is using Swiss Francs (CHF) cheaper than Euros (EUR) in Switzerland?
How much of a discount should I seek when prepaying a whole year's rent?
Will the same Javascript fetched by HTTP and HTTPS be cached separately by the browser?
What are the downsides of being a debt-free country (no foreign national debt)?
I keep rewriting the same section of my story. How do I move forward?
Monthly budget screen - need to take into account whether it's early/late in the month
Why do we have to make the Sign of the Cross physically?
finding IP return hex address
How do I take a photo so the moon doesn't have bright "rays"?
How to say No to idea given by team member, when I know from my experience that it is going to fail?
What is the reason for difference between integer division and float to int conversion in python?
Is it possible for a tiger's tail to be taken off and replaced with a living cobra, with both creatures still alive?
Why is there no FPU on (most) DSP chips?
Applying field calculator for multiple layers at once
What is considered room temperature in Celsius?
Are there languages with verb tenses, but no conjugation?
How does a religion based around destroying the world attract followers
What is a Aged Rope Phrase™?
How can I remove rest of file from string for all files?
Remove all text before last space in text file from CLIHow to remove domain part of each line from text file containing email addressesFilter consecutive identical characters using Sed and GrepHow to find logs that contain certain string in lots of gz/regular files and save it as one/more txt files?command to remove specific string from multiple filesRemove all non-numeric characters from text filesDelete all lines from middle of a line matching a string until the second string match is foundHow sed can be used to replace multiple patterns within a string for different patternsBash, get values from json file, all in one line
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
How can I remove rest of file from string for all files?
For example, input files 1
and 2
, string is ddd
.
Input file 1
.
aaa
bbb
ccc
ddfbbd dddaaa
eee
Output file 1
.
aaa
bbb
ccc
ddfbbd
Input file 2
.
ccc
aergddd
dasdvsdb
Output file 2
.
ccc
aerg
text-processing sed
add a comment
|
How can I remove rest of file from string for all files?
For example, input files 1
and 2
, string is ddd
.
Input file 1
.
aaa
bbb
ccc
ddfbbd dddaaa
eee
Output file 1
.
aaa
bbb
ccc
ddfbbd
Input file 2
.
ccc
aergddd
dasdvsdb
Output file 2
.
ccc
aerg
text-processing sed
add a comment
|
How can I remove rest of file from string for all files?
For example, input files 1
and 2
, string is ddd
.
Input file 1
.
aaa
bbb
ccc
ddfbbd dddaaa
eee
Output file 1
.
aaa
bbb
ccc
ddfbbd
Input file 2
.
ccc
aergddd
dasdvsdb
Output file 2
.
ccc
aerg
text-processing sed
How can I remove rest of file from string for all files?
For example, input files 1
and 2
, string is ddd
.
Input file 1
.
aaa
bbb
ccc
ddfbbd dddaaa
eee
Output file 1
.
aaa
bbb
ccc
ddfbbd
Input file 2
.
ccc
aergddd
dasdvsdb
Output file 2
.
ccc
aerg
text-processing sed
text-processing sed
edited Oct 1 at 15:06
Solomon Ucko
1051 silver badge5 bronze badges
1051 silver badge5 bronze badges
asked Sep 30 at 14:31
genderbeegenderbee
5429 bronze badges
5429 bronze badges
add a comment
|
add a comment
|
3 Answers
3
active
oldest
votes
With GNU sed:
str="ddd"
for file in 1 2; do
sed -i "/$str/ s/$str.*//; q" "$file"
done
This needs to be in a loop: otherwise the q
command would abort the whole process after processing only the first file.
1
Found it interesting you answered a similar question in Unix & LInux 8 years ago: unix.stackexchange.com/a/11323/200094
– WinEunuuchs2Unix
Oct 1 at 10:56
I wonder if you could justd
instead ofq
– D. Ben Knoble
Oct 1 at 18:31
You're welcome to try. sed is not a go-to tool for me once things get more complicated that what I did.
– glenn jackman
Oct 1 at 19:08
add a comment
|
With Perl:
perl -i -0777 -pe 's/ddd[sS]*//' file
or
perl -i -0777 -pe 's/ddd.*//s' file
-i
: modify the file in place.-0777
: force Perl to slurp the file as whole, not line by line.-pe
:-p
: loop Perl code.-e
: execute Perl code.
's/ddd[sS]*//'
: replace everything (every whitespace (s
) and non-whitespace (S
) character) afterddd
(including it) with an empty string.'s/ddd.*//s'
: replace everything (.*
) afterddd
(including it) with an empty string. Thes
flag at the end makes.*
also match newlines (thanks @glennjackman).
More about Perl flags can be found here.
Why not justs/ddd.*//
?
– dessert
Sep 30 at 16:41
@dessert.*
stops matching at newline.
– user3140225
Sep 30 at 16:53
You can use..*
if you add them
ors
flag to thes///
command (I forget which one)
– glenn jackman
Sep 30 at 21:23
@glennjackman It's thes
flag. Thanks!
– user3140225
Oct 1 at 7:33
add a comment
|
with GNU awk
, we can do:
awk 'function output() print >>FILENAME".out"
/ddd/ sub(/ddd.*/,""); output(); nextfile
output() ' file[12]
to change inplace, which it makes command even simple:
gawk -i inplace '/ddd/ sub(/ddd.*/,""); print ; nextfile 1' file[12]
add a comment
|
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
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/4.0/"u003ecc by-sa 4.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%2faskubuntu.com%2fquestions%2f1177693%2fhow-can-i-remove-rest-of-file-from-string-for-all-files%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
With GNU sed:
str="ddd"
for file in 1 2; do
sed -i "/$str/ s/$str.*//; q" "$file"
done
This needs to be in a loop: otherwise the q
command would abort the whole process after processing only the first file.
1
Found it interesting you answered a similar question in Unix & LInux 8 years ago: unix.stackexchange.com/a/11323/200094
– WinEunuuchs2Unix
Oct 1 at 10:56
I wonder if you could justd
instead ofq
– D. Ben Knoble
Oct 1 at 18:31
You're welcome to try. sed is not a go-to tool for me once things get more complicated that what I did.
– glenn jackman
Oct 1 at 19:08
add a comment
|
With GNU sed:
str="ddd"
for file in 1 2; do
sed -i "/$str/ s/$str.*//; q" "$file"
done
This needs to be in a loop: otherwise the q
command would abort the whole process after processing only the first file.
1
Found it interesting you answered a similar question in Unix & LInux 8 years ago: unix.stackexchange.com/a/11323/200094
– WinEunuuchs2Unix
Oct 1 at 10:56
I wonder if you could justd
instead ofq
– D. Ben Knoble
Oct 1 at 18:31
You're welcome to try. sed is not a go-to tool for me once things get more complicated that what I did.
– glenn jackman
Oct 1 at 19:08
add a comment
|
With GNU sed:
str="ddd"
for file in 1 2; do
sed -i "/$str/ s/$str.*//; q" "$file"
done
This needs to be in a loop: otherwise the q
command would abort the whole process after processing only the first file.
With GNU sed:
str="ddd"
for file in 1 2; do
sed -i "/$str/ s/$str.*//; q" "$file"
done
This needs to be in a loop: otherwise the q
command would abort the whole process after processing only the first file.
edited Sep 30 at 15:29
dessert
30.5k7 gold badges89 silver badges123 bronze badges
30.5k7 gold badges89 silver badges123 bronze badges
answered Sep 30 at 15:09
glenn jackmanglenn jackman
14k1 gold badge28 silver badges48 bronze badges
14k1 gold badge28 silver badges48 bronze badges
1
Found it interesting you answered a similar question in Unix & LInux 8 years ago: unix.stackexchange.com/a/11323/200094
– WinEunuuchs2Unix
Oct 1 at 10:56
I wonder if you could justd
instead ofq
– D. Ben Knoble
Oct 1 at 18:31
You're welcome to try. sed is not a go-to tool for me once things get more complicated that what I did.
– glenn jackman
Oct 1 at 19:08
add a comment
|
1
Found it interesting you answered a similar question in Unix & LInux 8 years ago: unix.stackexchange.com/a/11323/200094
– WinEunuuchs2Unix
Oct 1 at 10:56
I wonder if you could justd
instead ofq
– D. Ben Knoble
Oct 1 at 18:31
You're welcome to try. sed is not a go-to tool for me once things get more complicated that what I did.
– glenn jackman
Oct 1 at 19:08
1
1
Found it interesting you answered a similar question in Unix & LInux 8 years ago: unix.stackexchange.com/a/11323/200094
– WinEunuuchs2Unix
Oct 1 at 10:56
Found it interesting you answered a similar question in Unix & LInux 8 years ago: unix.stackexchange.com/a/11323/200094
– WinEunuuchs2Unix
Oct 1 at 10:56
I wonder if you could just
d
instead of q
– D. Ben Knoble
Oct 1 at 18:31
I wonder if you could just
d
instead of q
– D. Ben Knoble
Oct 1 at 18:31
You're welcome to try. sed is not a go-to tool for me once things get more complicated that what I did.
– glenn jackman
Oct 1 at 19:08
You're welcome to try. sed is not a go-to tool for me once things get more complicated that what I did.
– glenn jackman
Oct 1 at 19:08
add a comment
|
With Perl:
perl -i -0777 -pe 's/ddd[sS]*//' file
or
perl -i -0777 -pe 's/ddd.*//s' file
-i
: modify the file in place.-0777
: force Perl to slurp the file as whole, not line by line.-pe
:-p
: loop Perl code.-e
: execute Perl code.
's/ddd[sS]*//'
: replace everything (every whitespace (s
) and non-whitespace (S
) character) afterddd
(including it) with an empty string.'s/ddd.*//s'
: replace everything (.*
) afterddd
(including it) with an empty string. Thes
flag at the end makes.*
also match newlines (thanks @glennjackman).
More about Perl flags can be found here.
Why not justs/ddd.*//
?
– dessert
Sep 30 at 16:41
@dessert.*
stops matching at newline.
– user3140225
Sep 30 at 16:53
You can use..*
if you add them
ors
flag to thes///
command (I forget which one)
– glenn jackman
Sep 30 at 21:23
@glennjackman It's thes
flag. Thanks!
– user3140225
Oct 1 at 7:33
add a comment
|
With Perl:
perl -i -0777 -pe 's/ddd[sS]*//' file
or
perl -i -0777 -pe 's/ddd.*//s' file
-i
: modify the file in place.-0777
: force Perl to slurp the file as whole, not line by line.-pe
:-p
: loop Perl code.-e
: execute Perl code.
's/ddd[sS]*//'
: replace everything (every whitespace (s
) and non-whitespace (S
) character) afterddd
(including it) with an empty string.'s/ddd.*//s'
: replace everything (.*
) afterddd
(including it) with an empty string. Thes
flag at the end makes.*
also match newlines (thanks @glennjackman).
More about Perl flags can be found here.
Why not justs/ddd.*//
?
– dessert
Sep 30 at 16:41
@dessert.*
stops matching at newline.
– user3140225
Sep 30 at 16:53
You can use..*
if you add them
ors
flag to thes///
command (I forget which one)
– glenn jackman
Sep 30 at 21:23
@glennjackman It's thes
flag. Thanks!
– user3140225
Oct 1 at 7:33
add a comment
|
With Perl:
perl -i -0777 -pe 's/ddd[sS]*//' file
or
perl -i -0777 -pe 's/ddd.*//s' file
-i
: modify the file in place.-0777
: force Perl to slurp the file as whole, not line by line.-pe
:-p
: loop Perl code.-e
: execute Perl code.
's/ddd[sS]*//'
: replace everything (every whitespace (s
) and non-whitespace (S
) character) afterddd
(including it) with an empty string.'s/ddd.*//s'
: replace everything (.*
) afterddd
(including it) with an empty string. Thes
flag at the end makes.*
also match newlines (thanks @glennjackman).
More about Perl flags can be found here.
With Perl:
perl -i -0777 -pe 's/ddd[sS]*//' file
or
perl -i -0777 -pe 's/ddd.*//s' file
-i
: modify the file in place.-0777
: force Perl to slurp the file as whole, not line by line.-pe
:-p
: loop Perl code.-e
: execute Perl code.
's/ddd[sS]*//'
: replace everything (every whitespace (s
) and non-whitespace (S
) character) afterddd
(including it) with an empty string.'s/ddd.*//s'
: replace everything (.*
) afterddd
(including it) with an empty string. Thes
flag at the end makes.*
also match newlines (thanks @glennjackman).
More about Perl flags can be found here.
edited Oct 1 at 12:40
answered Sep 30 at 15:57
user3140225user3140225
2,7104 gold badges12 silver badges24 bronze badges
2,7104 gold badges12 silver badges24 bronze badges
Why not justs/ddd.*//
?
– dessert
Sep 30 at 16:41
@dessert.*
stops matching at newline.
– user3140225
Sep 30 at 16:53
You can use..*
if you add them
ors
flag to thes///
command (I forget which one)
– glenn jackman
Sep 30 at 21:23
@glennjackman It's thes
flag. Thanks!
– user3140225
Oct 1 at 7:33
add a comment
|
Why not justs/ddd.*//
?
– dessert
Sep 30 at 16:41
@dessert.*
stops matching at newline.
– user3140225
Sep 30 at 16:53
You can use..*
if you add them
ors
flag to thes///
command (I forget which one)
– glenn jackman
Sep 30 at 21:23
@glennjackman It's thes
flag. Thanks!
– user3140225
Oct 1 at 7:33
Why not just
s/ddd.*//
?– dessert
Sep 30 at 16:41
Why not just
s/ddd.*//
?– dessert
Sep 30 at 16:41
@dessert
.*
stops matching at newline.– user3140225
Sep 30 at 16:53
@dessert
.*
stops matching at newline.– user3140225
Sep 30 at 16:53
You can use.
.*
if you add the m
or s
flag to the s///
command (I forget which one)– glenn jackman
Sep 30 at 21:23
You can use.
.*
if you add the m
or s
flag to the s///
command (I forget which one)– glenn jackman
Sep 30 at 21:23
@glennjackman It's the
s
flag. Thanks!– user3140225
Oct 1 at 7:33
@glennjackman It's the
s
flag. Thanks!– user3140225
Oct 1 at 7:33
add a comment
|
with GNU awk
, we can do:
awk 'function output() print >>FILENAME".out"
/ddd/ sub(/ddd.*/,""); output(); nextfile
output() ' file[12]
to change inplace, which it makes command even simple:
gawk -i inplace '/ddd/ sub(/ddd.*/,""); print ; nextfile 1' file[12]
add a comment
|
with GNU awk
, we can do:
awk 'function output() print >>FILENAME".out"
/ddd/ sub(/ddd.*/,""); output(); nextfile
output() ' file[12]
to change inplace, which it makes command even simple:
gawk -i inplace '/ddd/ sub(/ddd.*/,""); print ; nextfile 1' file[12]
add a comment
|
with GNU awk
, we can do:
awk 'function output() print >>FILENAME".out"
/ddd/ sub(/ddd.*/,""); output(); nextfile
output() ' file[12]
to change inplace, which it makes command even simple:
gawk -i inplace '/ddd/ sub(/ddd.*/,""); print ; nextfile 1' file[12]
with GNU awk
, we can do:
awk 'function output() print >>FILENAME".out"
/ddd/ sub(/ddd.*/,""); output(); nextfile
output() ' file[12]
to change inplace, which it makes command even simple:
gawk -i inplace '/ddd/ sub(/ddd.*/,""); print ; nextfile 1' file[12]
edited Oct 1 at 10:05
answered Sep 30 at 15:39
αғsнιηαғsнιη
28.8k23 gold badges107 silver badges170 bronze badges
28.8k23 gold badges107 silver badges170 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Ask Ubuntu!
- 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%2faskubuntu.com%2fquestions%2f1177693%2fhow-can-i-remove-rest-of-file-from-string-for-all-files%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