4

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?

  • I've tried to format this appropriately, but can you check that I haven't changed what you meant to say? You could probably trim this down a lot too, and make the title ask your question. You can edit it yourself as well. – Michael Homer Feb 20 '16 at 1:52
  • Actually i need to get the similar output for a file which contains more that 1000+ lines – jevan Feb 20 '16 at 2:27
  • Can you upload a longer sample? – Quora Feans Feb 20 '16 at 2:41
  • I don't think anybody cares whether any line might contain the pattern more than once.  The more interesting question is whether the pattern can appear on consecutive lines, and, if so, what output do you want? – G-Man Feb 20 '16 at 2:52
  • no the pattern won't occur in consecutive lines – jevan Feb 20 '16 at 3:07
4

If the pattern cannot occur on consecutive lines you can simply run

sed '$!N;/.*\n.*PATTERN.*/P;D' infile

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:

sed '$!N;/.*\n.*PATTERN.*/{/.*PATTERN.*\n.*/!P;};D' infile

Another way, using the hold buffer.
If you want to ignore consecutive matches:

sed '/PATTERN/!{              # if line doesn't match PATTERN
h                             # copy pattern space content over the hold buffer
d                             # delete pattern space
}
//{                           # if line matches PATTERN
x                             # exchange pattern space with hold space
//d                           # if line matches PATTERN delete it
}' infile

or, in one line

sed '/PATTERN/!{h;d;};//{x;//d;}' infile

If you don't want to ignore consecutive matches:

sed '/PATTERN/!{              # if line doesn't match PATTERN
h                             # copy pattern space content over the hold buffer
d                             # delete pattern space
}
//x                           # if line matches PATTERN exchange buffers
' infile 

or, in one line

sed '/PATTERN/!{h;d;};//x' infile

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.

2

Using grep, then sed:

grep --no-group-separator -B 1 "italic" <yourfilename> | sed -n 1~2p`

Explanation:

grep manual:

-B num
--before-context=num
Print num lines of leading context before matching lines

--no-group-separator
When -A, -B or -C are in use, do not print a separator between groups of lines.

sed:

Pick the first line of two. We could also put sed -n 1~5p for picking the first of five.

  • You should probably use grep -v '^--$', to avoid clobbering lines from the input that contain --.  Even that will fail if a line from the input is --, with nothing else. – G-Man Feb 20 '16 at 2:56
  • i didn't get you man – jevan Feb 20 '16 at 2:59
  • @don_crissti: good tip. Answer updated. – Quora Feans Feb 20 '16 at 3:06
  • @QuoraFeans - keep in mind your answer will work only if the first line in the file doesn't match otherwise the sed part will do the opposite of what's supposed to do (it will print the lines that match instead of discarding them) – don_crissti Feb 20 '16 at 10:32
  • .. | grep -v italic ? When the first line has italic ( two lines right after each other) you have a special case,,, – Walter A Feb 27 '16 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.