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
Rat proofing compost bin but allowing worms in
How to read a file line by line in Julia?
How to make a gift without seeming creepy?
universal string conversion
Had there been instances of national states banning harmful imports before the Opium wars?
What does a "le" mean between subject and verb?
How come the Russian cognate for the Czech word "čerstvý" (fresh) means entirely the opposite thing (stale)?
How stable are PID loops really?
Would we have more than 8 minutes of light, if the sun "went out"?
Does permanent loss of castling rights reset three fold repetition
Why can I ping 10.0.0.0/8 addresses from a 192.168.1.0/24 subnet?
How much money should I save in order to generate $1000/month for the rest of my life?
Over powered shield?
In 1700s, why was 'books that never read' grammatical?
one-liner vs script
Why would they pick a gamma distribution here?
Why did a young George Washington sign a document admitting to assassinating a French military officer?
Is having your hand in your pocket during a presentation bad?
Are great composers almost always prodigious performers?
Can something have more sugar per 100g than the percentage of sugar that's in it?
How are steel imports supposed to threaten US national security?
What is /dev/null and why can't I use hx on it?
Why do English transliterations of Arabic names have so many Qs in them?
Is insurance company’s preferred auto shop biased?
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
Input file 2.
ccc
aergddd
dasdvsdb
Output file 1.
aaa
bbb
ccc
ddfbbd
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
Input file 2.
ccc
aergddd
dasdvsdb
Output file 1.
aaa
bbb
ccc
ddfbbd
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
Input file 2.
ccc
aergddd
dasdvsdb
Output file 1.
aaa
bbb
ccc
ddfbbd
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
Input file 2.
ccc
aergddd
dasdvsdb
Output file 1.
aaa
bbb
ccc
ddfbbd
Output file 2.
ccc
aerg
text-processing sed
text-processing sed
edited 7 hours ago
αғsнιη
25.9k23 gold badges105 silver badges167 bronze badges
25.9k23 gold badges105 silver badges167 bronze badges
asked 8 hours ago
genderbeegenderbee
1407 bronze badges
1407 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.
add a comment
|
with GNU awk, we can do:
awk 'function output() print >>FILENAME".out"
/ddd/ sub(/ddd.*/,""); output(); nextfile
output() ' file[12]
add a comment
|
With Perl:
perl -i -0777 -pe 's/ddd[sS]*//' 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.
More about Perl flags can be found here.
Why not justs/ddd.*//?
– dessert
6 hours ago
@dessert.*stops matching at newline.
– user3140225
5 hours ago
Ah, so a true equivalent would be[Nn]*– nice, thanks!
– dessert
4 hours ago
You can use..*if you add themorsflag to thes///command (I forget which one)
– glenn jackman
1 hour ago
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.
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.
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 7 hours ago
dessert
29.2k7 gold badges87 silver badges121 bronze badges
29.2k7 gold badges87 silver badges121 bronze badges
answered 7 hours ago
glenn jackmanglenn jackman
13.3k26 silver badges46 bronze badges
13.3k26 silver badges46 bronze badges
add a comment
|
add a comment
|
with GNU awk, we can do:
awk 'function output() print >>FILENAME".out"
/ddd/ sub(/ddd.*/,""); output(); nextfile
output() ' 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]
add a comment
|
with GNU awk, we can do:
awk 'function output() print >>FILENAME".out"
/ddd/ sub(/ddd.*/,""); output(); nextfile
output() ' file[12]
with GNU awk, we can do:
awk 'function output() print >>FILENAME".out"
/ddd/ sub(/ddd.*/,""); output(); nextfile
output() ' file[12]
answered 7 hours ago
αғsнιηαғsнιη
25.9k23 gold badges105 silver badges167 bronze badges
25.9k23 gold badges105 silver badges167 bronze badges
add a comment
|
add a comment
|
With Perl:
perl -i -0777 -pe 's/ddd[sS]*//' 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.
More about Perl flags can be found here.
Why not justs/ddd.*//?
– dessert
6 hours ago
@dessert.*stops matching at newline.
– user3140225
5 hours ago
Ah, so a true equivalent would be[Nn]*– nice, thanks!
– dessert
4 hours ago
You can use..*if you add themorsflag to thes///command (I forget which one)
– glenn jackman
1 hour ago
add a comment
|
With Perl:
perl -i -0777 -pe 's/ddd[sS]*//' 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.
More about Perl flags can be found here.
Why not justs/ddd.*//?
– dessert
6 hours ago
@dessert.*stops matching at newline.
– user3140225
5 hours ago
Ah, so a true equivalent would be[Nn]*– nice, thanks!
– dessert
4 hours ago
You can use..*if you add themorsflag to thes///command (I forget which one)
– glenn jackman
1 hour ago
add a comment
|
With Perl:
perl -i -0777 -pe 's/ddd[sS]*//' 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.
More about Perl flags can be found here.
With Perl:
perl -i -0777 -pe 's/ddd[sS]*//' 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.
More about Perl flags can be found here.
edited 6 hours ago
dessert
29.2k7 gold badges87 silver badges121 bronze badges
29.2k7 gold badges87 silver badges121 bronze badges
answered 6 hours ago
user3140225user3140225
2,1668 silver badges22 bronze badges
2,1668 silver badges22 bronze badges
Why not justs/ddd.*//?
– dessert
6 hours ago
@dessert.*stops matching at newline.
– user3140225
5 hours ago
Ah, so a true equivalent would be[Nn]*– nice, thanks!
– dessert
4 hours ago
You can use..*if you add themorsflag to thes///command (I forget which one)
– glenn jackman
1 hour ago
add a comment
|
Why not justs/ddd.*//?
– dessert
6 hours ago
@dessert.*stops matching at newline.
– user3140225
5 hours ago
Ah, so a true equivalent would be[Nn]*– nice, thanks!
– dessert
4 hours ago
You can use..*if you add themorsflag to thes///command (I forget which one)
– glenn jackman
1 hour ago
Why not just
s/ddd.*//?– dessert
6 hours ago
Why not just
s/ddd.*//?– dessert
6 hours ago
@dessert
.* stops matching at newline.– user3140225
5 hours ago
@dessert
.* stops matching at newline.– user3140225
5 hours ago
Ah, so a true equivalent would be
[Nn]* – nice, thanks!– dessert
4 hours ago
Ah, so a true equivalent would be
[Nn]* – nice, thanks!– dessert
4 hours ago
You can use.
.* if you add the m or s flag to the s/// command (I forget which one)– glenn jackman
1 hour ago
You can use.
.* if you add the m or s flag to the s/// command (I forget which one)– glenn jackman
1 hour ago
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