tags:

views:

65

answers:

5

How do I print a line which matches a particular pattern and the line before it?

I do have a dump like this:

Apple:Orange=9942501133;
Fault Code 9
Apple:Orange=9942501144;
Fault Code 9
Apple:Orange=9942501155;
Apple:Orange=9942501166;
Apple:Orange=9942501177;
Fault Code 9
Apple:Orange=9942501188;
Apple:Orange=9942501199;
Apple:Orange=9942501200;
Apple:Orange=9942501211;
Fault Code 9
Apple:Orange=9942501222;

The output result to be the above line of "Fault Code 9" with Fault Code 9 included:

Apple:Orange=9942501133;
Fault Code 9
Apple:Orange=9942501144;
Fault Code 9
Apple:Orange=9942501177;
Fault Code 9
Apple:Orange=9942501211;
Fault Code 9
+1  A: 

This should do the job.

cat yourfile | perl -e 'while(<STDIN>) { if(/Fault Code 9/) { print $prev; } $prev=$_; }'

In pure shell:

cat yourfile | while read line
do
  if [ "$line" == "Fault Code 9" ]; then
    echo "$prev"
  fi
  prev=$line
done
lasseoe
Great job...Thanks a lot...
http://sial.org/howto/shell/useless-cat/
Ether
Lasseoe ... It works ...Can u guide me to modify the output as Apple:Orange=9942501133;Fault Code 9Apple:Orange=9942501144;Fault Code 9Apple:Orange=9942501177;Fault Code 9Apple:Orange=9942501211;Fault Code 9ie, Number folled by Fault code .
@user: read the man pages for the commands used, and make the modifications yourself! We are not being paid to do your work; **you are**.
Ether
A: 
tr -d '\n' <yourfile | grep -E -o '[^; ]+; *Fault Code 9' | sed 's/;.*$//'
Aleksey Otrubennikov
http://sial.org/howto/shell/useless-cat/
Ether
@Ether: tr cannot take input from file, cat is necessary
Aleksey Otrubennikov
@Aleksey: a cat with one argument is never necessary in the shell, as you can use a funnel: `tr ... < yourfile | grep ...`
Ether
@Ether: yes, thanks for tip
Aleksey Otrubennikov
A: 
grep -B1 'Fault Code 9' filename.txt 
Ether
the output is not as required
Aleksey Otrubennikov
Ether Ji,what is ment by -B1 . The mentioned command does not work .it throws error like this grep: can't open -B1grep: can't open Fault Code 9
The `-B` option specifies how many lines *before* the search parameter it will print. Please type `man grep` at the command line.
Ether
@Ether: The older question was merged into this one. I'm reopening this one now.
Michael Myers
+3  A: 
# grep -B1 ^Fault log.txt

The -B switch means "before".

eduffy
Assuming GNU `grep`; POSIX `grep` does not support this option.
Jonathan Leffler
Hi Leffler, So kind of u to reply but it does not work.illegal option -- B is the reply from sun server .plz help .Can u try the below cmdcat yourfile | perl -e 'while(<STDIN>) { if(/Fault Code 9/) { print $prev; } $prev=$_; }' and reply
Buddy no reply ?????????
+1  A: 

gawk:

/^Fault Code 9/ {
  print s
  print $0
}

{
  s = $0
}
Ignacio Vazquez-Abrams
This illustrates the basic algorithm - if you need to print N lines prior to the match, then you have to store the N lines. If you have N > 1, then you also have to worry about making sure that you don't print too much data when the match occurs twice within a space of N lines.
Jonathan Leffler