converting config file to csv format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting converting config file to csv format
# 1  
Old 03-18-2008
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  
Old 03-18-2008
Hammer & Screwdriver 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  
Old 03-18-2008
Another approach:

Code:
awk ' 
BEGIN{print "host,parent"}
/host_name/{p=$2;getline;print p","$2}
' sample

Regards
# 4  
Old 03-18-2008
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  
Old 03-19-2008
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  
Old 03-19-2008
converting config file to csv format

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

-Ramesh
# 7  
Old 03-19-2008
Thank you very much for all your replies, i will give them a try today and let you know how it turned out.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Data extraction and converting into .csv file.

Hi All, I have a data file and need to extract and convert it into csv format: 1) Read and extract the line containing string ending with "----" (file sample_linebyline.txt file) and to make a .csv file from this. 2) To read the flat file flatfile_sample.txt which consists of similar data (... (9 Replies)
Discussion started by: abhi_123
9 Replies

2. Shell Programming and Scripting

Converting csv to html format

Below is the code I have - How can I convert the data in the csv into 3 tables in html. instead of 1 table. Attached is the format I am getting. (1 Reply)
Discussion started by: archana25
1 Replies

3. Shell Programming and Scripting

Need help in converting the file format

Hi All, I need help in converting the mentioned file format into desired output format using awk. Could anyone help me in this? Below is the input.. Date Account Campaign AdGroup Keyword Conversion Revenue Var1 Var2 Var3 Var4 Var5 10 20 30 ... (8 Replies)
Discussion started by: Ravi S M
8 Replies

4. Shell Programming and Scripting

Format problem while converting text file to csv

Hi , I need a help in following scenario.I tried searching in google but couldn't able to find the exact answer. Sorry if i am re-posting already answered query. While i am trying to convert into log file into csv i couldn't able to get the format which i am looking for. I converted file... (4 Replies)
Discussion started by: varmas424
4 Replies

5. Shell Programming and Scripting

Converting rows to columns in csv file

Hi, I have a requirement to convert rows into columns. data looks like: c1,c2,c3,.. r1,r2,r3,.. p1,p2,p3,.. and so on.. output shud be like this: c1,r1,p1,.. c2,r2,p2,.. c3,r3,p3,.. Thanks in advance, (12 Replies)
Discussion started by: Divya1987
12 Replies

6. Shell Programming and Scripting

Converting CSV to ascii format

Hej All, I have a file like this which is a comma dilimited: input: 6000318,-263.011520678,-59.05869872409,587.67924868792 6000319,-265.6996842902,-50.24902479999,590.65693082607 6000320,-238.1333898366,-288.801232595,633.75332173496... (5 Replies)
Discussion started by: Johanni
5 Replies

7. Shell Programming and Scripting

Converting windows format file to unix format using script

Hi, I am having couple of files which i used to copy from windows to Linux, so now in case of text files (CTRL^M) appears at end of line. I know i can convert this windows format file to unix format file by running dos2unix. My requirement here is that i want to do it automatically using a... (5 Replies)
Discussion started by: sarbjit
5 Replies

8. Shell Programming and Scripting

Converting txt file in csv

HI All, I have a text file memory.txt which has following values. Average: 822387 7346605 89.93 288845 4176593 2044589 51883 2.47 7600 i want to convert this file in csv format and i am using following command to do it. sed s/_/\./g <... (3 Replies)
Discussion started by: mkashif
3 Replies

9. Shell Programming and Scripting

Converting file format

My input file is Pipe delimited with 10 fields, I am trying to create a tab delimited output file with 6 fields from the provided input file. Below is sample data Input file abc||2|PIN|num||||www.123.com|abc@123.com| bcd||2|PIN|num|||||abc@123.com|... (3 Replies)
Discussion started by: pasupuleti81
3 Replies

10. Shell Programming and Scripting

converting text to csv format

I am trying to check each line and based on first two digits, the comma needs to be place. I checked in the earlier post where the text is converted to csv with a tab delimited. Here is the test file that needs to be changed to csv 11 051701 22 051701 330123405170105170112345... (13 Replies)
Discussion started by: gthokala
13 Replies
Login or Register to Ask a Question