LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-11-2008, 07:19 AM   #1
athreyavc
Member
 
Registered: May 2004
Location: bangalore
Distribution: Cent OS, Ubuntu
Posts: 105

Rep: Reputation: 15
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
 
Old 09-11-2008, 08:04 AM   #2
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,267

Rep: Reputation: 99
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
 
Old 09-22-2008, 04:42 AM   #3
athreyavc
Member
 
Registered: May 2004
Location: bangalore
Distribution: Cent OS, Ubuntu
Posts: 105

Original Poster
Rep: Reputation: 15
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
 
Old 09-22-2008, 05:57 AM   #4
athreyavc
Member
 
Registered: May 2004
Location: bangalore
Distribution: Cent OS, Ubuntu
Posts: 105

Original Poster
Rep: Reputation: 15
Hi Vikas,

Sorry, It worked now.

I was able to get it properly.

Thanks a million for your Help.

This case is "CLOSED"
 
  


Reply

Tags
closed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting doesn't result in empty partition mala fide Linux - Hardware 6 07-27-2008 02:27 AM
Need a shell script to turn a lined file into a comma seperated file Thaidog Linux - General 8 03-08-2008 07:19 PM
Shell script to cd into the result of Find odinsride Programming 5 10-17-2007 10:33 AM
file formatting via shell scripting athreyavc Programming 2 10-09-2007 03:55 AM
Need a script to search and replace text in file using shell script unixlearner Programming 14 06-21-2007 10:37 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration