Need to move vertical to horizontal using paste

Source file

Name:hostname1
 Masking views     : Yes
 Storage Group Names : hostname1
 device (5):
Name:hostname2
 Masking views     : Yes
 Storage Group Names : hostname2
 device (5):
Name:hostname3
 Masking views     : no
 Storage Group Names : hostname3
 device (5):
Name:hostname4
 Masking views     : no
 Storage Group Names : hostname4
Name:hostname5
 Masking views     : Yes
 Storage Group Names : hostname5
Name:hostname6
 Masking views     : Yes
 Storage Group Names : hostname6
 device (5):

Need output like this

Name:hostname1,Masking views     : Yes,Storage Group Names : hostname1,device (5):
Name:hostname2,Masking views     : Yes,Storage Group Names : hostname2,device (5):
Name:hostname3,Masking views     : no,Storage Group Names : hostname3,device (5):
Name:hostname4,Masking views     : no,Storage Group Names : hostname4
Name:hostname5,Masking views     : Yes,Storage Group Names : hostname5
Name:hostname6,Masking views     : Yes,Storage Group Names : hostname6,device (5):

I using

paste -d, - - - -

If i use four dash for some hostname there will be no device so it got jumbled. Is there any way i to update if device not found " Device notfound"

Did you consider searching these forums for similar or even identical problems with solutions to adapt?

If not, try

awk '
LAST    {printf "%s%s", DL, LAST
         DL = ","
        }
/^Name/ {if (LAST !~ /^ device/) printf DNF
         printf TRS
         DL = ""
         TRS = RS
         DNF = " device not found"
        }
        {LAST = $0
        }
END     {print LAST
        }
' file
Name:hostname1, Masking views     : Yes, Storage Group Names : hostname1, device (5):
Name:hostname2, Masking views     : Yes, Storage Group Names : hostname2, device (5):
Name:hostname3, Masking views     : no, Storage Group Names : hostname3, device (5):
Name:hostname4, Masking views     : no, Storage Group Names : hostname4 device not found
Name:hostname5, Masking views     : Yes, Storage Group Names : hostname5 device not found
Name:hostname6, Masking views     : Yes, Storage Group Names : hostname6 device (5):
awk '{l=l ((!/Name:/) ? "," : ((NR>1)?d ORS:_)) $0;d=_} !/[Dd]evice/ {d=", device not found"} END {print l d}' infile
1 Like

Thanks to both

Both the script is working. But when i test with my original file i getting text appended "device not found" to device (5):

Can you test with new input file

This is original input file

Name: server1
   Masking Views              : No
   Storage Group Names        : N/A
Name: server2
   Masking Views              : Yes
   Storage Group Names        : server2    (IsChild)
   Devices (9):
Name: server3
   Masking Views              : Yes
   Storage Group Names        : server3   (IsParent)
   Devices (2):
Name: server4
   Masking Views              : Yes
   Storage Group Names        : server4   (IsParent)
   Devices (9):
Name: server5
   Masking Views              : No
   Storage Group Names        : server5    (IsChild)
   Devices (53):

I can't find device (5) in your new sample. Plus: it appends "device not found" to every single record.
Seeing you being a member close to seven years with more than 110 posts, I'd propose you try to find and post the necessary (very small!) modification by yourself.

No i mean to say it appends to all record.

I am trying to learn but still i dont how this AWK filter works is there any good document to know about this

{l=l ((!/Name:/) ? "," : ((NR>1)?d ORS:_)) $0;d=_}
awk '{l=l ((!/Name:/) ? "," : ((NR>1)?d ORS:_)) $0;d=_} !/[Dd]evice/ {d=", device not found"} END {print l d}' infile

Both r same right what is the difference

letter casing on word "[Dd]evice". Original post word capitalization is different from second post. Script was updated to account for both.

1 Like

use sed

$ sed -r ':x /$/ { N; s/\n\s*/,/; /s/\):,(Name:h)/\):\n\1; bx}' sourcefile