Click to See Complete Forum and Search --> : creating comma separated file starting from much bigger logfile


maxthree
03-03-2003, 06:50 PM
Hi,

I'm trying to make a script that does following

1. say you have a log that reads

13h20
I
LOVE
AMANDA
BEARSE

13h50
I
HATE
CARL
LAGERSFELD

...


2. i need a comma separated file that reads
13h20,LOVE,AMANDA
13h50,HATE,CARL

3. im' okay with the grep bit ... I can select lines 1,3,4 ... and >> them to a file. Problem is that all lines are put UNDER each other.

4. now for the tricky part ... how do I let the script create an output file that reads like the one descibed above?

Anybody have a clue ??

spacedog
03-03-2003, 10:48 PM
hello,

Im not sure how your using grep, I havent used it past locating if a string exists inside a file. If you can please post the code you have so far. I did however write a brute perl script to accomplish what you want. this script assumes your input file is formatted like the one you presented in your post and the fields you want to extract are fields 1, 3, 4


by the way, i know someone named amanda bearse. I just cant remember where from?

#!/usr/bin/perl

$infile = 'file.txt'; #file where input exists.
$outfile = 'output.txt'; #file we are going to write output too.

#open $infile for reading
open IN, $infile or die "Could not open $infile for reading: $!";

#open $outfile for writing
open OUT, ">$outfile" or die "Could not open $outfile for writing: $!";

#loop through each line inside $infile until EOF
#concatinate each line with comma and store in $record
#while fails at EOF
$i=1; #i is going to be the lines we want to keep
while(<IN>){
if($_ =~ /^\n/){
$i=1;
next;
}else{
chomp($_); #remove the \n from each line
print OUT $_, "," if($i==1 || $i==3);
print OUT $_, "\n" if($i==4);
$i++;
}
}

close (IN);
close (OUT);

maxthree
03-04-2003, 12:17 AM
Hey spacedog

Tks for the reply. Unforrtunately, i know nothing about perl (know some C but that's about it)

Also, I was a bit vague in describing the prob,

1. Actual datafile looks as follows

01:15
MC 1200 :
-----------
bra 28 2 await user H.E0 no call active
bra 18 7 await user H.E0 no call active
bra 26 1 connected H.F4 no call active verify this port !!!!
bra 26 3 await user H.E0 no call active
bra 26 4 await user H.E0 no call active

MC 9600 GSM :
---------------
bra 21 1 await user H.A0 no call active
bra 8 7 await user H.A0 no call active

2. output I would like to have is
01:15,MC 1200,bra 26 1,verify this port

3. I can grep on lines with 'verify', and use cut to slice off the parts I need and assign them to variables, but how do I include timestamp on each line and make the file CSV

hotleadpdx
03-04-2003, 10:44 AM
maxthree,

Correct me if I'm wrong, but it sounds like you want to do the following:

From this line (or similar lines):
bra 26 1 connected H.F4 no call active verify this port !!!!

Take pertainent information and assign it to variables (for example):

user="bra 26"
connected=1
protocol=H.F4

You then want to take these and make a comma separated file with a timestamp as the first field:

03040640,bra 26,1,H.F4

If that's what you're looking to do, and you have the grep / cut working, all you have to do is add one more variable to get the date/time and then echo the string out:


cur_date=`date %M%D%h%m` # see man page for date

echo "$cur_date,$var1,$var2,$var3,etc" #this will echo out the variables with comma's between

chrism01
03-04-2003, 06:49 PM
According to your first post, you seem to be able to generate a file (say named t.t )like this:

line1
line2
line3
line4
line5
line6
line7
line8
line9

and you want each group of 3 successive lines to be put together on one line, CSV format

Here's a quick'n'dirty soln in shell:

i=0
for line in `cat t1.t`
do
out=$out$line
(( i=$i + 1 ))
if [[ $i -ne 3 ]]
then
out=$out,
continue
else
echo $out
out=""
i=0
fi

done


Result:

line1,line2,line3
line4,line5,line6
line7,line8,line9


HTH