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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communitySed 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
sed '/PATTERN/Q' FILE
will skip the matched line. Q
is a GNU extension, so it won't work with any sed.
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.
Apr 6, 2015 at 13:36
!
makes p
apply to lines not matching pattern
, but then the //q
confuses me...
//
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
?
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
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.
Dec 12, 2017 at 20:27
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.
Dec 14, 2017 at 19:02
.*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
.
.*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.
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.
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'
??
Oct 14, 2016 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.)
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.
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
.