grep for a string in a line if the previous line doesn't contain a specific string


I have the following lines in a file:

abcdef ghi jkl uvw xyz

I want to grep for the string "xyz" if the previous line is not contains the string "jkl".

I know how to grep for a string if the line doesn't contains a specific string using -v option. But i don't know how to do this with different lines.


grep is really a line-oriented tool. It might be possible to achieve what you want with it, but it's easier to use Awk:

awk ' /xyz/ && !skip { print } { skip = /jkl/ } ' file

Read as: for every line, do

<ul><li>if the current line matches xyz and we haven't just seen jkl, print it;</li> <li>set the variable skip to indicate whether we've just seen jkl.</li> </ul>


<pre class="lang-sh prettyprint-override">sed '/jkl/{N;d}; /xyz/!d' <ul><li>If find jkl, remove that line and next</li> <li>print only remaining lines with xyz</li> </ul>


I think you're better off using an actual programming language, even a simple one like Bash or AWK or sed. For example, using Bash:

( previous_line_matched= while IFS= read -r line ; do if [[ ! "$previous_line_matched" && "$line" == *xyz* ]] ; then echo "$line" fi if [[ "$line" == *jkl* ]] ; then previous_line_matched=1 else previous_line_matched= fi done < input_file )

Or, more tersely, using Perl:

perl -ne 'print if m/xyz/ && ! $skip; $skip = m/jkl/' < input_file


  • Assign a variable and check existence of hash key
  • Limiting file upload type
  • Can one compare java.lang.Class objects with == in Java? [duplicate]
  • Check that all values in a range are identical
  • Simple way to upload image with node.js and express?
  • Displaying text from a file in an Inno installer with new lines
  • python recursive directory reading
  • What are the matlab equivalent for '\\n' '\\t' and other escape characters?
  • BASH - Shuffle characters in strings from file
  • Using grep to read a log for pattern1 in the file and print only lines containing pattern1. Stop sea
  • How can I find the Size of some specified files?
  • function declaration within function declaration with same name javascript
  • Alternate (Free) to AOP PostSharp for method tracing (and exception)
  • Find by regex and replace match to lowercase in Bash
  • Exception handling as per java coding standards
  • Remove characters after a specific character in column
  • Creating a layer of gradient within an SVG path dynamically
  • Whats the right place for testhelper-classes? (phpunit/best practise)
  • SQL query to group by maximal sets of a column having inner consecutive distances below a threshold
  • EntLib Way to Bind “Null” Value to Parameter
  • Compare struct to a constant in C
  • Click on button in another program - FindWindow, C#
  • VSCode change debug shell to bash on windows
  • Does Apple allow the usage of sysctl.h within iOS applications?
  • Django model inheritance, filtering models
  • Get specific string
  • Calling Worksheet functions from vba in foreign language versions of Excel
  • How do I access an unhandled exception in an MVC Error view?
  • Moving mysql files across servers
  • Google Custom Search with transparent background
  • Checking free space on FTP server
  • Is possible to count alias result on mysql
  • vba code to select only visible cells in specific column except heading
  • Transpose CSV data with awk (pivot transformation)
  • PHP: When would you need the self:: keyword?
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • reshape alternating columns in less time and using less memory
  • To Get the radio button value in ruby on rails
  • How can I use threading to 'tick' a timer to be accessed by other threads?