Need to convert the content of file into COLUMN (To export into excel)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to convert the content of file into COLUMN (To export into excel)
# 1  
Old 12-05-2009
Need to convert the content of file into COLUMN (To export into excel)

I have multiple condition in file as below.

MONITOR "ALERT_INFO"
DESCRIPTION "Triggered when informational Netware alert occured"
MAXTHRESHOLD 95
SEVERITY Normal


MONITOR "ALERT_MAJOR"
DESCRIPTION "Triggered when major Netware alert occured"
MAXTHRESHOLD
SEVERITY Major

I need to convert them in format
MONITOR DESCRIPTION MAXTHRESHOLD SEVERITY

Please suggest
# 2  
Old 12-05-2009
Here's a perl solution:
Code:
#!/usr/bin/perl

use strict;

my @a_rec;
my @a_out;
my $i;
my $line;
my $outline;
my $col1;
my $infile = "monitor.dat";
my $outfile = "monitor.csv";

open INFILE, "<$infile"
  or die "can't open file: $!";

open OUTFILE, ">$outfile"
  or die "can't open file: $!";

print OUTFILE "MONITOR,DESCRIPTION,MAXTHRESHOLD,SEVERITY\n";

@a_rec = (<INFILE>);

$i=0;
while (@a_rec[$i] )
{
   chomp($a_rec[$i]);

   while ( $a_rec[$i] )
   {
      $line = $a_rec[$i];
      $col1 = (split(/ /, $line)) [0];
      $line =~ s/$col1\s*//;
      push (@a_out, $line);
      $i++;
      chomp ($a_rec[$i]);
   }

   if ( $a_out[0] )
   {
      $outline = join(",", @a_out);
      print OUTFILE "$outline\n";
   }
   undef @a_out;
   $i++;
}

close(INFILE);

output:
Code:
cat monitor.csv
MONITOR,DESCRIPTION,MAXTHRESHOLD,SEVERITY
"ALERT_INFO","Triggered when informational Netware alert occured",95,Normal
"ALERT_MAJOR","Triggered when major Netware alert occured",,Major

# 3  
Old 12-05-2009
Here is a nawk solution: -

Code:
nawk '
        BEGIN{ print "MONITOR,DESCRIPTION,MAXTHRESHOLD,SEVERITY" }
        (! NF){next}
        /^DESCRIPTION/{ol = ol substr($0,13)","}
        /^MONITOR|^MAXTHRESHOLD/{ol = ol $2","}
        /^SEVERITY/{ol = ol$2; print ol; ol = ""; next}
' infile > out.csv

# 4  
Old 12-06-2009
Code:
$ awk 'BEGIN {RS=""; FS="\n"; print "MONITOR,DESCRIPTION,MAXTHRESHOLD,SEVERITY" }
{split ($1,a,"\"");split($2,b,"\""); split ($3,c," "); split ($4,d," ")}
{print a[2],b[2],c[2],d[2]}' OFS="," input.txt

# 5  
Old 12-06-2009
I got lazy yesterday and just printed the four column headers. I changed the code to to dynamically determine the column headers. This will allow you to add more rows to your monitor logs and they will be picked up automatically or to use the code against other files with the same format.

Code:
If I add rows to the monitor.dat file:
MONITOR "ALERT_INFO"
DESCRIPTION "Triggered when informational Netware alert occured"
MAXTHRESHOLD 95
SEVERITY Normal
NEW_COL NEW_VAL_1


MONITOR "ALERT_MAJOR"
DESCRIPTION "Triggered when major Netware alert occured"
MAXTHRESHOLD
SEVERITY Major
NEW_COL NEW_VAL_2

They get picked up automatically:
Code:
MONITOR,DESCRIPTION,MAXTHRESHOLD,SEVERITY,NEW_COL
"ALERT_INFO","Triggered when informational Netware alert occured",95,Normal,NEW_VAL_1
"ALERT_MAJOR","Triggered when major Netware alert occured",,Major,NEW_VAL_2

The new code:
Code:
#!/usr/bin/perl

use strict;

my @a_rec;
my @a_out;
my @a_hdr;
my $col_hdr = 0;
my $i;
my $line;
my $outline;
my $col1;
my $infile = "monitor.dat";
my $outfile = "monitor.csv";

open INFILE, "<$infile"
  or die "can't open file: $!";

open OUTFILE, ">$outfile"
  or die "can't open file: $!";

@a_rec = (<INFILE>);

$i=0;
while (@a_rec[$i] )
{
   chomp($a_rec[$i]);

   while ( $a_rec[$i] )
   {
      $line = $a_rec[$i];
      $col1 = (split(/ /, $line)) [0];
      unless ( $col_hdr )
      {
         push (@a_hdr, $col1);
      }
      $line =~ s/$col1\s*//;
      push (@a_out, $line);
      $i++;
      chomp ($a_rec[$i]);
   }

   unless ( $col_hdr )
   {
      $outline = join(",", @a_hdr);
      print OUTFILE "$outline\n";
      $col_hdr++;
   }

   if ( $a_out[0] )
   {
      $outline = join(",", @a_out);
      print OUTFILE "$outline\n";
   }
   undef @a_out;
   $i++;
}

close(INFILE);

close(OUTFILE);

# 6  
Old 12-06-2009
My attempt in bash:
Code:
#!/bin/bash                                                                               

{
    printf "MONITOR DESCRIPTION MAXTHRESHOLD SEVERITY\n"
    while read key value
    do
        case $key in
            "MONITOR") monitor=$value ;;
            "DESCRIPTION") desc=$value ;;
            "MAXTHRESHOLD") max=$value ;;
            "SEVERITY") printf "$monitor $desc $max $value\n" ;;
        esac
    done < $1
} > $2

