Operating System - HP-UX
1834434 Members
2749 Online
110067 Solutions
New Discussion

grep one line + one line more is possible??

 
SOLVED
Go to solution
Carme Torca
Super Advisor

grep one line + one line more is possible??

Hi,

I would like to grep one word in one file, and I have to take this line and the line after... If its possible with grep??... if not is it possibe with another command??

Exemple

If I have this text in a file:

Completed: ALTER DATABASE DATAFILE '/oracle/BUGD02/oradata1/B
Wed Nov 19 18:29:44 2003

Thread 1 advanced to log sequence 2
Current log# 2 seq# 2 mem# 0: /oracle/BUGD02/oradata1/BUGD02/logBUGD02_2.dbl
Wed Nov 19 18:31:07 2003
Thread 1 advanced to log sequence 3
Current log# 3 seq# 3 mem# 0: /oracle/BUGD02/oradata1/BUGD02/logBUGD02_3.dbl
(.....)

Wed Nov 19 18:35:07 2003
Completed: CREATE TABLESPACE RBS DATAFILE '/oracle/BUGD02/ora
Wed Nov 19 18:35:07 2003

I would like to take only the line with Completed and the line with de date.


Thanks!
Carmen.
Users are not too bad ;-)
10 REPLIES 10
Naveej.K.A
Honored Contributor

Re: grep one line + one line more is possible??

hi,

may look a bit complicated, but is should work

awk '/(^Completed)|(^Mon) | (^Tue) | (^Wed) | (^Thu) | (^Fri) | (^Sat) | (^Sun)/ {print $0}' filename

with best wishes
Naveej
practice makes a man perfect!!!
Mark Grant
Honored Contributor

Re: grep one line + one line more is possible??

If you installed GNU grep you could use grep with the "-C 1" option which would print one line of context i.e the line after.

You can get GNU grep here

http://hpux.connect.org.uk/hppd/hpux/Gnu/grep-2.5.1/


Never preceed any demonstration with anything more predictive than "watch this"
Carme Torca
Super Advisor

Re: grep one line + one line more is possible??

Hi,

This only shows lines whith Completed and Mon.... ¿?

Completed: ALTER DATABASE CLOSE NORMAL
Mon Apr 26 00:10:55 2004
Completed: ALTER DATABASE DISMOUNT
Mon Apr 26 03:53:58 2004
Mon Apr 26 03:54:02 2004
Mon Apr 26 03:54:07 2004

But I only need lines with 1) Completed and 2) the date....

If there are more lines with Completed or dates in the middle I would like ignore it.
If it possible with awk??

Thanks!
Carme
Users are not too bad ;-)
Mark Grant
Honored Contributor
Solution

Re: grep one line + one line more is possible??

I'm sure someone could do this better but here is a perl grep that shows the next line too

perl -ne '/text to match/ || next;print $_;$_=<>;print' datafile
Never preceed any demonstration with anything more predictive than "watch this"
Nicolas Dumeige
Esteemed Contributor

Re: grep one line + one line more is possible??

Hello Carme,

I use a script to get all errors in the alert*.log and the "lines around", i.e. 2 before and 5 after without getting duplicated lines - for what it's worth - :

grep -n '^ORA' $PATH/$ALERT_FILE | while read line
do
expr $line - 2 | read before
expr $line + 5 | read after

if [ $last -lt $before ] ; then
printf "%d,%dp\n" $before $after >> $SED_SCRIPT
else
printf "%d,%dp\n" `expr $last + 1` $after >> $SED_SCRIPT
fi

last=$after

expr $line + 1 | read after
printf "%d,%dp\n" $line $after >> $SED_SCRIPT
done
sed -nf $SED_SCRIPT $ALERTE_FILE


Change it to suit your need (^ORA --> ^Completed ; ....)

Cheers

Nicolas
All different, all Unix
Bharat Katkar
Honored Contributor

Re: grep one line + one line more is possible??

Carmen,
what about this one:

# cat filename | egrep 'Completed|Mon|Tues|Wed|Thur|Fri|Sat|Sun'

Regards,
You need to know a lot to actually know how little you know
Mark Grant
Honored Contributor

Re: grep one line + one line more is possible??

Carmen,

I did just notice that my perl thing above will hang if it matches something in the last line of the file because it tries to read the next line.

This change stops that happening

perl -ne '/text to match/ || next;print $_;if(not eof){$_=<>;print}' datafile


Never preceed any demonstration with anything more predictive than "watch this"
Bharat Katkar
Honored Contributor

Re: grep one line + one line more is possible??

Carmen,
See if this one is useful..

#!/usr/bin/ksh
list=`cat file1 | cut -d " " -f 1`
echo " Enter file name to search patterns \n"
read VAR2
myfunct()
{
while true
do
VAR1=$1
if [ $# -gt 0 ]
then
for VAR1 in Mon Wed Tues Fri Sat Sun Completed
do
cat $VAR2 | grep "$VAR1"
done
shift
else
exit
fi
done
}
myfunct $list

Regards,

You need to know a lot to actually know how little you know
Simon Page_1
Frequent Advisor

Re: grep one line + one line more is possible??

Hi Carmen

Try this: sed -n '/^Completed: /{N;p;}'

Regards
Simon
Alan Mason
Advisor

Re: grep one line + one line more is possible??

Its Unix - You can always do it in one line!
(though sometimes the lines can be quite long!!)

cat logfile | sed -n -e '/^Completed/{N;p;}'

Regards
Alan Mason