Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output:

File inputs
------------------------------------------

Server Host = mike

 id     rl  images   allocated        last updated      density  
           vimages   expiration       last read         <------- STATUS ------->
--------------------------------------------------------------------------------
0000L3  10    161   04/19/2012 17:20  04/28/2012 02:35  hcart3   767701387     1
              161   04/27/2019 02:35  04/27/2012 15:08  FULL

0001L3  10    197   04/19/2012 17:21  04/28/2012 10:53  hcart3   759678237     0
              197   04/27/2019 10:53        N/A         FULL

0002L3  10    145   04/19/2012 17:21  04/29/2012 11:19  hcart3   761213111     0
              144   04/28/2019 11:19        N/A         FULL

0003L3  10    166   04/19/2012 17:21  04/29/2012 03:02  hcart3   764249813     0
              166   04/28/2019 03:02        N/A         FULL

0004L3  10    188   04/19/2012 17:21  04/29/2012 19:44  hcart3   759852744     0
              188   04/28/2019 19:44        N/A         FULL

Server Host = alpha

 id     rl  images   allocated        last updated      density 
           vimages   expiration       last read         <------- STATUS ------->
--------------------------------------------------------------------------------
0030L3  10    327   02/05/2013 21:32  02/07/2013 21:18  hcart3   119479242     1
              327   02/06/2020 20:17  04/23/2013 12:08  FULL

0055L3   8      9   05/29/2014 21:00  05/30/2014 03:20  hcart3    38717803     0
                8   05/30/2015 03:20        N/A         FULL

0072L3  10    853   04/23/2012 16:41  04/26/2012 21:58  hcart3   772844983     0
              853   04/25/2019 20:03        N/A         FULL

Please help to get output below as :

output
----------------------

mike  id     rl  images   allocated        last updated      density   vimages   expiration       last read         <------- STATUS ------->
mike  0000L3  10    161   04/19/2012 17:20  04/28/2012 02:35  hcart3   767701387     1  161   04/27/2019 02:35  04/27/2012 15:08  FULL

mike 0001L3  10    197   04/19/2012 17:21  04/28/2012 10:53  hcart3   759678237     0   197   04/27/2019 10:53        N/A         FULL

mike 0002L3  10    145   04/19/2012 17:21  04/29/2012 11:19  hcart3   761213111     0  144   04/28/2019 11:19        N/A         FULL

mike 0003L3  10    166   04/19/2012 17:21  04/29/2012 03:02  hcart3   764249813     0 166   04/28/2019 03:02        N/A         FULL

mike 0004L3  10    188   04/19/2012 17:21  04/29/2012 19:44  hcart3   759852744     0  188   04/28/2019 19:44        N/A         FULL

alpha  id     rl  images   allocated        last updated      density   vimages   expiration       last read         <------- STATUS ------->
alpha  0030L3  10    327   02/05/2013 21:32  02/07/2013 21:18  hcart3   119479242     1  327   02/06/2020 20:17  04/23/2013 12:08  FULL

alpha  0055L3   8      9   05/29/2014 21:00  05/30/2014 03:20  hcart3    38717803     0  8   05/30/2015 03:20        N/A         FULL

alpha  0072L3  10    853   04/23/2012 16:41  04/26/2012 21:58  hcart3   772844983     0 853   04/25/2019 20:03        N/A         FULL

Please help to get this done suing PERL program

Hi, try something like:

awk '/Server Host/{h=$NF; next} $1=="id" || length($1)==6 {getline n; print h, $0 n}' OFS='\t' file

Hello Tigerhills,

Welcome to forums, kindly use code tags for commands/codes/Inputs used in your posts as per forum rules.
Following may help you in same.

awk '/Server Host/{A=$NF;next} /^ id/{B=A $0;getline;print B  $0} /^00/{J=$0;getline;print A,J, $0}' Input_file

Output will be as follows.

mike id     rl  images   allocated        last updated      density             vimages   expiration       last read         <------- STATUS ------->
mike 0000L3  10    161   04/19/2012 17:20  04/28/2012 02:35  hcart3   767701387     1               161   04/27/2019 02:35  04/27/2012 15:08  FULL
mike 0001L3  10    197   04/19/2012 17:21  04/28/2012 10:53  hcart3   759678237     0               197   04/27/2019 10:53        N/A         FULL
mike 0002L3  10    145   04/19/2012 17:21  04/29/2012 11:19  hcart3   761213111     0               144   04/28/2019 11:19        N/A         FULL
mike 0003L3  10    166   04/19/2012 17:21  04/29/2012 03:02  hcart3   764249813     0               166   04/28/2019 03:02        N/A         FULL
mike 0004L3  10    188   04/19/2012 17:21  04/29/2012 19:44  hcart3   759852744     0               188   04/28/2019 19:44        N/A         FULL
alpha id     rl  images   allocated        last updated      density            vimages   expiration       last read         <------- STATUS ------->
alpha 0030L3  10    327   02/05/2013 21:32  02/07/2013 21:18  hcart3   119479242     1               327   02/06/2020 20:17  04/23/2013 12:08  FULL
alpha 0055L3   8      9   05/29/2014 21:00  05/30/2014 03:20  hcart3    38717803     0                 8   05/30/2015 03:20        N/A         FULL
alpha 0072L3  10    853   04/23/2012 16:41  04/26/2012 21:58  hcart3   772844983     0               853   04/25/2019 20:03        N/A         FULL

Also I have made this script according to the input given by you, if your whole file is of same pattern it will provide you correct(requested) output
else if there are any changes in input file, kindly let us know we can help you to get the requested output.

Thanks,
R. Singh

:slight_smile:

Scrutinizer and Ravindra - friends you both are Bond007.:b::b::b: !! 3 Applauds to you both !!!

experts - if you provide subsequent perl code for same problem , that will be real great and helpfull as in some solaris systems awk doesn't works .

Please provide a similar perl code as well

You're welcome :slight_smile: The code should also work on Solaris, but you need to use /usr/xpg4/bin/awk instead of awk ..