sed delete all the words before a matchHow to count the number of words and print the lines that match exactly a given pattern?sed lines after match and before next matchUsing sed to replace wordsHow to delete all occurrences of a list of words from a text file?Delete lines containing the pattern and the line beforeHow to match a pattern in lines before another pattern matchRemove all words before a specific pattern, after another patternDelete n lines after pattern and m lines before patternhow to add words before line on all the scripts in current foldersed: delete all lines before matching one, including this one
sed delete all the words before a match
Why is there a need to prevent a racist, sexist, or otherwise bigoted vendor from discriminating who they sell to?
Is it possible to script what applications should open certain file extensions?
Short story about a teenager who has his brain replaced with a microchip (Psychological Horror)
How to mark beverage cans in a cooler for a blind person?
Are there any financial disadvantages to living significantly "below your means"?
How to say "fit" in Latin?
Ex-contractor published company source code and secrets online
Pandas: fill one column with count of # of obs between occurrences in a 2nd column
Secure my password from unsafe servers
Blocking people from taking pictures of me with smartphone
Shabbat clothing on shabbat chazon
Are any jet engines used in combat aircraft water cooled?
Improving software when the author can see no need for improvement
During the Space Shuttle Columbia Disaster of 2003, Why Did The Flight Director Say, "Lock the doors."?
Pretty heat maps
Why should public servants be apolitical?
How does The Fools Guild make its money?
Why couldn't soldiers sight their own weapons without officers' orders?
Why are there so many Doppler Effect formulas?
How would I as a DM create a smart phone-like spell/device my players could use?
Looking for a new job because of relocation - is it okay to tell the real reason?
Could one become a successful researcher by writing some really good papers while being outside academia?
Is refreshing multiple times a test case for web applications?
sed delete all the words before a match
How to count the number of words and print the lines that match exactly a given pattern?sed lines after match and before next matchUsing sed to replace wordsHow to delete all occurrences of a list of words from a text file?Delete lines containing the pattern and the line beforeHow to match a pattern in lines before another pattern matchRemove all words before a specific pattern, after another patternDelete n lines after pattern and m lines before patternhow to add words before line on all the scripts in current foldersed: delete all lines before matching one, including this one
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I want to delete all the words before a pattern for example: I want to delete all the words before STAC.
Input:
asd
asdd
asddd
STAC
asd
as
Output:
STAC
asd
as
I have this code sed -ni "s/^.*STAC//d" myfile
text-processing sed
New contributor
Nicolás Gómez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I want to delete all the words before a pattern for example: I want to delete all the words before STAC.
Input:
asd
asdd
asddd
STAC
asd
as
Output:
STAC
asd
as
I have this code sed -ni "s/^.*STAC//d" myfile
text-processing sed
New contributor
Nicolás Gómez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
If a line has that word it doesn't matter, you just have to eliminate everything that is before that precise word, that is, STAC no HAYSTACK or any combination. Just STAC
– Nicolás Gómez
9 hours ago
thanks for the comment. I just modified the input
– Nicolás Gómez
9 hours ago
add a comment |
I want to delete all the words before a pattern for example: I want to delete all the words before STAC.
Input:
asd
asdd
asddd
STAC
asd
as
Output:
STAC
asd
as
I have this code sed -ni "s/^.*STAC//d" myfile
text-processing sed
New contributor
Nicolás Gómez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I want to delete all the words before a pattern for example: I want to delete all the words before STAC.
Input:
asd
asdd
asddd
STAC
asd
as
Output:
STAC
asd
as
I have this code sed -ni "s/^.*STAC//d" myfile
text-processing sed
text-processing sed
New contributor
Nicolás Gómez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Nicolás Gómez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 9 hours ago
Nicolás Gómez
New contributor
Nicolás Gómez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 9 hours ago
Nicolás GómezNicolás Gómez
212 bronze badges
212 bronze badges
New contributor
Nicolás Gómez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Nicolás Gómez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
If a line has that word it doesn't matter, you just have to eliminate everything that is before that precise word, that is, STAC no HAYSTACK or any combination. Just STAC
– Nicolás Gómez
9 hours ago
thanks for the comment. I just modified the input
– Nicolás Gómez
9 hours ago
add a comment |
If a line has that word it doesn't matter, you just have to eliminate everything that is before that precise word, that is, STAC no HAYSTACK or any combination. Just STAC
– Nicolás Gómez
9 hours ago
thanks for the comment. I just modified the input
– Nicolás Gómez
9 hours ago
If a line has that word it doesn't matter, you just have to eliminate everything that is before that precise word, that is, STAC no HAYSTACK or any combination. Just STAC
– Nicolás Gómez
9 hours ago
If a line has that word it doesn't matter, you just have to eliminate everything that is before that precise word, that is, STAC no HAYSTACK or any combination. Just STAC
– Nicolás Gómez
9 hours ago
thanks for the comment. I just modified the input
– Nicolás Gómez
9 hours ago
thanks for the comment. I just modified the input
– Nicolás Gómez
9 hours ago
add a comment |
5 Answers
5
active
oldest
votes
sed works linewise, that's why your try will not work.
So how to do it with sed? Define an address range, starting from the STAC line (/^STAC$/) to the end of the file ($). Those should be printed, so everything else (!) should get deleted:
sed -i '/^STAC$/,$!d' myfile
2
Also possible:sed -ni '/^STAC$/,$p' myfile
– filbranden
7 hours ago
1
This is for GNUsedonly.
– Christopher
7 hours ago
@Christopher Indeed, the-ioption requires an argument (extension) for BSDsedand is not part of the standard. While the OP obviously uses GNUsed, your remark is valuable for future readers, so thank you!
– Philippos
6 hours ago
add a comment |
An awk variant which prints all lines after the match (including the match):
$ awk '/^STAC$/ out=1 out' file
STAC
asd
as
This matches the line that only contains the string STAC and sets out to a non-zero value. For each line, if out is non-zero, print it.
Use $0 == "STAC" instead of /^STAC$/ to do a string comparison instead of a regular expression match.
Slightly more obfuscated but shorter, using the boolean result of the match with the regular expression as an integer (will be 0 for a non-match, and 1 for a match):
awk 'p += /^STAC$/' file
If the result in p is non-zero, which it will be from the point where the regular expression first matches, the current line will be printed.
Use p += ($0 == "STAC") instead of p += /^STAC$/ to do a string comparison instead of a regular expression match.
add a comment |
Another option would be to use a scriptable editor like ed:
printf '%sn' '1,/^STAC/-1 d' 'wq' | ed -s myfile
This prints two commands to ed:
- delete lines from 1 through (the line before the one that starts with
STAC) - write the file back to disk and quit
The -s option inhibits ed's default printing of the number of bytes read & written.
add a comment |
Using awk:
awk '/^STAC$/,/$ /' input
This will print all lines between STAC and anything (including the matching lines)
Or using a grep that supports the -z option (BSD grep does not):
Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline.
grep -z 'STAC' input
add a comment |
grep -wn STAC file.txt | cut -d":" -f 1 | xargs -I % sed '1,%d' file.txt
Get line number of the word, pass it to xargs and use sed to delete.
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
);
);
Nicolás Gómez is a new contributor. Be nice, and check out our Code of Conduct.
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%2f534745%2fsed-delete-all-the-words-before-a-match%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
sed works linewise, that's why your try will not work.
So how to do it with sed? Define an address range, starting from the STAC line (/^STAC$/) to the end of the file ($). Those should be printed, so everything else (!) should get deleted:
sed -i '/^STAC$/,$!d' myfile
2
Also possible:sed -ni '/^STAC$/,$p' myfile
– filbranden
7 hours ago
1
This is for GNUsedonly.
– Christopher
7 hours ago
@Christopher Indeed, the-ioption requires an argument (extension) for BSDsedand is not part of the standard. While the OP obviously uses GNUsed, your remark is valuable for future readers, so thank you!
– Philippos
6 hours ago
add a comment |
sed works linewise, that's why your try will not work.
So how to do it with sed? Define an address range, starting from the STAC line (/^STAC$/) to the end of the file ($). Those should be printed, so everything else (!) should get deleted:
sed -i '/^STAC$/,$!d' myfile
2
Also possible:sed -ni '/^STAC$/,$p' myfile
– filbranden
7 hours ago
1
This is for GNUsedonly.
– Christopher
7 hours ago
@Christopher Indeed, the-ioption requires an argument (extension) for BSDsedand is not part of the standard. While the OP obviously uses GNUsed, your remark is valuable for future readers, so thank you!
– Philippos
6 hours ago
add a comment |
sed works linewise, that's why your try will not work.
So how to do it with sed? Define an address range, starting from the STAC line (/^STAC$/) to the end of the file ($). Those should be printed, so everything else (!) should get deleted:
sed -i '/^STAC$/,$!d' myfile
sed works linewise, that's why your try will not work.
So how to do it with sed? Define an address range, starting from the STAC line (/^STAC$/) to the end of the file ($). Those should be printed, so everything else (!) should get deleted:
sed -i '/^STAC$/,$!d' myfile
answered 8 hours ago
PhilipposPhilippos
7,0591 gold badge20 silver badges51 bronze badges
7,0591 gold badge20 silver badges51 bronze badges
2
Also possible:sed -ni '/^STAC$/,$p' myfile
– filbranden
7 hours ago
1
This is for GNUsedonly.
– Christopher
7 hours ago
@Christopher Indeed, the-ioption requires an argument (extension) for BSDsedand is not part of the standard. While the OP obviously uses GNUsed, your remark is valuable for future readers, so thank you!
– Philippos
6 hours ago
add a comment |
2
Also possible:sed -ni '/^STAC$/,$p' myfile
– filbranden
7 hours ago
1
This is for GNUsedonly.
– Christopher
7 hours ago
@Christopher Indeed, the-ioption requires an argument (extension) for BSDsedand is not part of the standard. While the OP obviously uses GNUsed, your remark is valuable for future readers, so thank you!
– Philippos
6 hours ago
2
2
Also possible:
sed -ni '/^STAC$/,$p' myfile– filbranden
7 hours ago
Also possible:
sed -ni '/^STAC$/,$p' myfile– filbranden
7 hours ago
1
1
This is for GNU
sed only.– Christopher
7 hours ago
This is for GNU
sed only.– Christopher
7 hours ago
@Christopher Indeed, the
-i option requires an argument (extension) for BSD sed and is not part of the standard. While the OP obviously uses GNU sed, your remark is valuable for future readers, so thank you!– Philippos
6 hours ago
@Christopher Indeed, the
-i option requires an argument (extension) for BSD sed and is not part of the standard. While the OP obviously uses GNU sed, your remark is valuable for future readers, so thank you!– Philippos
6 hours ago
add a comment |
An awk variant which prints all lines after the match (including the match):
$ awk '/^STAC$/ out=1 out' file
STAC
asd
as
This matches the line that only contains the string STAC and sets out to a non-zero value. For each line, if out is non-zero, print it.
Use $0 == "STAC" instead of /^STAC$/ to do a string comparison instead of a regular expression match.
Slightly more obfuscated but shorter, using the boolean result of the match with the regular expression as an integer (will be 0 for a non-match, and 1 for a match):
awk 'p += /^STAC$/' file
If the result in p is non-zero, which it will be from the point where the regular expression first matches, the current line will be printed.
Use p += ($0 == "STAC") instead of p += /^STAC$/ to do a string comparison instead of a regular expression match.
add a comment |
An awk variant which prints all lines after the match (including the match):
$ awk '/^STAC$/ out=1 out' file
STAC
asd
as
This matches the line that only contains the string STAC and sets out to a non-zero value. For each line, if out is non-zero, print it.
Use $0 == "STAC" instead of /^STAC$/ to do a string comparison instead of a regular expression match.
Slightly more obfuscated but shorter, using the boolean result of the match with the regular expression as an integer (will be 0 for a non-match, and 1 for a match):
awk 'p += /^STAC$/' file
If the result in p is non-zero, which it will be from the point where the regular expression first matches, the current line will be printed.
Use p += ($0 == "STAC") instead of p += /^STAC$/ to do a string comparison instead of a regular expression match.
add a comment |
An awk variant which prints all lines after the match (including the match):
$ awk '/^STAC$/ out=1 out' file
STAC
asd
as
This matches the line that only contains the string STAC and sets out to a non-zero value. For each line, if out is non-zero, print it.
Use $0 == "STAC" instead of /^STAC$/ to do a string comparison instead of a regular expression match.
Slightly more obfuscated but shorter, using the boolean result of the match with the regular expression as an integer (will be 0 for a non-match, and 1 for a match):
awk 'p += /^STAC$/' file
If the result in p is non-zero, which it will be from the point where the regular expression first matches, the current line will be printed.
Use p += ($0 == "STAC") instead of p += /^STAC$/ to do a string comparison instead of a regular expression match.
An awk variant which prints all lines after the match (including the match):
$ awk '/^STAC$/ out=1 out' file
STAC
asd
as
This matches the line that only contains the string STAC and sets out to a non-zero value. For each line, if out is non-zero, print it.
Use $0 == "STAC" instead of /^STAC$/ to do a string comparison instead of a regular expression match.
Slightly more obfuscated but shorter, using the boolean result of the match with the regular expression as an integer (will be 0 for a non-match, and 1 for a match):
awk 'p += /^STAC$/' file
If the result in p is non-zero, which it will be from the point where the regular expression first matches, the current line will be printed.
Use p += ($0 == "STAC") instead of p += /^STAC$/ to do a string comparison instead of a regular expression match.
edited 3 hours ago
answered 7 hours ago
Kusalananda♦Kusalananda
158k18 gold badges313 silver badges499 bronze badges
158k18 gold badges313 silver badges499 bronze badges
add a comment |
add a comment |
Another option would be to use a scriptable editor like ed:
printf '%sn' '1,/^STAC/-1 d' 'wq' | ed -s myfile
This prints two commands to ed:
- delete lines from 1 through (the line before the one that starts with
STAC) - write the file back to disk and quit
The -s option inhibits ed's default printing of the number of bytes read & written.
add a comment |
Another option would be to use a scriptable editor like ed:
printf '%sn' '1,/^STAC/-1 d' 'wq' | ed -s myfile
This prints two commands to ed:
- delete lines from 1 through (the line before the one that starts with
STAC) - write the file back to disk and quit
The -s option inhibits ed's default printing of the number of bytes read & written.
add a comment |
Another option would be to use a scriptable editor like ed:
printf '%sn' '1,/^STAC/-1 d' 'wq' | ed -s myfile
This prints two commands to ed:
- delete lines from 1 through (the line before the one that starts with
STAC) - write the file back to disk and quit
The -s option inhibits ed's default printing of the number of bytes read & written.
Another option would be to use a scriptable editor like ed:
printf '%sn' '1,/^STAC/-1 d' 'wq' | ed -s myfile
This prints two commands to ed:
- delete lines from 1 through (the line before the one that starts with
STAC) - write the file back to disk and quit
The -s option inhibits ed's default printing of the number of bytes read & written.
answered 7 hours ago
Jeff Schaller♦Jeff Schaller
48.7k11 gold badges72 silver badges162 bronze badges
48.7k11 gold badges72 silver badges162 bronze badges
add a comment |
add a comment |
Using awk:
awk '/^STAC$/,/$ /' input
This will print all lines between STAC and anything (including the matching lines)
Or using a grep that supports the -z option (BSD grep does not):
Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline.
grep -z 'STAC' input
add a comment |
Using awk:
awk '/^STAC$/,/$ /' input
This will print all lines between STAC and anything (including the matching lines)
Or using a grep that supports the -z option (BSD grep does not):
Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline.
grep -z 'STAC' input
add a comment |
Using awk:
awk '/^STAC$/,/$ /' input
This will print all lines between STAC and anything (including the matching lines)
Or using a grep that supports the -z option (BSD grep does not):
Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline.
grep -z 'STAC' input
Using awk:
awk '/^STAC$/,/$ /' input
This will print all lines between STAC and anything (including the matching lines)
Or using a grep that supports the -z option (BSD grep does not):
Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline.
grep -z 'STAC' input
edited 7 hours ago
answered 9 hours ago
Jesse_bJesse_b
18.5k3 gold badges46 silver badges86 bronze badges
18.5k3 gold badges46 silver badges86 bronze badges
add a comment |
add a comment |
grep -wn STAC file.txt | cut -d":" -f 1 | xargs -I % sed '1,%d' file.txt
Get line number of the word, pass it to xargs and use sed to delete.
add a comment |
grep -wn STAC file.txt | cut -d":" -f 1 | xargs -I % sed '1,%d' file.txt
Get line number of the word, pass it to xargs and use sed to delete.
add a comment |
grep -wn STAC file.txt | cut -d":" -f 1 | xargs -I % sed '1,%d' file.txt
Get line number of the word, pass it to xargs and use sed to delete.
grep -wn STAC file.txt | cut -d":" -f 1 | xargs -I % sed '1,%d' file.txt
Get line number of the word, pass it to xargs and use sed to delete.
answered 4 hours ago
Death MetalDeath Metal
793 silver badges12 bronze badges
793 silver badges12 bronze badges
add a comment |
add a comment |
Nicolás Gómez is a new contributor. Be nice, and check out our Code of Conduct.
Nicolás Gómez is a new contributor. Be nice, and check out our Code of Conduct.
Nicolás Gómez is a new contributor. Be nice, and check out our Code of Conduct.
Nicolás Gómez is a new contributor. Be nice, and check out our Code of Conduct.
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%2f534745%2fsed-delete-all-the-words-before-a-match%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
If a line has that word it doesn't matter, you just have to eliminate everything that is before that precise word, that is, STAC no HAYSTACK or any combination. Just STAC
– Nicolás Gómez
9 hours ago
thanks for the comment. I just modified the input
– Nicolás Gómez
9 hours ago