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?How 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
Getting data from Seagate ST-238R drive
What does Ȝecyndbēc mean?
What are pitfalls I should be aware of with NPC/PC romantic relationships?
How do people create difficult, recreational problems (e.g. like those found in competitions such as the IMO)?
Find difference between dates in Excel
Doing chemistry under water?
Why didn't Abraham ask the single best question?
What color is a wolf's coat?
How to quantify Code Quality
Numeric example to understand the effect of option gamma
Paint Exterior Door that is in Sun
What to do if caught in a physical pentest?
Does the German President's apology for WWII reflect the views of the people of Germany?
What makes an airport "international"?
Does every truth have to be provable based on evidence?
When "this" is captured by a lambda, does it have to be used explicitly?
Are the expansion number tokens in 5-6 players expansion different from the basic Catan?
LACPDU packets to pass through unmanaged switch
Get verbatim output of 'curl wttr.in' in org src code
Expand a recursive pattern
Are all LTI systems invertible? If not, what is a good counterexample?
Does a lich die if its phylactery is destroyed, or can it simply not rejuvenate anymore?
How should I say "where", as in a mathematical definitions?
Is camera at risk of condensation in cold temperatures if never removed from bag?
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?How 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
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
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
– nicg
Aug 9 at 14:48
thanks for the comment. I just modified the input
– nicg
Aug 9 at 14:52
awk '/^STAC$/,0'
,awk '$0=="STAC",0'
,grep -A100000 '^STAC$'
– mosvy
Aug 10 at 0:20
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
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
edited Aug 9 at 14:51
nicg
asked Aug 9 at 14:41
nicgnicg
313 bronze badges
313 bronze badges
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
– nicg
Aug 9 at 14:48
thanks for the comment. I just modified the input
– nicg
Aug 9 at 14:52
awk '/^STAC$/,0'
,awk '$0=="STAC",0'
,grep -A100000 '^STAC$'
– mosvy
Aug 10 at 0:20
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
– nicg
Aug 9 at 14:48
thanks for the comment. I just modified the input
– nicg
Aug 9 at 14:52
awk '/^STAC$/,0'
,awk '$0=="STAC",0'
,grep -A100000 '^STAC$'
– mosvy
Aug 10 at 0:20
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
– nicg
Aug 9 at 14:48
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
– nicg
Aug 9 at 14:48
thanks for the comment. I just modified the input
– nicg
Aug 9 at 14:52
thanks for the comment. I just modified the input
– nicg
Aug 9 at 14:52
awk '/^STAC$/,0'
, awk '$0=="STAC",0'
, grep -A100000 '^STAC$'
– mosvy
Aug 10 at 0:20
awk '/^STAC$/,0'
, awk '$0=="STAC",0'
, grep -A100000 '^STAC$'
– mosvy
Aug 10 at 0:20
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 d
eleted:
sed -i '/^STAC$/,$!d' myfile
2
Also possible:sed -ni '/^STAC$/,$p' myfile
– filbranden
Aug 9 at 16:17
1
This is for GNUsed
only.
– Christopher
Aug 9 at 17:03
@Christopher Indeed, the-i
option requires an argument (extension) for BSDsed
and is not part of the standard. While the OP obviously uses GNUsed
, your remark is valuable for future readers, so thank you!
– Philippos
Aug 9 at 17:16
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/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%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 d
eleted:
sed -i '/^STAC$/,$!d' myfile
2
Also possible:sed -ni '/^STAC$/,$p' myfile
– filbranden
Aug 9 at 16:17
1
This is for GNUsed
only.
– Christopher
Aug 9 at 17:03
@Christopher Indeed, the-i
option requires an argument (extension) for BSDsed
and is not part of the standard. While the OP obviously uses GNUsed
, your remark is valuable for future readers, so thank you!
– Philippos
Aug 9 at 17:16
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 d
eleted:
sed -i '/^STAC$/,$!d' myfile
2
Also possible:sed -ni '/^STAC$/,$p' myfile
– filbranden
Aug 9 at 16:17
1
This is for GNUsed
only.
– Christopher
Aug 9 at 17:03
@Christopher Indeed, the-i
option requires an argument (extension) for BSDsed
and is not part of the standard. While the OP obviously uses GNUsed
, your remark is valuable for future readers, so thank you!
– Philippos
Aug 9 at 17:16
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 d
eleted:
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 d
eleted:
sed -i '/^STAC$/,$!d' myfile
answered Aug 9 at 15:46
PhilipposPhilippos
7,8631 gold badge21 silver badges56 bronze badges
7,8631 gold badge21 silver badges56 bronze badges
2
Also possible:sed -ni '/^STAC$/,$p' myfile
– filbranden
Aug 9 at 16:17
1
This is for GNUsed
only.
– Christopher
Aug 9 at 17:03
@Christopher Indeed, the-i
option requires an argument (extension) for BSDsed
and is not part of the standard. While the OP obviously uses GNUsed
, your remark is valuable for future readers, so thank you!
– Philippos
Aug 9 at 17:16
add a comment
|
2
Also possible:sed -ni '/^STAC$/,$p' myfile
– filbranden
Aug 9 at 16:17
1
This is for GNUsed
only.
– Christopher
Aug 9 at 17:03
@Christopher Indeed, the-i
option requires an argument (extension) for BSDsed
and is not part of the standard. While the OP obviously uses GNUsed
, your remark is valuable for future readers, so thank you!
– Philippos
Aug 9 at 17:16
2
2
Also possible:
sed -ni '/^STAC$/,$p' myfile
– filbranden
Aug 9 at 16:17
Also possible:
sed -ni '/^STAC$/,$p' myfile
– filbranden
Aug 9 at 16:17
1
1
This is for GNU
sed
only.– Christopher
Aug 9 at 17:03
This is for GNU
sed
only.– Christopher
Aug 9 at 17:03
@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
Aug 9 at 17:16
@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
Aug 9 at 17:16
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 Aug 9 at 20:16
answered Aug 9 at 16:54
Kusalananda♦Kusalananda
171k20 gold badges330 silver badges535 bronze badges
171k20 gold badges330 silver badges535 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 Aug 9 at 16:38
Jeff Schaller♦Jeff Schaller
51.6k11 gold badges76 silver badges171 bronze badges
51.6k11 gold badges76 silver badges171 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 Aug 9 at 16:49
answered Aug 9 at 14:53
Jesse_bJesse_b
20.5k3 gold badges49 silver badges92 bronze badges
20.5k3 gold badges49 silver badges92 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 Aug 9 at 19:19
Death MetalDeath Metal
893 silver badges13 bronze badges
893 silver badges13 bronze badges
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%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
– nicg
Aug 9 at 14:48
thanks for the comment. I just modified the input
– nicg
Aug 9 at 14:52
awk '/^STAC$/,0'
,awk '$0=="STAC",0'
,grep -A100000 '^STAC$'
– mosvy
Aug 10 at 0:20