I need to find a string and need to print the line above it.
case1: The same line won't have the more than one matching pattern
ie)
consider a file containing
$cat > para
returns between the paragaraphs
italic or bold
quotes by placing
italic
Here i need to find for italic, and i need to get the output as below
returns between the paragaraphs quotes by placing
How can i get the output like this?
Best Answer
If the pattern cannot occur on consecutive lines you can simply run
I've explained here how the
N
;P
;D
cycle works. The difference is that here the first line in the pattern space is printed only if the second one matches, otherwise it's deleted.If the pattern can occur on consecutive lines the above solution will print a line that matches if it's followed by another line that matches.
To ignore consecutive matches add a second condition to print the first line in the pattern space only if it doesn't match:
Another way, using the hold buffer.
If you want to ignore consecutive matches:
or, in one line
If you don't want to ignore consecutive matches:
or, in one line
Though keep in mind the solutions that use the hold buffer will print a leading empty line if the first line in your file matches.