The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 03-18-2008
ShawnMilo ShawnMilo is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 252
A Perl One-Liner


Code:
$ 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

1. Print the headers on first line of input.

2. Print the host name if found (with no newline)

3. Print the comma, parent name, and newline when parent is found.

ShawnMilo