Extract specific pattern from a file

Hi All,

I am a newbie to Shell Scripting.

I have a File

The Server Name
XXX002
-------------------------
2.1 LAPD

Iface Id Link MTU Side
ecc_3_1 4 Up 512 User
ecc_3_2 5 Up 512 User

The Server Name
XXX003
-------------------------
2.1 LAPD

Iface Id Link MTU Side
ecc_3_1 4 Up 512 User
ecc_3_2 5 down 512 User

I want to seperate All the servers which have a Id "Down" status

Like

The Server Name
XXX003
-------------------------
2.1 LAPD

Iface Id Link MTU Side
ecc_3_1 4 Up 512 User
ecc_3_2 5 down 512 User

Seperated from the file.

How can I do this with Shell Scripting ?

Please help me on the same.

Try

awk '/^The/ {getline; s=$1} /[Dd]own/ {print s}' infile

Hi zaxxon,

Thanks for the reply.

It displays only the server name which has got a link down status

But I want the whole Server details,

The Server Name
XXX003
-------------------------
2.1 LAPD

Iface Id Link MTU Side
ecc_3_1 4 Up 512 User
ecc_3_2 5 down 512 User

Please help me on the same

Thanks again,

Best Regards,

Athreya

try something like this

awk 'disp(var) { if (match( var ~= /^The/) )
                      {if (index(save,"down")>0) { print save}
                        else {save=""}
                    } 
                    else {save=sprintf("%s%s\n", save, var)}
               }
      {disp($0) ; continue }' oldfile > newfile

Hi All,

Sorry for the delay in replying

I achieved the task by

awk '/The server Name/ {n++} {print > f n}' f=/export/home/admin/op /export/home/admin/result.txt;
for i in `ls -lrt /export/home/admin/ | awk '{print $9}' | grep op`;
do
grep down $i

if [ $? -eq 0 ]

then

echo $i >> /export/home/admin/down

else

echo $i >> /export/home/admin/up

fi

done;

cat /export/home/admin/down | while read line

do

cat $line >> /export/home/admin/result$DATE.txt

done

rm /export/home/admin/op*

But when "result.txt" goes too large, awk fails with the error "Too many Files"

Some solution said to use "nawk"

Please help me to convert this to nawk

Regards,

Athreya