grep --before-context 5
shows 5 lines before the match.
I want to show everything before the match.
Doing grep --before-context 99999999
would work but it is not very... professional.
How to show all the file up to the match?
grep --before-context 5
shows 5 lines before the match.
I want to show everything before the match.
Doing grep --before-context 99999999
would work but it is not very... professional.
How to show all the file up to the match?
Sed is better for that.
Just do:
sed '/PATTERN/q' FILE
It works like this:
For each line, we look if it matches /PATTERN
:
This is the most efficient solution, because as soon as it sees PATTERN
, it quits. Without q
, sed would continue to read the rest of the file, and do nothing with it. For big files it can make a difference.
This trick can also be used to emulate head
:
sed 10q FILE
print up to and including the match:
awk '{print} /pattern/ {exit}' filename
sed '/pattern/q' filename
print up to BUT NOT including the match:
awk '/pattern/ {exit} {print}' filename
sed '/pattern/Q' filename
Q
's cool but gnu specific afaik, sed -n '/pattern/!p;//q'
would be more portable.
– don_crissti
Apr 06 '15 at 13:36
!
makes p
apply to lines not matching pattern
, but then the //q
confuses me...
– jwd
Aug 22 '19 at 01:25
//
means "previous regular expression" (I thought it meant "match the empty string"). I think a shorter version of the same solution is: sed -n '/pattern/q;p
?
– jwd
Aug 22 '19 at 01:39
sed
can replace most of grep's functionality.
sed -n '1,/<pattern>/ p' <file>
This means print from the first line until pattern is matched.
A couple of range examples
sed -n '/<pattern>/,$ p' <file> # from pattern to end of file
sed -n '/<pattern1>/,/<pattern2>/ p' <file> # from pattern1 to pattern2
For people who choose to remember only the basic tools in day to day work, and willing to accept less elegant and less efficient solutions:
head -n $(grep -n pattern filename | cut -d: -f1) filename
If this command is for a script then I will look for more elegant (and possibly efficient) solutions. If this is a one time command or a throw away script then I don't care.
Adding onto Mikel's answer above...
To print all lines up to, but not including, the first line in FILE
containing PATTERN
, try:
sed '/.*PATTERN.*/{s///;q;}' FILE
This matches the entire line containing the pattern, replaces it with a blank line, then quits without processing the rest of the file.
Post-script:
The easiest/clearest way I could think of to prevent printing an extra newline at the end (without involving another tool), was to run sed again and delete the new final line:
sed '/.*PATTERN.*/{s///;q;}' FILE | sed '$d'
... and since we're now deleting that line anyway, our previous work is redundant and we can simplify to:
sed '/PATTERN/q' FILE | sed '$d'
sed
invocation.
– don_crissti
Dec 12 '17 at 20:27
Since I was using this in both a tcsh
and a bash
alias, I needed to make sure I had a relatively concise one-line solution that worked in both standard and GNU sed
(for portability); all requirements that your contribution may very well have met.
As someone who uses sed
very rarely, my most important requirement was for something that I could quickly understand when I want to easily edit or re-purpose it years from now.
.*PATTERN.*
with grep or sed. Those tools look anywhere in the line anyway and this just makes things more complicate for the matching algorithm. In worst case it could be a lot slower than just PATTERN
.
– Thraidh
Oct 04 '21 at 15:12
.*PATTERN.*
for searching'. If you want to use it for purposes of replacement, then it is a acceptable. Anyway, if you want to replace a whole line which contains a specific pattern, it is probably better to use sed '/PATTERN/s/.*/new content/
. Anyway, if PATTERN
is simple, it does not matter. If it contains one or more .*
itself, you'll have to start to be careful.
– Thraidh
Oct 06 '21 at 12:25
The following pure GNU grep
methods are not efficient.
Search for everything up to the first instance of string "foo" in file bar, using three grep
s:
grep -m 1 -B $(grep -n -m 1 foo bar | grep -o '^[0-9]*') foo bar
Matching up to the last instance of "foo":
grep -oPz "(?s)[^\n]*${s}.*?\n.*?foo.*?\n" bar
Note: details on the last grep
can be found in: Regex (grep) for multi-line search needed.
grep
s (+ pcre) when it's a matter of simply running a single sed
invocation: sed 'x;/./G;//!x;/foo/p;//s/.*//;x;d'
??
– don_crissti
Oct 14 '16 at 13:02
sed
code seems worth its own answer, or could be added to one of the others. Re 7 grep
s: Because there was no grep
answer... (plus, the answer helps show why not.)
– agc
Oct 14 '16 at 18:21
You could also use one of the following
tac ./test | grep -B $(cat ./test | wc -l) -m 1 'pattern'|tac
or
tac ./test |head -n $(tac ./test | grep -n 'pattern' | cut -d: -f1 | head -n 1)|tac
or
tac ./test |sed ':a;N;$!ba;s/\n/'"pattern"'/g' | sed 's/'"patternpattern"'/\n/g'|head -n 1|sed 's/'"pattern"'/\n/g'|tac
The first option is very similar to what the OP suggested only it makes sure ti show enough lines before context by counting the lines in the file
The second option searches the line number of the first match (you could change that as well by changing the inner 'head') and than uses head on that number
The last option replaces all new lines with the match and than replaces two adjacent matches with a new line. The output of this is a line for every block of text between two matches. After that it uses 'head' to choose the first line (mwaning the block of text until the first match) match and than retranslates each match to a new line. this option works only if the file is in the following format
pattern
texttexttext
texttexttext texttexttext
texttexttexttexttexttexttexttexttext
pattern
texttexttext
pattern
texttexttext
texttexttexttexttexttexttexttexttext
and so forth
sed
command at the bottom is kind of gnarly.
– strugee
Jul 09 '15 at 19:45
ruby -pe 'exit if /PATTERN/' file(s)
sed
and alike is certainly preferable performance-wise. It can also be done as a Ruby one-liner with an order of magnitude performance penalty. This may be of interest if, in addition to stopping at a pattern, you want to do some processing that requires methods found in the standard Ruby library or a third-party Gem.
# Extract unique links from Markdown files
ruby -rcommonregex -ne \
'puts $<.map{CommonRegex.get_links(_1)}.uniq.sort;
exit if /PATTERN/' *.md
Aside from usefulness, its little known Ruby has adopted conventions found in awk
or perl
.
sed '/PATTERN/Q' FILE
will skip the matched line.Q
is a GNU extension, so it won't work with any sed. – Alex O Sep 08 '18 at 13:37