The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
converting date format mohan705 Shell Programming and Scripting 7 05-22-2008 11:10 AM
converting text to csv format gthokala Shell Programming and Scripting 13 06-09-2006 10:44 AM
converting PDF to text, rtf doc format saurya_s UNIX for Advanced & Expert Users 1 04-23-2004 03:25 PM
Converting BMP to BM (or other unix format) EJ =) UNIX Desktop for Dummies Questions & Answers 1 06-12-2002 08:42 AM
Converting the File Creation Date to a new format barney_clough UNIX for Dummies Questions & Answers 1 06-12-2002 07:43 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-18-2008
zer0dvide zer0dvide is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 3
converting config file to csv format

Hello,

For 2 days now i've been searching for a solution to this. I am now beginning to doubt this is even possible. It's even harder when you don't know how to search for it. (which keywords generate enough relevancy etc..)

I need to parse a config file to generate a CSV file in return.
It would need to take these parameters out of the file:

define host{

host_name hostA
parents hostNull
}
--
define host{

host_name hostB
parents hostA
}
--
define host{

host_name hostC
parents hostB
}
--

and create this:

host,parent
hostA,hostNull
hostB,hostA
hostC,hostB

Does anyone know a solution to this or can someone point me out to some doc that explains how ?
  #2 (permalink)  
Old 03-18-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink One approach to try...

From what info you provided, this is one approach.

Quote:
> cat sample
define host{

host_name hostA
parents hostNull
}
--
define host{

host_name hostB
parents hostA
}
--
define host{

host_name hostC
parents hostB
}
Code:
> cat bld_csv 
#! /bin/bash

echo "host,parent" >host.csv
while read zf
  do
  hck=$(echo "$zf" | cut -d" " -f1)
  if [ "$hck" = "host_name" ]
    then
    hnm=$(echo "$zf" | cut -d" " -f2)           
  fi
  if [ "$hck" = "parents" ]
    then
    pnm=$(echo "$zf" | cut -d" " -f2)
    echo $hnm","$pnm >>host.csv
  fi

done < sample
Quote:
> cat host.csv
host,parent
hostA,hostNull
hostB,hostA
hostC,hostB
  #3 (permalink)  
Old 03-18-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
Another approach:

Code:
awk ' 
BEGIN{print "host,parent"}
/host_name/{p=$2;getline;print p","$2}
' sample
Regards
  #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
  #5 (permalink)  
Old 03-18-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,047
Code:
nawk 'BEGIN{RS="--";n=1;host[n]=sprintf("host,parent")}
{
n=n+1
host[n]=sprintf("%s,%s",$4,$6)
}
END{
for(i=1;i<=n;i++)
if(host[i]!=",")
print host[i]
}
' filename
  #6 (permalink)  
Old 03-19-2008
faltooweb faltooweb is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 11
converting config file to csv format

awk '
BEGIN { print "host,parent" }
/host_name/ {printf "%s,",$NF; getline; print $NF}
' $INFILE

-Ramesh
  #7 (permalink)  
Old 03-19-2008
zer0dvide zer0dvide is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 3
Thank you very much for all your replies, i will give them a try today and let you know how it turned out.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 02:17 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0