$ cat temp.txt | perl -ne 'chomp; print "host,parent\n" if $. == 1; print $_ if $_ =~ s/^host_name\s+(.*)$/$1/; print ",$_\n" if $_ =~ s/^parents\s+(.*)$/$1/;' host,parent hostA,hostNull hostB,hostA hostC,hostB