Rearranging content of file

Hi Guys,

Need help with modifying the file using shell script.I have file as :

------------
Hostname1
------------
process1
process2
process3
------------
Hostname2
------------
process4
prcoess5
prcoess6
prcoess7
------------ and so on.

I need some syntax which I can use to update the file as :
Hostname1 process1
Hostname1 process2
Hostname1 process3
Hostname2 process4
Hostname2 process5
Hostname2 process6
Hostname2 process7

Now able to think , how can I start for this, please help. Thanks
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.

ozoCommented:
perl -ne 'print "$ARGV $_"' Hostname[12]
0
sunilsagarAuthor Commented:
Thanks for quick response.

I tried with above syntax, as
perl -ne 'print "$ARGV $_"' Hostname[12] filename

I got output as

filename ------------
filename Hostname1
filename ------------
filename process1
filename process2
filename process3
filename ------------
filename Hostname2
filename ------------
filename process4
filename prcoess5
filename prcoess6
filename prcoess7
filename ------------
0
ozoCommented:
Or did you mean something like
#!/usr/bin/perl
my $h=1;
while( <DATA> ){
    chomp;
    if( /------------/ ){
        $h^=1;
    }else{
        $h[$h] = $_;
        print "@h\n" if $h;
    }
}
__DATA__
------------
Hostname1
------------
process1
process2
process3
------------
Hostname2
------------
process4
prcoess5
prcoess6

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

sunilsagarAuthor Commented:
Yes, exactly this way I am looking for . I got the output as expected. Is it possible I can modify the script and pass the inputfile some like ./perlscript.pl <inpout data file>


Output :
Hostname1 process1
Hostname1 process2
Hostname1 process3
Hostname2 process4
Hostname2 prcoess5
Hostname2 prcoess6
0
ozoCommented:
perl -lne 'if( $h^=/------------/ ){ $p=$_ }elsif( /\w/ ){ print "$p $_"}' filename
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 for quick help
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
Linux

From novice to tech pro — start learning today.