Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting group of unwanted lines from a file.

Status
Not open for further replies.

skuthe

Programmer
Sep 10, 2002
33
0
0
US
Hi,
I need to verify the next line of the matched pattern in the file and if the next line of the pattern does not match 'SQR' word then delete the lines of the matched pattern.

f.eg : Myfile..

/users/ciim/942ftp.com
-------------------------------------

/users/ciim/abm/com/driverall.com
-------------------------------------
sqr $USR_HOME/driverall.sqr $USR_HOME/driverall.txt

/users/ciim/abm/com/drivers.com
-------------------------------------
sqr $USR_HOME/driver10.sqr ciim/iis $USR_HOME/driver10.txt
sqr $USR_HOME/abm/sqr/driver11.sqr $USR_HOME/driver11.txt
sqr $USR_HOME/abm/sqr/driver12.sqr $USR_HOME/driver12.txt

/users/ciim/942ftp.com
-------------------------------------

/users/ciim/943ftp.com
-------------------------------------

/users/ciim/944ftp.com
-------------------------------------

In the above example I want to search for the pattern..
/users/ciim*
------------
If I find it then I need to check that whether the line that is following the ------- line starts with sqr/SQR.
If this line does not start with sqr/SQR then delete the above two lines of the matched pattern ( /users/ciim*
------------ ).

In the above case my output should like ....

/users/ciim/abm/com/driverall.com
-------------------------------------
sqr $USR_HOME/driverall.sqr $USR_HOME/driverall.txt

/users/ciim/abm/com/drivers.com
-------------------------------------
sqr $USR_HOME/driver10.sqr ciim/iis $USR_HOME/driver10.txt
sqr $USR_HOME/abm/sqr/driver11.sqr $USR_HOME/driver11.txt
sqr $USR_HOME/abm/sqr/driver12.sqr $USR_HOME/driver12.txt


Any and All help is appreciated. Thanking you in advance.

Thanks.
 

This might do what you want. It won't work if the "/users/ciim" and "====" lines are immediately followed by another "/users/ciim" line.

/\/users\/ciim/ {
a=$0
if (getline<=0) exit
b=$0
if (getline<=0) exit
if ($1==&quot;sqr&quot; || $1==&quot;SQR&quot;) {
print a
print b
}
print
next
}
{
print
} CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 

Or maybe this is better

{
while ($0 ~ /\/users\/ciim/) {
a=$0
if (getline<=0) exit
b=$0
if (getline<=0) exit
if ($1==&quot;sqr&quot; || $1==&quot;SQR&quot;) {
print a
print b
}
}
print
} CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Many thanks CaKiwi. This is great !. The above program works fine 90% of the time but it fails at the following scenarios.

f.eg: If the input file has following...

/users/ciim/transfer/com/completed/mropd.com
-------------------------------------

/users/ciim/transfer/com/completed/reqsbydept.com
-------------------------------------

/users/ciim/transfer/com/completed/vendbyyr.com
-------------------------------------
if [ -r $USR_HOME/vendbyyr.sqr ]

The o/p after running the program is...

if [ -r $USR_HOME/vendbyyr.sqr ]

i.e.The program deletes the '/users/ciim.../' row before the if statement also. I know you have mention this in your comments but I was wondering whether you can give me any idea to improve on this further. I am a novice in awk but am sure it can be done.

Pl. help.

Thanks.
 
I assumed the SQR would always be the first field of the line as it was in your example. If it can be anywhere in the line, change

if ($1==&quot;sqr&quot; || $1==&quot;SQR&quot;) {

to

if ($0 ~ /[Ss][Qq][Rr]/) {
CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top