0
votes

I have several logfiles stored in a dir lets say /x_dir.

Log files are divided in different sections A B C

A is Textual part

B is mostly tabulated

C is also tabulated but with more columns

A-----
ds ads adsa
ad sad sads
ad sa dsa dsa
dsa dsad sad
ad sad sa dsa
asd sa dsasa

B------
name     2
age      3
len      4
char     5
 e      6

C------

header min max avg a 2 3 4
b 6 7 8
c 10 11 12 d 1 2 3
e 5 6 7

Currently I am using a for loop i.e

for file in $(ls /x_dir)
do
grep "pattern" | awk '{print $"what I need"}' >> write_to_a_file
..
..
..
.
.
.
repeat until values from all the rows are extracted  

done

After this I have to display these extracted values column wise with column headings being the same for the row of each name in section B and C . like :

logfile_no name age len char   a      b       c       d       e 
  1          x   x   x   x    min-max min-max min-max min-max min-max
  2          x   x   x   x    min-max min-max min-max min-max min-max
  3          x   x   x   x    min-max min-max min-max min-max min-max
  4          x   x   x   x    min-max min-max min-max min-max min-max

I am doing this by pasting several files together into one file and the doing a

cat filename to display the contents on screen.

please see the script if you want to see details :

http://textuploader.com/d0z0u

I am new to Tcl and couldn't get my head around on a quick and better way to do it in Tcl.

I want to switch to tcl in order to get it running with a GUI which I have started writing with Tk.

Thanks for Help.

1

1 Answers

0
votes

Overall, your script is too long for it to be suitable for wholesale conversion for free by random dudes on the internet. However, we are happy to help you work on the conversion:


For the part you mention explicitly here:

for file in $(ls /x_dir)
do
grep "pattern" | awk '{print $"what I need"}' >> write_to_a_file
…
done

This sort of thing can be done in Tcl fairly easily. (I'm basing off the code you posted at your link.)

# Trivial helper
proc appendToFile {filename content} {
    set f [open $filename "a"]
    puts $f $content
    close $f
}

foreach filename [glob -directory $loc -type f *] {
    set f [open $filename]
    # If the files are in another encoding, e.g. an EBCDIC code page, configure that here with fconfigure
    set lines [split [read $f] "\n"]
    close $f

    foreach line [lsearch -all -inline -regexp "C02" $lines] {
        # Tcl uses zero-based indexing
        set name [lindex [split [lindex $line 1] ":"] 1]
        appendToFile $loc/Logs/line_name $name
    }

    # etc.
}

You can also do the intermediate processing by calling out to grep and awk, bearing in mind that Tcl and Shell have a bit different syntax for invoking subcommands (Tcl's equivalent of single-quotes in shell is {braces}, and you must explicitly exec at the start):

exec grep "C02" $filename | awk {{print $2}} | awk -F ":" {{print $2}} >> $loc/Logs/line_name

It's the sort of thing you might do to start out with before switching processing to be internal. (Keeping processing within a single process will greatly reduce the bottlenecks due to I/O.)