Usage: script INPUT OUTPUT

Last edited by PerG; 12-06-2009 at 05:01 PM..
# 7  
Old 12-07-2009
Code:
while(<DATA>){
	chomp;
	if(/^$/){
		print "\n";
	}
	else{
		my @tmp=split(" ",$_,2);
		print $tmp[1]," ";
	}
}
__DATA__
MONITOR "ALERT_INFO"
DESCRIPTION "Triggered when informational Netware alert occured"
MAXTHRESHOLD 95
SEVERITY Normal


MONITOR "ALERT_MAJOR"
DESCRIPTION "Triggered when major Netware alert occured"
MAXTHRESHOLD
SEVERITY Major



---------- Post updated at 01:26 AM ---------- Previous update was at 12:23 AM ----------

Code:
while(<DATA>){
	chomp;
	if(/^$/){
		print "\n";
	}
	else{
		my @tmp=split(" ",$_,2);
		print $tmp[1]," ";
	}
}
__DATA__
MONITOR "ALERT_INFO"
DESCRIPTION "Triggered when informational Netware alert occured"
MAXTHRESHOLD 95
SEVERITY Normal


MONITOR "ALERT_MAJOR"
DESCRIPTION "Triggered when major Netware alert occured"
MAXTHRESHOLD
SEVERITY Major

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to copy a column of multiple files and paste into new excel file (next to column)?

I have data of an excel files as given below, file1 org1_1 1 1 2.5 100 org1_2 1 2 5.5 98 org1_3 1 3 7.2 88 file2 org2_1 1 1 2.5 100 org2_2 1 2 5.5 56 org2_3 1 3 7.2 70 I have multiple excel files as above shown. I have to copy column 1, column 4 and paste into a new excel file as... (26 Replies)
Discussion started by: dineshkumarsrk
26 Replies

2. UNIX for Beginners Questions & Answers

How to insert data into black column( Secound Column ) in excel (.XLSX) file using shell script?

Source Code of the original script is down below please run the script and try to solve this problem this is my data and I want it column wise 2019-03-20 13:00:00:000 2019-03-20 15:00:00:000 1 Operating System LAB 0 1 1 1 1 1 1 1 1 1 0 1 (5 Replies)
Discussion started by: Shubham1182
5 Replies

3. UNIX for Beginners Questions & Answers

Convert Excel File (xls) to tab delimited text file on AIX

Hi i have a problem in my job i try to convert an excel file (xls extention) to text file (tab delimited), but no result with this comand cat xxx.xls > xxx.txt Do you have eny idea? PS: sorry for my english Thanks!! (4 Replies)
Discussion started by: frisso
4 Replies

4. Shell Programming and Scripting

Need help to write a shell script to convert text file to excel file.

Hi Everyone, I want your help to write a script which will take text file as input and on the basis of delimiter ":"script will create excel sheet. Example input: IpAdress:InstanceName:Port:ServerName 10.255.255.1:abc:2232:xyz_abc Output should be an excel sheet like below: Column... (8 Replies)
Discussion started by: akabhinav18
8 Replies

5. Shell Programming and Scripting

Need to convert text file into Excel sheet

:cool:Hi, Iam new to this Scripts and forum too. Plz excuse if may i ask silly questions now and further.. Prob: i need to convert txt files data into excel sheet. Suppose if i have hour-wise data in txt file1 and file2 like file1 file2 00: 140 00: 235 01: 160 01:415... (1 Reply)
Discussion started by: mrudula
1 Replies

6. Shell Programming and Scripting

shell scripting to export the results to an excel file

Hi, I am facing issues while exporting the results retrieved from the Database to an excel file. I have 5 count query in order to count the records in 5 different tables. I had connected to the DB and retrieved the column count and printed in the screen. But wat I need to do is, I... (8 Replies)
Discussion started by: Learner 20
8 Replies

7. OS X (Apple)

Creating a file to export into Excel

Hello, This is my first post so not sure what response I will get I do have a very small knowledge of UNIX used last over 10 years ago I have recently move over to an Apple IMAC I need to copy all my jpg files into excel Could someone please let me know if this is possible and... (2 Replies)
Discussion started by: Nacnud
2 Replies

8. Shell Programming and Scripting

script to format rows to column and export to excel

i need to write script to copy the txt file to excel. (data can be 2000+), data may not be in order ex: my name: abc age: 20 add: xyz DOB: 17-mar-2010 add1: adf add2: guioth my name: cat age: 35 DOB: 11-oct-2005 city: yeshjl add: opq DOB: 17-mar-2010 add1: atg add2: gth add3:ert ... (2 Replies)
Discussion started by: pjain
2 Replies

9. Shell Programming and Scripting

How to convert a excel file to a .csv file from unix script

Hi I have a excel file in unix machine and have to convert it into a .csv file.I have to do this from a unix script.How do we do this? Thanks Abhinav (3 Replies)
Discussion started by: akashtcs
3 Replies

10. Shell Programming and Scripting

how to convert fields from a text file to excel columns

i have this file which has the following contents: ,-0.3000 ,-0.3000 ,-0.3000 ,-0.9000 ,-0.9000 ,-0.9000 i would like to get this: -0.3-0.9-0.3-0.9-0.3-0.9 so far i am trying: awk '{for(i=1; i<=NF; i++) {printf("%f\n",$i)}}' test1 > test2 any help... (4 Replies)
Discussion started by: npatwardhan
4 Replies
Login or Register to Ask a Question