LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Formatting result file using Shell script (https://www.linuxquestions.org/questions/programming-9/formatting-result-file-using-shell-script-669193/)

athreyavc 09-11-2008 07:19 AM

Formatting result file using Shell script
 
Hi All,

I have written a Shell script that produces the output like this

The Server Name
XXXXXX002
-------------------------
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
XXXXXX003
-------------------------
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 differentiate the output here.

In the second server one link status is Down, so I want All the servers whose link status is down should be in one file I mean the whole
"
The Server Name
XXXXXX003
-------------------------
2.1 LAPD

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

In a different file and Success results in a different file.

Can we do that with Shell Scripting ?

Regards,

Athreya

vikas027 09-11-2008 08:04 AM

Yes, we can do this through shell scripting.

First, I would recommend you putting a keyword, say "VIKAS" in my example between output for each servers as :-

Code:

VIKAS
The Server Name
XXXXXX002
-------------------------
2.1 LAPD

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



VIKAS
The Server Name
XXXXXX003
-------------------------
2.1 LAPD

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

... and so on.


Then, use this command to put output of each server in different files

Code:

awk '/vikas/{n++}{print > f n}' f=/vikas/op /vikas/output;
In this example, /vikas/output is the base file(as yours) and files needed will be made by op1,op2,op3,op4 ....

Now, you can grep "down" in each file and join them as
Code:

for i in `ls -lrt /vikas/ | awk '{print $9}' | grep op`;
do
grep down $i
  if [ $? -eq 0 ]
  then
  echo $i >> /vikas/down
  else
  echo $i >> /vikas/up
  fi
done;

Now, you have two files /vikas/down where link status is Down
and /vikas/up where ink status is UP.

Now, your task is to join all files whose filenames are in /vikas/down and /vikas/up.

Just give it a try yourself and then ask if any problem occurs.

Regards,
VIKAS

athreyavc 09-22-2008 04:42 AM

Hi Vikas,

Thanks for the reply.

But this did not work for me.

This is what I did.

more filename | awk '/The Server Name/{n++} {print > f n}' f=/home/op /home/output

these files are not getting created.

I am all confused.

Please help me on the same.

Regards,

Athreya VC

athreyavc 09-22-2008 05:57 AM

Hi Vikas,

Sorry, It worked now.

I was able to get it properly.

Thanks a million for your Help.

This case is "CLOSED"


All times are GMT -5. The time now is 02:52 PM.