I want to print previous line,every time a match was found. I know about grep -A and -B options. But my Solaris 5.10 machine doesn't support those options.I want solution using only sed.Foo.txt:Name is : saraage is : 10Name is : johnage is : 20Name is : Ronage is : 10Name is : peggyage is : 30Out.txt:Name is : saraName is : RonPattern I am trying to match was age is : 10.My environment, Solaris 5.10.
Print previous line after a pattern match using sed?
sed
$ sed -n '/age is : 10/{x;p;d;}; x' Foo.txt Name is : saraName is : RonThe above was tested on GNU sed.  If Solaris' sed does not support chaining commands together with semicolons, try:$ sed -n -e '/age is : 10/{x;p;d;}' -e x Foo.txt Name is : saraName is : RonHow it workssed has a hold space and a pattern space.  Newlines are read into the pattern space.  The idea of this script is that the previous line is saved in the hold space./age is : 10/{x;p;d;}If the current line contains age is : 10, then do:x : swap the pattern and hold space so that the prior line is in the pattern spacep : print the prior lined : delete the pattern space and start processing next linexThis is executed only on lines which do not contain age is : 10.  In this case, save the current line in the hold space.Doing the oppositeSuppose that we want to print the names for people whose age is not 10:$ sed -n -e '/age is : 10/{x;d}' -e '/age is :/{x;p;d;}' -e x Foo.txt Name is : johnName is : peggyThe above adds a command to the beginning, /age is : 10/{x;d}, to ignore any age-10 people.  The command which follows, /age is :/{x;p;d;}, now accepts all the remaining ages.