Operating System - HP-UX
1824149 Members
4484 Online
109668 Solutions
New Discussion юеВ

Help with filter lines in a file

 
Andy S
Advisor

Help with filter lines in a file

I have a file which has contents as follows
Job: WHALE #PCBSY9LDT Description:
JCL File : ^CBS^/whale/u17/cdb/admin/CDBY9LDT.sh
Logon : cdb Creator: root


Job: WHALE #PCDBD0UTL Description:
JCL File : ^CDB^/whale/u17/cdb/admin/CDBD0UTL.sh
Logon : cdb Creator: root


Job: WHALE #PCDBD0WEB Description:
JCL File : ^CDB^/whale/u17/cdb/admin/CDBD0WEB.sh
Logon : cdb Creator: root

How do I write a shell script that will filter the word say PCDBD0WE and print that line alongwith the 2 more lines below this word?

Thanks
-Andy

2 REPLIES 2
John Palmer
Honored Contributor

Re: Help with filter lines in a file

A simple shell script like the following will do what you want:-

#!/usr/bin/sh

STRING=PCDBD0WE

while read LINE
do
_if___[[ ${LINE} = *(?)${STRING}*(?) ]];
_then_print -- "${LINE}"
______read LINE || break
______print -- "${LINE}"
______read LINE || break
______print -- "${LINE}"
_fi
done
# end of script

Note that I have put underscores in the script for indentation because the ITRC removes leading spaces. Change them to spaces.

Call it (for instance) filterit. Run it as follows:
filterit < yourfile


Regards,
John
Hein van den Heuvel
Honored Contributor

Re: Help with filter lines in a file


See also:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=237288



awk '/PCDBD0WE/{print $0; i=2; while (i--) {getline; print $0}}' <

awk '//{print $0; i=; while (i--) {getline; print $0}}' <


In your case:

awk '/PCDBD0WE/{print $0; i=2; while (i--) {getline; print $0}}' <


Cheers,
Hein.