shell script to organize a file

sunilsagar
sunilsagar used Ask the Experts™
on
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
----------------------------
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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

Author

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
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.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

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
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.

Author

Commented:
Thanks so much , this worked perfectly fine,

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial