shell script to organize a file

Hi ,

I am monitoring processes running on multiple remote server using ps command .I want output as hostname  process .
Or, I have the output file which can be converted :

hostname1
--------------------------
process1
process2
prcoess3
prcoess4
--------------------------
hostname2
processA
processB
prcoessC
processD
prcoessE
processF
processG
----------------------------

I want output as :

hostname1
--------------------------
hostname1 process1
hostname1 process2
hostname1 prcoess3
hostname1 prcoess4
--------------------------
hostname2
hostname2 processA
hostname2 processB
hostname2 prcoessC
hostname2 processD
hostname2 prcoessE
hostname2 processF
hostname2 processG
----------------------------
sunilsagarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

simon3270Commented:
How do we know what a hostname looks like?  You first one appears before the "----------" line, while the second appears after it.

If the process lines are all numeric, and the hostnames are not, you could use:
awk '/^[0-9][0-9]*$/{print hostname " " $0;next}
/^--------/{print;next}
{hostname=$0;print;next}' input_file

Open in new window

0
sunilsagarAuthor Commented:
Hi Simon,

Thanks for the input, my output file is exactly same as input.
Below is one sample file :

My input file is like as shown below
hostname1.sample.net:
-----------------------------------
/usr/sbin/httpd
-----------------------------------
hostname2.sample.net:
-----------------------------------
/usr/sbin/httpd
/usr/sbin/httpd1
-----------------------------------
hostname3.sample.net:
-----------------------------------
/usr/sbin/httpd1
/usr/sbin/httpd2
/usr/sbin/httpd3
-----------------------------------


And I want out file as below. "-----------------------------------" is constant everywhere.


hostname1.sample.net:
-----------------------------------
hostname1.sample.net: /usr/sbin/httpd
-----------------------------------
hostname2.sample.net:
-----------------------------------
hostname1.sample.net: /usr/sbin/httpd
hostname1.sample.net: /usr/sbin/httpd1
-----------------------------------
hostname3.sample.net:
-----------------------------------
hostname1.sample.net: /usr/sbin/httpd1
hostname1.sample.net: /usr/sbin/httpd2
hostname1.sample.net: /usr/sbin/httpd3
-----------------------------------

Thanks
0
simon3270Commented:
All of the processes start with "/", so you could change the first line of the script to:
    awk '/^\//{print

and then carry on as before.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

sunilsagarAuthor Commented:
Thanks for the quick input , this works perfectly fine with processes starting with '/' , but failing for the process starting with numbers , alphabet .
Any suggestion ,

Thanks
0
simon3270Commented:
That was the quick fix (I was on my phone, so typing was tricky!).  Here's a more complete fix:
awk '/^--------/{print;st=1-st;next}
{if (st == 0){hostname=$0;print}else{print hostname " " $0;}}'  input_file

Open in new window

This uses a state flag (st) to say whether it is reading the hostname (st = 0) or process names (st = 1).  The state changes on every "-----" line, so it relies on the input being exactly as you said - hostname, followed by a single  "-----" line, followed by processes, followed by a single "-----" line.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sunilsagarAuthor Commented:
Thanks so much , this worked perfectly fine,
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.