How to print all lines after a match up to the end of the file?
Input file1 is:
dog 123 4335
cat 13123 23424
deer 2131 213132
bear 2313 21313
I give the match the pattern from in other file
( like dog 123 4335
from file2).
I match the pattern of the line is dog 123 4335
and after printing
all lines without match line my output is:
cat 13123 23424
deer 2131 213132
bear 2313 21313
If only use without address of line only use the pattern, for example 1s
how to match and print the lines?
text-processing sed grep
add a comment |
Input file1 is:
dog 123 4335
cat 13123 23424
deer 2131 213132
bear 2313 21313
I give the match the pattern from in other file
( like dog 123 4335
from file2).
I match the pattern of the line is dog 123 4335
and after printing
all lines without match line my output is:
cat 13123 23424
deer 2131 213132
bear 2313 21313
If only use without address of line only use the pattern, for example 1s
how to match and print the lines?
text-processing sed grep
Can other file contain just a single pattern to search for, or one per line, and start searching at whichever line is found first in the searched file?
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Sep 14 '16 at 12:52
add a comment |
Input file1 is:
dog 123 4335
cat 13123 23424
deer 2131 213132
bear 2313 21313
I give the match the pattern from in other file
( like dog 123 4335
from file2).
I match the pattern of the line is dog 123 4335
and after printing
all lines without match line my output is:
cat 13123 23424
deer 2131 213132
bear 2313 21313
If only use without address of line only use the pattern, for example 1s
how to match and print the lines?
text-processing sed grep
Input file1 is:
dog 123 4335
cat 13123 23424
deer 2131 213132
bear 2313 21313
I give the match the pattern from in other file
( like dog 123 4335
from file2).
I match the pattern of the line is dog 123 4335
and after printing
all lines without match line my output is:
cat 13123 23424
deer 2131 213132
bear 2313 21313
If only use without address of line only use the pattern, for example 1s
how to match and print the lines?
text-processing sed grep
text-processing sed grep
edited Nov 23 '12 at 23:29
Gilles
541k12810941610
541k12810941610
asked Nov 23 '12 at 7:17
loganaayaheeloganaayahee
369136
369136
Can other file contain just a single pattern to search for, or one per line, and start searching at whichever line is found first in the searched file?
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Sep 14 '16 at 12:52
add a comment |
Can other file contain just a single pattern to search for, or one per line, and start searching at whichever line is found first in the searched file?
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Sep 14 '16 at 12:52
Can other file contain just a single pattern to search for, or one per line, and start searching at whichever line is found first in the searched file?
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Sep 14 '16 at 12:52
Can other file contain just a single pattern to search for, or one per line, and start searching at whichever line is found first in the searched file?
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Sep 14 '16 at 12:52
add a comment |
12 Answers
12
active
oldest
votes
If you have a reasonably short file grep
alone might work:
grep -A5000 -m1 -e 'dog 123 4335' animals.txt
5000 is just my guess at "reasonably short", as grep
finds the first match and outputs it together with the next 5000 lines (the file doesn't need to have that many). If you don't want the match itself you'll need to cut it off, e.g.
grep -A5000 -m1 -e 'dog 123 4335' animals.txt | tail -n+2
If you do not want the first, but the last match as delimiter you could use this:
tac animals.txt | sed -e '/dog 123 4335/q' | tac
This line reads animals.txt
in reverse order of lines and outputs up to and including the line with dog 123 4335
and then reverses again to restore proper order.
Again, if you don't need the match in the result, append tail. (You could also complicate the sed expression to discard its buffer before quitting.)
By my test, GNU grep 3.0 doesn't output more than 132 lines in after-context (regardless of specified value).
– ruvim
Aug 29 '17 at 9:44
add a comment |
Assuming you want to match the whole line with your pattern, with GNU sed
, this works:
sed -n '/^dog 123 4335$/ { :a; n; p; ba; }' infile
Standard equivalent:
sed -ne '/^dog 123 4335$/{:a' -e 'n;p;ba' -e '}' infile
With the following input (infile
):
cat 13123 23424
deer 2131 213132
bear 2313 21313
dog 123 4335
cat 13123 23424
deer 2131 213132
bear 2313 21313
The output is:
cat 13123 23424
deer 2131 213132
bear 2313 21313
Explanation:
/^dog 123 4335$/
searches for the desired pattern.
:a; n; p; ba;
is a loop that fetches a new line from input (n
), prints it (p
), and branches back to label a:a; ...; ba;
.
Update
Here's an answer that comes closer to your needs, i.e. pattern in file2, grepping from file1:
tail -n +$(( 1 + $(grep -m1 -n -f file2 file1 | cut -d: -f1) )) file1
The embedded grep and cut find the first line containing a pattern from file2, this line number plus one is passed on to tail, the plus one is there to skip the line with the pattern.
If you want to start from the last match instead of the first match it would be:
tail -n +$(( 1 + $(grep -n -f file2 file1 | tail -n1 | cut -d: -f1) )) file1
Note that not all versions of tail support the plus-notation.
@don_crissti: rightn
takes care of that. Thanks.
– Thor
Sep 18 '18 at 11:12
add a comment |
In practice I'd probably use Aet3miirah's answer most of the time and alexey's answer is wonderful when wanting to navigate through the lines (also, it also works with less
). OTOH, I really like another approach (which is kind of the reversed Gilles' answer:
sed -n '/dog 123 4335/,$p'
When called with the -n
flag, sed
does not print by default the lines it processes any more. Then we use a 2-address form that says to apply a command from the line matching /dog 123 4335/
until the end of the file (represented by $
). The command in question is p
, which prints the current line. So, this means "print all lines from the one matching /dog 123 4335/
until the end."
3
That prints thedog
line though which is not wanted here.
– Stéphane Chazelas
Jun 10 '15 at 9:49
1
This looks like the best answer (and works for my own case) but would need to be adapted to skip the matched line as well.
– Pavel Šimerda
Mar 14 '16 at 7:17
1
sed -n '/dog 123 4335/,$p' | sed '1d' will remove the dog line
– Kemin Zhou
Jul 2 '18 at 5:26
1
sed -n '/dog 123 4335/,$p' | tail -n +2
will remove the match as well
– gilad mayani
Oct 24 '18 at 13:01
add a comment |
sed -e '1,/dog 123 4335/d' file1
If you need to read the pattern from a file, substitute it into the sed command. If the file contains a sed pattern:
sed -e "1,/$(cat file2)/d" file1
If the file contains a literal string to look for, quote all special characters. I assume the file contains a single line.
sed -e "1,/$(sed 's/[\/^$.*]/\&/g' file2)/d" file1
If you want the match to be the whole line, not just a substring, wrap the pattern in ^…$
.
sed -e "1,/^$(sed 's/[\/^$.*]/\&/g' file2)$/d" file1
6
That won't work if the pattern is on the first line. GNUsed
has0,/dog.../d
for that.
– Stéphane Chazelas
Jun 10 '15 at 9:48
add a comment |
$ more +/"dog 123 4335" file1
4
It also works withless
.
– brandizzi
May 26 '15 at 13:40
3
clever on the terminal, but it doesn't actually work if you pipe it into something else liketac
.
– jcomeau_ictx
Jul 2 '15 at 6:25
i am using it like this , $ more +/"match my words" file1 >> file2
– AMB
Jul 7 '15 at 17:38
1
Maybe+
was replaced by-p
in POSIX 7: pubs.opengroup.org/onlinepubs/9699919799/utilities/more.html but not yet implemented in util-linux 2.20.1. And this also printsskipping..
and some extra newlines (to stderr I expect, so might be fine).
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Sep 14 '16 at 12:41
@jcomeau_ictx: What do you mean? It works fine here
– Thor
Nov 3 '17 at 9:57
|
show 1 more comment
With awk
:
awk 'BEGIN {getline pattern < "other file"}
NR == 1, $0 ~ pattern {next}; {print}' < "input file"
add a comment |
If the input is an lseekable regular file:
With GNU grep
:
{ grep -xFm1 'dog 123 4335' >&2
cat; } <infile 2>/dev/null >outfile
With sed
:
{ sed -n '/^dog 123 4335$/q'
cat; } <infile >outfile
A GNU grep
called w/ the -m
option will quit input at the match - and it will leave its (lseekable) input fd immediately after the point it found its last match. So calling grep
w/ -m1
finds the first occurrence of a pattern in a file, and leaves the input offset at precisely the right place for cat
to write everything following the pattern's first match in a file to stdout.
Even without a GNU grep
you can do the exact same thing w/ a POSIX compatible sed
- when sed
q
uits it is specified to leave its input offset right where it does. GNU sed
is not standards-compliant in this way, though, and so the above will likely not work w/ a GNU sed
unless you call it with its -u
switch.
note, thesed
stream sharing demonstrated here is not specially (though, yes, the standard referenced does specifically examplesed
as a utility thus capable) of the free-form and conditionally cooperative workflow shown. notably, all standard utilities are meant and specified to thus cooperate and share cursor positions of input streams without failing the next reader any processing at all.grep -q
should do this; quietlygrep
should return as soon as any match in input is found, and any remaining of input should not, by standard, be consumed by default.
– mikeserv
Oct 29 '18 at 10:43
add a comment |
One way using awk:
awk 'NR==FNR{a[$0];next}f;($0 in a){f=1}' file2 file1
where file2 contains your search patterns. First, all the contents of file2 are stored in the array "a". When the file1 is processed, every line is checked against the array, and printed only if is not present.
I think the OP wants to output every line following the pattern.
– Thor
Nov 23 '12 at 11:41
@Thor : thanks for pointing it out, updated it now...
– Guru
Nov 23 '12 at 11:57
Nicely done :).
– Thor
Nov 23 '12 at 13:04
add a comment |
My answer for the question in the subject, without storing pattern in a second file.
Here is my test file:
$ cat animals.txt
cat 13123 23424
deer 2131 213132
bear 2313 21313
dog 123 4335
cat 13123 23424
deer 2131 213132
bear 2313 21313
GNU sed:
$ sed '0,/^dog 123 4335$/d' animals.txt
cat 13123 23424
deer 2131 213132
bear 2313 21313
Perl:
$ perl -ne 'print unless 1.../^dog 123 4335$/' animals.txt
cat 13123 23424
deer 2131 213132
bear 2313 21313
Perl variant with pattern in a file:
$ cat pattern.txt
dog 123 4335
$ perl -ne 'BEGIN{chomp($p=(<STDIN>)[0])};print unless 1../$p/;' animals.txt < pattern.txt
cat 13123 23424
deer 2131 213132
bear 2313 21313
add a comment |
How to print all lines after a match up to the end of the file?
Another way to put it is "how to delete all lines from the 1st one till the match (including)", and this can be sed
-written as:
sed -e '1,/MATCH PATTERN/d'
1
The only problem is when the pattern is on the first line...
– don_crissti
Sep 24 '15 at 20:45
1
Is this different from unix.stackexchange.com/a/56517/32558 ?
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Sep 14 '16 at 12:42
I guess we need a committee here to decide.
– poige
Sep 14 '16 at 14:02
1
@poige: nah, you provide the same answer less comprehensively
– Thor
Nov 3 '17 at 10:03
@don_crissti, what aboutsed -e '0,/MATCH PATTERN/d'
then?
– Velkan
Dec 5 '17 at 11:02
|
show 1 more comment
Wth ed
:
ed -s file1 <<< $'/dog 123 4335/+1,$p'
This sends one p
rint command to ed in a here-string; the print command is limited in range to one after (+1
) the dog 123 4335
match until the end of the file ($
).
add a comment |
Since awk isn't expressly disallowed, here's my offering assuming 'cat' is the match.
awk '$0 ~ /cat/ { vart = NR }{ arr[NR]=$0 } END { for (i = vart; i<=NR ; i++) print arr[i] }' animals.txt
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
});
}
});
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%2f56429%2fhow-to-print-all-lines-after-a-match-up-to-the-end-of-the-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
12 Answers
12
active
oldest
votes
12 Answers
12
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you have a reasonably short file grep
alone might work:
grep -A5000 -m1 -e 'dog 123 4335' animals.txt
5000 is just my guess at "reasonably short", as grep
finds the first match and outputs it together with the next 5000 lines (the file doesn't need to have that many). If you don't want the match itself you'll need to cut it off, e.g.
grep -A5000 -m1 -e 'dog 123 4335' animals.txt | tail -n+2
If you do not want the first, but the last match as delimiter you could use this:
tac animals.txt | sed -e '/dog 123 4335/q' | tac
This line reads animals.txt
in reverse order of lines and outputs up to and including the line with dog 123 4335
and then reverses again to restore proper order.
Again, if you don't need the match in the result, append tail. (You could also complicate the sed expression to discard its buffer before quitting.)
By my test, GNU grep 3.0 doesn't output more than 132 lines in after-context (regardless of specified value).
– ruvim
Aug 29 '17 at 9:44
add a comment |
If you have a reasonably short file grep
alone might work:
grep -A5000 -m1 -e 'dog 123 4335' animals.txt
5000 is just my guess at "reasonably short", as grep
finds the first match and outputs it together with the next 5000 lines (the file doesn't need to have that many). If you don't want the match itself you'll need to cut it off, e.g.
grep -A5000 -m1 -e 'dog 123 4335' animals.txt | tail -n+2
If you do not want the first, but the last match as delimiter you could use this:
tac animals.txt | sed -e '/dog 123 4335/q' | tac
This line reads animals.txt
in reverse order of lines and outputs up to and including the line with dog 123 4335
and then reverses again to restore proper order.
Again, if you don't need the match in the result, append tail. (You could also complicate the sed expression to discard its buffer before quitting.)
By my test, GNU grep 3.0 doesn't output more than 132 lines in after-context (regardless of specified value).
– ruvim
Aug 29 '17 at 9:44
add a comment |
If you have a reasonably short file grep
alone might work:
grep -A5000 -m1 -e 'dog 123 4335' animals.txt
5000 is just my guess at "reasonably short", as grep
finds the first match and outputs it together with the next 5000 lines (the file doesn't need to have that many). If you don't want the match itself you'll need to cut it off, e.g.
grep -A5000 -m1 -e 'dog 123 4335' animals.txt | tail -n+2
If you do not want the first, but the last match as delimiter you could use this:
tac animals.txt | sed -e '/dog 123 4335/q' | tac
This line reads animals.txt
in reverse order of lines and outputs up to and including the line with dog 123 4335
and then reverses again to restore proper order.
Again, if you don't need the match in the result, append tail. (You could also complicate the sed expression to discard its buffer before quitting.)
If you have a reasonably short file grep
alone might work:
grep -A5000 -m1 -e 'dog 123 4335' animals.txt
5000 is just my guess at "reasonably short", as grep
finds the first match and outputs it together with the next 5000 lines (the file doesn't need to have that many). If you don't want the match itself you'll need to cut it off, e.g.
grep -A5000 -m1 -e 'dog 123 4335' animals.txt | tail -n+2
If you do not want the first, but the last match as delimiter you could use this:
tac animals.txt | sed -e '/dog 123 4335/q' | tac
This line reads animals.txt
in reverse order of lines and outputs up to and including the line with dog 123 4335
and then reverses again to restore proper order.
Again, if you don't need the match in the result, append tail. (You could also complicate the sed expression to discard its buffer before quitting.)
answered Jul 2 '14 at 8:40
Aet3miirahAet3miirah
36132
36132
By my test, GNU grep 3.0 doesn't output more than 132 lines in after-context (regardless of specified value).
– ruvim
Aug 29 '17 at 9:44
add a comment |
By my test, GNU grep 3.0 doesn't output more than 132 lines in after-context (regardless of specified value).
– ruvim
Aug 29 '17 at 9:44
By my test, GNU grep 3.0 doesn't output more than 132 lines in after-context (regardless of specified value).
– ruvim
Aug 29 '17 at 9:44
By my test, GNU grep 3.0 doesn't output more than 132 lines in after-context (regardless of specified value).
– ruvim
Aug 29 '17 at 9:44
add a comment |
Assuming you want to match the whole line with your pattern, with GNU sed
, this works:
sed -n '/^dog 123 4335$/ { :a; n; p; ba; }' infile
Standard equivalent:
sed -ne '/^dog 123 4335$/{:a' -e 'n;p;ba' -e '}' infile
With the following input (infile
):
cat 13123 23424
deer 2131 213132
bear 2313 21313
dog 123 4335
cat 13123 23424
deer 2131 213132
bear 2313 21313
The output is:
cat 13123 23424
deer 2131 213132
bear 2313 21313
Explanation:
/^dog 123 4335$/
searches for the desired pattern.
:a; n; p; ba;
is a loop that fetches a new line from input (n
), prints it (p
), and branches back to label a:a; ...; ba;
.
Update
Here's an answer that comes closer to your needs, i.e. pattern in file2, grepping from file1:
tail -n +$(( 1 + $(grep -m1 -n -f file2 file1 | cut -d: -f1) )) file1
The embedded grep and cut find the first line containing a pattern from file2, this line number plus one is passed on to tail, the plus one is there to skip the line with the pattern.
If you want to start from the last match instead of the first match it would be:
tail -n +$(( 1 + $(grep -n -f file2 file1 | tail -n1 | cut -d: -f1) )) file1
Note that not all versions of tail support the plus-notation.
@don_crissti: rightn
takes care of that. Thanks.
– Thor
Sep 18 '18 at 11:12
add a comment |
Assuming you want to match the whole line with your pattern, with GNU sed
, this works:
sed -n '/^dog 123 4335$/ { :a; n; p; ba; }' infile
Standard equivalent:
sed -ne '/^dog 123 4335$/{:a' -e 'n;p;ba' -e '}' infile
With the following input (infile
):
cat 13123 23424
deer 2131 213132
bear 2313 21313
dog 123 4335
cat 13123 23424
deer 2131 213132
bear 2313 21313
The output is:
cat 13123 23424
deer 2131 213132
bear 2313 21313
Explanation:
/^dog 123 4335$/
searches for the desired pattern.
:a; n; p; ba;
is a loop that fetches a new line from input (n
), prints it (p
), and branches back to label a:a; ...; ba;
.
Update
Here's an answer that comes closer to your needs, i.e. pattern in file2, grepping from file1:
tail -n +$(( 1 + $(grep -m1 -n -f file2 file1 | cut -d: -f1) )) file1
The embedded grep and cut find the first line containing a pattern from file2, this line number plus one is passed on to tail, the plus one is there to skip the line with the pattern.
If you want to start from the last match instead of the first match it would be:
tail -n +$(( 1 + $(grep -n -f file2 file1 | tail -n1 | cut -d: -f1) )) file1
Note that not all versions of tail support the plus-notation.
@don_crissti: rightn
takes care of that. Thanks.
– Thor
Sep 18 '18 at 11:12
add a comment |
Assuming you want to match the whole line with your pattern, with GNU sed
, this works:
sed -n '/^dog 123 4335$/ { :a; n; p; ba; }' infile
Standard equivalent:
sed -ne '/^dog 123 4335$/{:a' -e 'n;p;ba' -e '}' infile
With the following input (infile
):
cat 13123 23424
deer 2131 213132
bear 2313 21313
dog 123 4335
cat 13123 23424
deer 2131 213132
bear 2313 21313
The output is:
cat 13123 23424
deer 2131 213132
bear 2313 21313
Explanation:
/^dog 123 4335$/
searches for the desired pattern.
:a; n; p; ba;
is a loop that fetches a new line from input (n
), prints it (p
), and branches back to label a:a; ...; ba;
.
Update
Here's an answer that comes closer to your needs, i.e. pattern in file2, grepping from file1:
tail -n +$(( 1 + $(grep -m1 -n -f file2 file1 | cut -d: -f1) )) file1
The embedded grep and cut find the first line containing a pattern from file2, this line number plus one is passed on to tail, the plus one is there to skip the line with the pattern.
If you want to start from the last match instead of the first match it would be:
tail -n +$(( 1 + $(grep -n -f file2 file1 | tail -n1 | cut -d: -f1) )) file1
Note that not all versions of tail support the plus-notation.
Assuming you want to match the whole line with your pattern, with GNU sed
, this works:
sed -n '/^dog 123 4335$/ { :a; n; p; ba; }' infile
Standard equivalent:
sed -ne '/^dog 123 4335$/{:a' -e 'n;p;ba' -e '}' infile
With the following input (infile
):
cat 13123 23424
deer 2131 213132
bear 2313 21313
dog 123 4335
cat 13123 23424
deer 2131 213132
bear 2313 21313
The output is:
cat 13123 23424
deer 2131 213132
bear 2313 21313
Explanation:
/^dog 123 4335$/
searches for the desired pattern.
:a; n; p; ba;
is a loop that fetches a new line from input (n
), prints it (p
), and branches back to label a:a; ...; ba;
.
Update
Here's an answer that comes closer to your needs, i.e. pattern in file2, grepping from file1:
tail -n +$(( 1 + $(grep -m1 -n -f file2 file1 | cut -d: -f1) )) file1
The embedded grep and cut find the first line containing a pattern from file2, this line number plus one is passed on to tail, the plus one is there to skip the line with the pattern.
If you want to start from the last match instead of the first match it would be:
tail -n +$(( 1 + $(grep -n -f file2 file1 | tail -n1 | cut -d: -f1) )) file1
Note that not all versions of tail support the plus-notation.
edited Oct 26 '18 at 8:38
answered Nov 23 '12 at 7:36
ThorThor
11.9k13459
11.9k13459
@don_crissti: rightn
takes care of that. Thanks.
– Thor
Sep 18 '18 at 11:12
add a comment |
@don_crissti: rightn
takes care of that. Thanks.
– Thor
Sep 18 '18 at 11:12
@don_crissti: right
n
takes care of that. Thanks.– Thor
Sep 18 '18 at 11:12
@don_crissti: right
n
takes care of that. Thanks.– Thor
Sep 18 '18 at 11:12
add a comment |
In practice I'd probably use Aet3miirah's answer most of the time and alexey's answer is wonderful when wanting to navigate through the lines (also, it also works with less
). OTOH, I really like another approach (which is kind of the reversed Gilles' answer:
sed -n '/dog 123 4335/,$p'
When called with the -n
flag, sed
does not print by default the lines it processes any more. Then we use a 2-address form that says to apply a command from the line matching /dog 123 4335/
until the end of the file (represented by $
). The command in question is p
, which prints the current line. So, this means "print all lines from the one matching /dog 123 4335/
until the end."
3
That prints thedog
line though which is not wanted here.
– Stéphane Chazelas
Jun 10 '15 at 9:49
1
This looks like the best answer (and works for my own case) but would need to be adapted to skip the matched line as well.
– Pavel Šimerda
Mar 14 '16 at 7:17
1
sed -n '/dog 123 4335/,$p' | sed '1d' will remove the dog line
– Kemin Zhou
Jul 2 '18 at 5:26
1
sed -n '/dog 123 4335/,$p' | tail -n +2
will remove the match as well
– gilad mayani
Oct 24 '18 at 13:01
add a comment |
In practice I'd probably use Aet3miirah's answer most of the time and alexey's answer is wonderful when wanting to navigate through the lines (also, it also works with less
). OTOH, I really like another approach (which is kind of the reversed Gilles' answer:
sed -n '/dog 123 4335/,$p'
When called with the -n
flag, sed
does not print by default the lines it processes any more. Then we use a 2-address form that says to apply a command from the line matching /dog 123 4335/
until the end of the file (represented by $
). The command in question is p
, which prints the current line. So, this means "print all lines from the one matching /dog 123 4335/
until the end."
3
That prints thedog
line though which is not wanted here.
– Stéphane Chazelas
Jun 10 '15 at 9:49
1
This looks like the best answer (and works for my own case) but would need to be adapted to skip the matched line as well.
– Pavel Šimerda
Mar 14 '16 at 7:17
1
sed -n '/dog 123 4335/,$p' | sed '1d' will remove the dog line
– Kemin Zhou
Jul 2 '18 at 5:26
1
sed -n '/dog 123 4335/,$p' | tail -n +2
will remove the match as well
– gilad mayani
Oct 24 '18 at 13:01
add a comment |
In practice I'd probably use Aet3miirah's answer most of the time and alexey's answer is wonderful when wanting to navigate through the lines (also, it also works with less
). OTOH, I really like another approach (which is kind of the reversed Gilles' answer:
sed -n '/dog 123 4335/,$p'
When called with the -n
flag, sed
does not print by default the lines it processes any more. Then we use a 2-address form that says to apply a command from the line matching /dog 123 4335/
until the end of the file (represented by $
). The command in question is p
, which prints the current line. So, this means "print all lines from the one matching /dog 123 4335/
until the end."
In practice I'd probably use Aet3miirah's answer most of the time and alexey's answer is wonderful when wanting to navigate through the lines (also, it also works with less
). OTOH, I really like another approach (which is kind of the reversed Gilles' answer:
sed -n '/dog 123 4335/,$p'
When called with the -n
flag, sed
does not print by default the lines it processes any more. Then we use a 2-address form that says to apply a command from the line matching /dog 123 4335/
until the end of the file (represented by $
). The command in question is p
, which prints the current line. So, this means "print all lines from the one matching /dog 123 4335/
until the end."
edited Apr 13 '17 at 12:37
Community♦
1
1
answered May 26 '15 at 13:49
brandizzibrandizzi
1,39411425
1,39411425
3
That prints thedog
line though which is not wanted here.
– Stéphane Chazelas
Jun 10 '15 at 9:49
1
This looks like the best answer (and works for my own case) but would need to be adapted to skip the matched line as well.
– Pavel Šimerda
Mar 14 '16 at 7:17
1
sed -n '/dog 123 4335/,$p' | sed '1d' will remove the dog line
– Kemin Zhou
Jul 2 '18 at 5:26
1
sed -n '/dog 123 4335/,$p' | tail -n +2
will remove the match as well
– gilad mayani
Oct 24 '18 at 13:01
add a comment |
3
That prints thedog
line though which is not wanted here.
– Stéphane Chazelas
Jun 10 '15 at 9:49
1
This looks like the best answer (and works for my own case) but would need to be adapted to skip the matched line as well.
– Pavel Šimerda
Mar 14 '16 at 7:17
1
sed -n '/dog 123 4335/,$p' | sed '1d' will remove the dog line
– Kemin Zhou
Jul 2 '18 at 5:26
1
sed -n '/dog 123 4335/,$p' | tail -n +2
will remove the match as well
– gilad mayani
Oct 24 '18 at 13:01
3
3
That prints the
dog
line though which is not wanted here.– Stéphane Chazelas
Jun 10 '15 at 9:49
That prints the
dog
line though which is not wanted here.– Stéphane Chazelas
Jun 10 '15 at 9:49
1
1
This looks like the best answer (and works for my own case) but would need to be adapted to skip the matched line as well.
– Pavel Šimerda
Mar 14 '16 at 7:17
This looks like the best answer (and works for my own case) but would need to be adapted to skip the matched line as well.
– Pavel Šimerda
Mar 14 '16 at 7:17
1
1
sed -n '/dog 123 4335/,$p' | sed '1d' will remove the dog line
– Kemin Zhou
Jul 2 '18 at 5:26
sed -n '/dog 123 4335/,$p' | sed '1d' will remove the dog line
– Kemin Zhou
Jul 2 '18 at 5:26
1
1
sed -n '/dog 123 4335/,$p' | tail -n +2
will remove the match as well– gilad mayani
Oct 24 '18 at 13:01
sed -n '/dog 123 4335/,$p' | tail -n +2
will remove the match as well– gilad mayani
Oct 24 '18 at 13:01
add a comment |
sed -e '1,/dog 123 4335/d' file1
If you need to read the pattern from a file, substitute it into the sed command. If the file contains a sed pattern:
sed -e "1,/$(cat file2)/d" file1
If the file contains a literal string to look for, quote all special characters. I assume the file contains a single line.
sed -e "1,/$(sed 's/[\/^$.*]/\&/g' file2)/d" file1
If you want the match to be the whole line, not just a substring, wrap the pattern in ^…$
.
sed -e "1,/^$(sed 's/[\/^$.*]/\&/g' file2)$/d" file1
6
That won't work if the pattern is on the first line. GNUsed
has0,/dog.../d
for that.
– Stéphane Chazelas
Jun 10 '15 at 9:48
add a comment |
sed -e '1,/dog 123 4335/d' file1
If you need to read the pattern from a file, substitute it into the sed command. If the file contains a sed pattern:
sed -e "1,/$(cat file2)/d" file1
If the file contains a literal string to look for, quote all special characters. I assume the file contains a single line.
sed -e "1,/$(sed 's/[\/^$.*]/\&/g' file2)/d" file1
If you want the match to be the whole line, not just a substring, wrap the pattern in ^…$
.
sed -e "1,/^$(sed 's/[\/^$.*]/\&/g' file2)$/d" file1
6
That won't work if the pattern is on the first line. GNUsed
has0,/dog.../d
for that.
– Stéphane Chazelas
Jun 10 '15 at 9:48
add a comment |
sed -e '1,/dog 123 4335/d' file1
If you need to read the pattern from a file, substitute it into the sed command. If the file contains a sed pattern:
sed -e "1,/$(cat file2)/d" file1
If the file contains a literal string to look for, quote all special characters. I assume the file contains a single line.
sed -e "1,/$(sed 's/[\/^$.*]/\&/g' file2)/d" file1
If you want the match to be the whole line, not just a substring, wrap the pattern in ^…$
.
sed -e "1,/^$(sed 's/[\/^$.*]/\&/g' file2)$/d" file1
sed -e '1,/dog 123 4335/d' file1
If you need to read the pattern from a file, substitute it into the sed command. If the file contains a sed pattern:
sed -e "1,/$(cat file2)/d" file1
If the file contains a literal string to look for, quote all special characters. I assume the file contains a single line.
sed -e "1,/$(sed 's/[\/^$.*]/\&/g' file2)/d" file1
If you want the match to be the whole line, not just a substring, wrap the pattern in ^…$
.
sed -e "1,/^$(sed 's/[\/^$.*]/\&/g' file2)$/d" file1
answered Nov 23 '12 at 23:55
GillesGilles
541k12810941610
541k12810941610
6
That won't work if the pattern is on the first line. GNUsed
has0,/dog.../d
for that.
– Stéphane Chazelas
Jun 10 '15 at 9:48
add a comment |
6
That won't work if the pattern is on the first line. GNUsed
has0,/dog.../d
for that.
– Stéphane Chazelas
Jun 10 '15 at 9:48
6
6
That won't work if the pattern is on the first line. GNU
sed
has 0,/dog.../d
for that.– Stéphane Chazelas
Jun 10 '15 at 9:48
That won't work if the pattern is on the first line. GNU
sed
has 0,/dog.../d
for that.– Stéphane Chazelas
Jun 10 '15 at 9:48
add a comment |
$ more +/"dog 123 4335" file1
4
It also works withless
.
– brandizzi
May 26 '15 at 13:40
3
clever on the terminal, but it doesn't actually work if you pipe it into something else liketac
.
– jcomeau_ictx
Jul 2 '15 at 6:25
i am using it like this , $ more +/"match my words" file1 >> file2
– AMB
Jul 7 '15 at 17:38
1
Maybe+
was replaced by-p
in POSIX 7: pubs.opengroup.org/onlinepubs/9699919799/utilities/more.html but not yet implemented in util-linux 2.20.1. And this also printsskipping..
and some extra newlines (to stderr I expect, so might be fine).
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Sep 14 '16 at 12:41
@jcomeau_ictx: What do you mean? It works fine here
– Thor
Nov 3 '17 at 9:57
|
show 1 more comment
$ more +/"dog 123 4335" file1
4
It also works withless
.
– brandizzi
May 26 '15 at 13:40
3
clever on the terminal, but it doesn't actually work if you pipe it into something else liketac
.
– jcomeau_ictx
Jul 2 '15 at 6:25
i am using it like this , $ more +/"match my words" file1 >> file2
– AMB
Jul 7 '15 at 17:38
1
Maybe+
was replaced by-p
in POSIX 7: pubs.opengroup.org/onlinepubs/9699919799/utilities/more.html but not yet implemented in util-linux 2.20.1. And this also printsskipping..
and some extra newlines (to stderr I expect, so might be fine).
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Sep 14 '16 at 12:41
@jcomeau_ictx: What do you mean? It works fine here
– Thor
Nov 3 '17 at 9:57
|
show 1 more comment
$ more +/"dog 123 4335" file1
$ more +/"dog 123 4335" file1
answered May 30 '13 at 13:27
alexeyalexey
14112
14112
4
It also works withless
.
– brandizzi
May 26 '15 at 13:40
3
clever on the terminal, but it doesn't actually work if you pipe it into something else liketac
.
– jcomeau_ictx
Jul 2 '15 at 6:25
i am using it like this , $ more +/"match my words" file1 >> file2
– AMB
Jul 7 '15 at 17:38
1
Maybe+
was replaced by-p
in POSIX 7: pubs.opengroup.org/onlinepubs/9699919799/utilities/more.html but not yet implemented in util-linux 2.20.1. And this also printsskipping..
and some extra newlines (to stderr I expect, so might be fine).
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Sep 14 '16 at 12:41
@jcomeau_ictx: What do you mean? It works fine here
– Thor
Nov 3 '17 at 9:57
|
show 1 more comment
4
It also works withless
.
– brandizzi
May 26 '15 at 13:40
3
clever on the terminal, but it doesn't actually work if you pipe it into something else liketac
.
– jcomeau_ictx
Jul 2 '15 at 6:25
i am using it like this , $ more +/"match my words" file1 >> file2
– AMB
Jul 7 '15 at 17:38
1
Maybe+
was replaced by-p
in POSIX 7: pubs.opengroup.org/onlinepubs/9699919799/utilities/more.html but not yet implemented in util-linux 2.20.1. And this also printsskipping..
and some extra newlines (to stderr I expect, so might be fine).
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Sep 14 '16 at 12:41
@jcomeau_ictx: What do you mean? It works fine here
– Thor
Nov 3 '17 at 9:57
4
4
It also works with
less
.– brandizzi
May 26 '15 at 13:40
It also works with
less
.– brandizzi
May 26 '15 at 13:40
3
3
clever on the terminal, but it doesn't actually work if you pipe it into something else like
tac
.– jcomeau_ictx
Jul 2 '15 at 6:25
clever on the terminal, but it doesn't actually work if you pipe it into something else like
tac
.– jcomeau_ictx
Jul 2 '15 at 6:25
i am using it like this , $ more +/"match my words" file1 >> file2
– AMB
Jul 7 '15 at 17:38
i am using it like this , $ more +/"match my words" file1 >> file2
– AMB
Jul 7 '15 at 17:38
1
1
Maybe
+
was replaced by -p
in POSIX 7: pubs.opengroup.org/onlinepubs/9699919799/utilities/more.html but not yet implemented in util-linux 2.20.1. And this also prints skipping..
and some extra newlines (to stderr I expect, so might be fine).– Ciro Santilli 新疆改造中心 六四事件 法轮功
Sep 14 '16 at 12:41
Maybe
+
was replaced by -p
in POSIX 7: pubs.opengroup.org/onlinepubs/9699919799/utilities/more.html but not yet implemented in util-linux 2.20.1. And this also prints skipping..
and some extra newlines (to stderr I expect, so might be fine).– Ciro Santilli 新疆改造中心 六四事件 法轮功
Sep 14 '16 at 12:41
@jcomeau_ictx: What do you mean? It works fine here
– Thor
Nov 3 '17 at 9:57
@jcomeau_ictx: What do you mean? It works fine here
– Thor
Nov 3 '17 at 9:57
|
show 1 more comment
With awk
:
awk 'BEGIN {getline pattern < "other file"}
NR == 1, $0 ~ pattern {next}; {print}' < "input file"
add a comment |
With awk
:
awk 'BEGIN {getline pattern < "other file"}
NR == 1, $0 ~ pattern {next}; {print}' < "input file"
add a comment |
With awk
:
awk 'BEGIN {getline pattern < "other file"}
NR == 1, $0 ~ pattern {next}; {print}' < "input file"
With awk
:
awk 'BEGIN {getline pattern < "other file"}
NR == 1, $0 ~ pattern {next}; {print}' < "input file"
edited Jun 10 '15 at 9:58
answered Jun 10 '15 at 9:51
Stéphane ChazelasStéphane Chazelas
309k57582942
309k57582942
add a comment |
add a comment |
If the input is an lseekable regular file:
With GNU grep
:
{ grep -xFm1 'dog 123 4335' >&2
cat; } <infile 2>/dev/null >outfile
With sed
:
{ sed -n '/^dog 123 4335$/q'
cat; } <infile >outfile
A GNU grep
called w/ the -m
option will quit input at the match - and it will leave its (lseekable) input fd immediately after the point it found its last match. So calling grep
w/ -m1
finds the first occurrence of a pattern in a file, and leaves the input offset at precisely the right place for cat
to write everything following the pattern's first match in a file to stdout.
Even without a GNU grep
you can do the exact same thing w/ a POSIX compatible sed
- when sed
q
uits it is specified to leave its input offset right where it does. GNU sed
is not standards-compliant in this way, though, and so the above will likely not work w/ a GNU sed
unless you call it with its -u
switch.
note, thesed
stream sharing demonstrated here is not specially (though, yes, the standard referenced does specifically examplesed
as a utility thus capable) of the free-form and conditionally cooperative workflow shown. notably, all standard utilities are meant and specified to thus cooperate and share cursor positions of input streams without failing the next reader any processing at all.grep -q
should do this; quietlygrep
should return as soon as any match in input is found, and any remaining of input should not, by standard, be consumed by default.
– mikeserv
Oct 29 '18 at 10:43
add a comment |
If the input is an lseekable regular file:
With GNU grep
:
{ grep -xFm1 'dog 123 4335' >&2
cat; } <infile 2>/dev/null >outfile
With sed
:
{ sed -n '/^dog 123 4335$/q'
cat; } <infile >outfile
A GNU grep
called w/ the -m
option will quit input at the match - and it will leave its (lseekable) input fd immediately after the point it found its last match. So calling grep
w/ -m1
finds the first occurrence of a pattern in a file, and leaves the input offset at precisely the right place for cat
to write everything following the pattern's first match in a file to stdout.
Even without a GNU grep
you can do the exact same thing w/ a POSIX compatible sed
- when sed
q
uits it is specified to leave its input offset right where it does. GNU sed
is not standards-compliant in this way, though, and so the above will likely not work w/ a GNU sed
unless you call it with its -u
switch.
note, thesed
stream sharing demonstrated here is not specially (though, yes, the standard referenced does specifically examplesed
as a utility thus capable) of the free-form and conditionally cooperative workflow shown. notably, all standard utilities are meant and specified to thus cooperate and share cursor positions of input streams without failing the next reader any processing at all.grep -q
should do this; quietlygrep
should return as soon as any match in input is found, and any remaining of input should not, by standard, be consumed by default.
– mikeserv
Oct 29 '18 at 10:43
add a comment |
If the input is an lseekable regular file:
With GNU grep
:
{ grep -xFm1 'dog 123 4335' >&2
cat; } <infile 2>/dev/null >outfile
With sed
:
{ sed -n '/^dog 123 4335$/q'
cat; } <infile >outfile
A GNU grep
called w/ the -m
option will quit input at the match - and it will leave its (lseekable) input fd immediately after the point it found its last match. So calling grep
w/ -m1
finds the first occurrence of a pattern in a file, and leaves the input offset at precisely the right place for cat
to write everything following the pattern's first match in a file to stdout.
Even without a GNU grep
you can do the exact same thing w/ a POSIX compatible sed
- when sed
q
uits it is specified to leave its input offset right where it does. GNU sed
is not standards-compliant in this way, though, and so the above will likely not work w/ a GNU sed
unless you call it with its -u
switch.
If the input is an lseekable regular file:
With GNU grep
:
{ grep -xFm1 'dog 123 4335' >&2
cat; } <infile 2>/dev/null >outfile
With sed
:
{ sed -n '/^dog 123 4335$/q'
cat; } <infile >outfile
A GNU grep
called w/ the -m
option will quit input at the match - and it will leave its (lseekable) input fd immediately after the point it found its last match. So calling grep
w/ -m1
finds the first occurrence of a pattern in a file, and leaves the input offset at precisely the right place for cat
to write everything following the pattern's first match in a file to stdout.
Even without a GNU grep
you can do the exact same thing w/ a POSIX compatible sed
- when sed
q
uits it is specified to leave its input offset right where it does. GNU sed
is not standards-compliant in this way, though, and so the above will likely not work w/ a GNU sed
unless you call it with its -u
switch.
edited Jun 10 '15 at 21:34
answered Jun 10 '15 at 11:25
mikeservmikeserv
45.8k668160
45.8k668160
note, thesed
stream sharing demonstrated here is not specially (though, yes, the standard referenced does specifically examplesed
as a utility thus capable) of the free-form and conditionally cooperative workflow shown. notably, all standard utilities are meant and specified to thus cooperate and share cursor positions of input streams without failing the next reader any processing at all.grep -q
should do this; quietlygrep
should return as soon as any match in input is found, and any remaining of input should not, by standard, be consumed by default.
– mikeserv
Oct 29 '18 at 10:43
add a comment |
note, thesed
stream sharing demonstrated here is not specially (though, yes, the standard referenced does specifically examplesed
as a utility thus capable) of the free-form and conditionally cooperative workflow shown. notably, all standard utilities are meant and specified to thus cooperate and share cursor positions of input streams without failing the next reader any processing at all.grep -q
should do this; quietlygrep
should return as soon as any match in input is found, and any remaining of input should not, by standard, be consumed by default.
– mikeserv
Oct 29 '18 at 10:43
note, the
sed
stream sharing demonstrated here is not specially (though, yes, the standard referenced does specifically example sed
as a utility thus capable) of the free-form and conditionally cooperative workflow shown. notably, all standard utilities are meant and specified to thus cooperate and share cursor positions of input streams without failing the next reader any processing at all. grep -q
should do this; quietly grep
should return as soon as any match in input is found, and any remaining of input should not, by standard, be consumed by default.– mikeserv
Oct 29 '18 at 10:43
note, the
sed
stream sharing demonstrated here is not specially (though, yes, the standard referenced does specifically example sed
as a utility thus capable) of the free-form and conditionally cooperative workflow shown. notably, all standard utilities are meant and specified to thus cooperate and share cursor positions of input streams without failing the next reader any processing at all. grep -q
should do this; quietly grep
should return as soon as any match in input is found, and any remaining of input should not, by standard, be consumed by default.– mikeserv
Oct 29 '18 at 10:43
add a comment |
One way using awk:
awk 'NR==FNR{a[$0];next}f;($0 in a){f=1}' file2 file1
where file2 contains your search patterns. First, all the contents of file2 are stored in the array "a". When the file1 is processed, every line is checked against the array, and printed only if is not present.
I think the OP wants to output every line following the pattern.
– Thor
Nov 23 '12 at 11:41
@Thor : thanks for pointing it out, updated it now...
– Guru
Nov 23 '12 at 11:57
Nicely done :).
– Thor
Nov 23 '12 at 13:04
add a comment |
One way using awk:
awk 'NR==FNR{a[$0];next}f;($0 in a){f=1}' file2 file1
where file2 contains your search patterns. First, all the contents of file2 are stored in the array "a". When the file1 is processed, every line is checked against the array, and printed only if is not present.
I think the OP wants to output every line following the pattern.
– Thor
Nov 23 '12 at 11:41
@Thor : thanks for pointing it out, updated it now...
– Guru
Nov 23 '12 at 11:57
Nicely done :).
– Thor
Nov 23 '12 at 13:04
add a comment |
One way using awk:
awk 'NR==FNR{a[$0];next}f;($0 in a){f=1}' file2 file1
where file2 contains your search patterns. First, all the contents of file2 are stored in the array "a". When the file1 is processed, every line is checked against the array, and printed only if is not present.
One way using awk:
awk 'NR==FNR{a[$0];next}f;($0 in a){f=1}' file2 file1
where file2 contains your search patterns. First, all the contents of file2 are stored in the array "a". When the file1 is processed, every line is checked against the array, and printed only if is not present.
edited Nov 23 '12 at 11:56
answered Nov 23 '12 at 7:37
GuruGuru
4,15211216
4,15211216
I think the OP wants to output every line following the pattern.
– Thor
Nov 23 '12 at 11:41
@Thor : thanks for pointing it out, updated it now...
– Guru
Nov 23 '12 at 11:57
Nicely done :).
– Thor
Nov 23 '12 at 13:04
add a comment |
I think the OP wants to output every line following the pattern.
– Thor
Nov 23 '12 at 11:41
@Thor : thanks for pointing it out, updated it now...
– Guru
Nov 23 '12 at 11:57
Nicely done :).
– Thor
Nov 23 '12 at 13:04
I think the OP wants to output every line following the pattern.
– Thor
Nov 23 '12 at 11:41
I think the OP wants to output every line following the pattern.
– Thor
Nov 23 '12 at 11:41
@Thor : thanks for pointing it out, updated it now...
– Guru
Nov 23 '12 at 11:57
@Thor : thanks for pointing it out, updated it now...
– Guru
Nov 23 '12 at 11:57
Nicely done :).
– Thor
Nov 23 '12 at 13:04
Nicely done :).
– Thor
Nov 23 '12 at 13:04
add a comment |
My answer for the question in the subject, without storing pattern in a second file.
Here is my test file:
$ cat animals.txt
cat 13123 23424
deer 2131 213132
bear 2313 21313
dog 123 4335
cat 13123 23424
deer 2131 213132
bear 2313 21313
GNU sed:
$ sed '0,/^dog 123 4335$/d' animals.txt
cat 13123 23424
deer 2131 213132
bear 2313 21313
Perl:
$ perl -ne 'print unless 1.../^dog 123 4335$/' animals.txt
cat 13123 23424
deer 2131 213132
bear 2313 21313
Perl variant with pattern in a file:
$ cat pattern.txt
dog 123 4335
$ perl -ne 'BEGIN{chomp($p=(<STDIN>)[0])};print unless 1../$p/;' animals.txt < pattern.txt
cat 13123 23424
deer 2131 213132
bear 2313 21313
add a comment |
My answer for the question in the subject, without storing pattern in a second file.
Here is my test file:
$ cat animals.txt
cat 13123 23424
deer 2131 213132
bear 2313 21313
dog 123 4335
cat 13123 23424
deer 2131 213132
bear 2313 21313
GNU sed:
$ sed '0,/^dog 123 4335$/d' animals.txt
cat 13123 23424
deer 2131 213132
bear 2313 21313
Perl:
$ perl -ne 'print unless 1.../^dog 123 4335$/' animals.txt
cat 13123 23424
deer 2131 213132
bear 2313 21313
Perl variant with pattern in a file:
$ cat pattern.txt
dog 123 4335
$ perl -ne 'BEGIN{chomp($p=(<STDIN>)[0])};print unless 1../$p/;' animals.txt < pattern.txt
cat 13123 23424
deer 2131 213132
bear 2313 21313
add a comment |
My answer for the question in the subject, without storing pattern in a second file.
Here is my test file:
$ cat animals.txt
cat 13123 23424
deer 2131 213132
bear 2313 21313
dog 123 4335
cat 13123 23424
deer 2131 213132
bear 2313 21313
GNU sed:
$ sed '0,/^dog 123 4335$/d' animals.txt
cat 13123 23424
deer 2131 213132
bear 2313 21313
Perl:
$ perl -ne 'print unless 1.../^dog 123 4335$/' animals.txt
cat 13123 23424
deer 2131 213132
bear 2313 21313
Perl variant with pattern in a file:
$ cat pattern.txt
dog 123 4335
$ perl -ne 'BEGIN{chomp($p=(<STDIN>)[0])};print unless 1../$p/;' animals.txt < pattern.txt
cat 13123 23424
deer 2131 213132
bear 2313 21313
My answer for the question in the subject, without storing pattern in a second file.
Here is my test file:
$ cat animals.txt
cat 13123 23424
deer 2131 213132
bear 2313 21313
dog 123 4335
cat 13123 23424
deer 2131 213132
bear 2313 21313
GNU sed:
$ sed '0,/^dog 123 4335$/d' animals.txt
cat 13123 23424
deer 2131 213132
bear 2313 21313
Perl:
$ perl -ne 'print unless 1.../^dog 123 4335$/' animals.txt
cat 13123 23424
deer 2131 213132
bear 2313 21313
Perl variant with pattern in a file:
$ cat pattern.txt
dog 123 4335
$ perl -ne 'BEGIN{chomp($p=(<STDIN>)[0])};print unless 1../$p/;' animals.txt < pattern.txt
cat 13123 23424
deer 2131 213132
bear 2313 21313
answered Oct 9 '14 at 11:49
jbgoodjbgood
411
411
add a comment |
add a comment |
How to print all lines after a match up to the end of the file?
Another way to put it is "how to delete all lines from the 1st one till the match (including)", and this can be sed
-written as:
sed -e '1,/MATCH PATTERN/d'
1
The only problem is when the pattern is on the first line...
– don_crissti
Sep 24 '15 at 20:45
1
Is this different from unix.stackexchange.com/a/56517/32558 ?
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Sep 14 '16 at 12:42
I guess we need a committee here to decide.
– poige
Sep 14 '16 at 14:02
1
@poige: nah, you provide the same answer less comprehensively
– Thor
Nov 3 '17 at 10:03
@don_crissti, what aboutsed -e '0,/MATCH PATTERN/d'
then?
– Velkan
Dec 5 '17 at 11:02
|
show 1 more comment
How to print all lines after a match up to the end of the file?
Another way to put it is "how to delete all lines from the 1st one till the match (including)", and this can be sed
-written as:
sed -e '1,/MATCH PATTERN/d'
1
The only problem is when the pattern is on the first line...
– don_crissti
Sep 24 '15 at 20:45
1
Is this different from unix.stackexchange.com/a/56517/32558 ?
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Sep 14 '16 at 12:42
I guess we need a committee here to decide.
– poige
Sep 14 '16 at 14:02
1
@poige: nah, you provide the same answer less comprehensively
– Thor
Nov 3 '17 at 10:03
@don_crissti, what aboutsed -e '0,/MATCH PATTERN/d'
then?
– Velkan
Dec 5 '17 at 11:02
|
show 1 more comment
How to print all lines after a match up to the end of the file?
Another way to put it is "how to delete all lines from the 1st one till the match (including)", and this can be sed
-written as:
sed -e '1,/MATCH PATTERN/d'
How to print all lines after a match up to the end of the file?
Another way to put it is "how to delete all lines from the 1st one till the match (including)", and this can be sed
-written as:
sed -e '1,/MATCH PATTERN/d'
answered Sep 24 '15 at 20:17
poigepoige
4,1571644
4,1571644
1
The only problem is when the pattern is on the first line...
– don_crissti
Sep 24 '15 at 20:45
1
Is this different from unix.stackexchange.com/a/56517/32558 ?
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Sep 14 '16 at 12:42
I guess we need a committee here to decide.
– poige
Sep 14 '16 at 14:02
1
@poige: nah, you provide the same answer less comprehensively
– Thor
Nov 3 '17 at 10:03
@don_crissti, what aboutsed -e '0,/MATCH PATTERN/d'
then?
– Velkan
Dec 5 '17 at 11:02
|
show 1 more comment
1
The only problem is when the pattern is on the first line...
– don_crissti
Sep 24 '15 at 20:45
1
Is this different from unix.stackexchange.com/a/56517/32558 ?
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Sep 14 '16 at 12:42
I guess we need a committee here to decide.
– poige
Sep 14 '16 at 14:02
1
@poige: nah, you provide the same answer less comprehensively
– Thor
Nov 3 '17 at 10:03
@don_crissti, what aboutsed -e '0,/MATCH PATTERN/d'
then?
– Velkan
Dec 5 '17 at 11:02
1
1
The only problem is when the pattern is on the first line...
– don_crissti
Sep 24 '15 at 20:45
The only problem is when the pattern is on the first line...
– don_crissti
Sep 24 '15 at 20:45
1
1
Is this different from unix.stackexchange.com/a/56517/32558 ?
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Sep 14 '16 at 12:42
Is this different from unix.stackexchange.com/a/56517/32558 ?
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Sep 14 '16 at 12:42
I guess we need a committee here to decide.
– poige
Sep 14 '16 at 14:02
I guess we need a committee here to decide.
– poige
Sep 14 '16 at 14:02
1
1
@poige: nah, you provide the same answer less comprehensively
– Thor
Nov 3 '17 at 10:03
@poige: nah, you provide the same answer less comprehensively
– Thor
Nov 3 '17 at 10:03
@don_crissti, what about
sed -e '0,/MATCH PATTERN/d'
then?– Velkan
Dec 5 '17 at 11:02
@don_crissti, what about
sed -e '0,/MATCH PATTERN/d'
then?– Velkan
Dec 5 '17 at 11:02
|
show 1 more comment
Wth ed
:
ed -s file1 <<< $'/dog 123 4335/+1,$p'
This sends one p
rint command to ed in a here-string; the print command is limited in range to one after (+1
) the dog 123 4335
match until the end of the file ($
).
add a comment |
Wth ed
:
ed -s file1 <<< $'/dog 123 4335/+1,$p'
This sends one p
rint command to ed in a here-string; the print command is limited in range to one after (+1
) the dog 123 4335
match until the end of the file ($
).
add a comment |
Wth ed
:
ed -s file1 <<< $'/dog 123 4335/+1,$p'
This sends one p
rint command to ed in a here-string; the print command is limited in range to one after (+1
) the dog 123 4335
match until the end of the file ($
).
Wth ed
:
ed -s file1 <<< $'/dog 123 4335/+1,$p'
This sends one p
rint command to ed in a here-string; the print command is limited in range to one after (+1
) the dog 123 4335
match until the end of the file ($
).
answered 6 hours ago
Jeff SchallerJeff Schaller
43.2k1159138
43.2k1159138
add a comment |
add a comment |
Since awk isn't expressly disallowed, here's my offering assuming 'cat' is the match.
awk '$0 ~ /cat/ { vart = NR }{ arr[NR]=$0 } END { for (i = vart; i<=NR ; i++) print arr[i] }' animals.txt
add a comment |
Since awk isn't expressly disallowed, here's my offering assuming 'cat' is the match.
awk '$0 ~ /cat/ { vart = NR }{ arr[NR]=$0 } END { for (i = vart; i<=NR ; i++) print arr[i] }' animals.txt
add a comment |
Since awk isn't expressly disallowed, here's my offering assuming 'cat' is the match.
awk '$0 ~ /cat/ { vart = NR }{ arr[NR]=$0 } END { for (i = vart; i<=NR ; i++) print arr[i] }' animals.txt
Since awk isn't expressly disallowed, here's my offering assuming 'cat' is the match.
awk '$0 ~ /cat/ { vart = NR }{ arr[NR]=$0 } END { for (i = vart; i<=NR ; i++) print arr[i] }' animals.txt
answered Nov 23 '12 at 10:25
TomTom
427
427
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%2f56429%2fhow-to-print-all-lines-after-a-match-up-to-the-end-of-the-file%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
Can other file contain just a single pattern to search for, or one per line, and start searching at whichever line is found first in the searched file?
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Sep 14 '16 at 12:52