Question : Parse autosys jil file using Unix tools

0.Still trying to get the answer to
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Q_26137365.html
Now I'm taking Pure Unix or just EXCEL approach ,
I need to basically pivot all rows into columns with titles when the column name is before ":" at each line
1.How can I do it with Unix tools: perl/sed/ksh/awk?
2.How can I do it with Excel macro?


The structure of Jil file is like this:
/* ----------------- backupJIL ----------------- */

insert_job: backupJIL   job_type: c
command: autorep -J ALL -q > /home/autosys/...p/autosys_jil_bk
machine: machine
owner: autosys@machine
permission: gx,ge,wx,we
date_conditions: 1
days_of_week: tu,we,th,fr,sa
start_times: "17:00"
description: "Daily backup of job definitions"
std_out_file: /tmp/autosys_jil_backup.out
std_err_file: /tmp/autosys_jil_backup.err
alarm_if_fail: 1
/* ----------------- BC_mount ----------------- */

  insert_job: BC_mount   job_type: c
  box_name: SAN_test_refresh_box
  command: /sysadm/xp/bin/mountprodbc.sh
  machine: b_dexter
  owner: autosys@/* ----------------- BC_mount ----------------- */

  insert_job: BC_mount   job_type: c
  box_name: S..fresh_box
  command: /sysadm/xp/bin..odbc.sh
  machine: machine
  owner: autosys@machine
  permission: gx,wx
  condition: success(Check_BC_Status)
  description: "Mount the BCs on machine"
  std_out_file: >/var/tmp/mountprodbc.log
  std_err_file: >/var/tmp/mountprodbc.err
  alarm_if_fail: 1
 

Answer : Parse autosys jil file using Unix tools

I've done a similar task in Excel and it involved the following basic steps.

  • Get the users input on where the files are located. 
  • Retrieve a full list of files that need to be processed. 
  • For each file: 
    • Open the file as a delimited text file using Workbooks.OpenText (basically all your rows come into column A 
    • Insert formulas into columns in the opened workbook (B, C and D in my case) to parse each line into the data required 
    • Copy the parsed data into a raw data table in the summary workbook (one row per file obviously) 
    • Close the raw data file as it is no longer needed 
  • Next file 

Basically use the power of Excel to open each file, parse it, then copy the results into your summary sheet transforming the rows into columns as you do so. I also kept a column with the originating filename as part of the summary table. I can't attach an example since it contains people's personal info but the code should give you an idea.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
Option Explicit

Public sFolder As String
Public sFilename() As String
Public NFiles As Integer

Sub GetFileList()
Dim varFileList As Variant
Dim i As Integer

    ' Get the directory from the user'
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Show
        If .SelectedItems.Count = 0 Then Exit Sub 'user cancelled'
        sFolder = .SelectedItems(1)
    End With
    
    ' Get a list of all the files in this directory.'
    ' Note that this isn't recursive... although it could be...'
    varFileList = GetFileNames(sFolder, "*.txt")
    
    If Not IsArray(varFileList) Then
        MsgBox "No files found.", vbInformation
        Exit Sub
    End If
    
    ' Setup the filename array based on the selected directory'
    NFiles = UBound(varFileList) + 1
    ReDim sFilename(1 To NFiles)
    
    ' Copy the filenames into a string array for use elsewhere'
    For i = 0 To UBound(varFileList)
        sFilename(i + 1) = CStr(varFileList(i))
    Next i
    
    OutputFilenames2XL
    ImportFileContent
End Sub


Private Function GetFileNames(ByVal sPath As String, Optional sFilter As String) As Variant
'   Returns a one dimensional array with filenames'
'   Otherwise returns False'

Dim f As String
Dim i As Integer
Dim FileList() As String

    If sFilter = "" Then sFilter = "*.*"

    Select Case Right$(sPath, 1)
      Case "\", "/"
         sPath = Left$(sPath, Len(sPath) - 1)
    End Select

    ReDim Preserve FileList(0)

    f = Dir$(sPath & "\" & sFilter)
    Do While Len(f) > 0
        ReDim Preserve FileList(i) As String
        FileList(i) = f
        i = i + 1
        f = Dir$()
    Loop
    
    If FileList(0) <> Empty Then
        GetFileNames = FileList
    Else
        GetFileNames = False
    End If    
End Function


Private Sub OutputFilenames2XL()
Dim iBaseRow As Integer, iBaseCol As Integer
Dim shTarget As Worksheet
Dim i As Integer

    Set shTarget = Application.ActiveSheet
    iBaseRow = Range("BaseCell").Row
    iBaseCol = Range("BaseCell").Column

    'Populate the header fields'
    shTarget.Range("ImportDate").Value = Now
    shTarget.Range("FolderPath").Value = sFolder
    shTarget.Range("NFiles") = NFiles
    
    'Populate filename column of the table'
    For i = 1 To NFiles
        shTarget.Cells(iBaseRow + i - 1, iBaseCol).Value = sFilename(i)
    Next i

    'Put formula to parse transaction id into first column'
    shTarget.Range(Cells(iBaseRow, iBaseCol - 1), Cells(iBaseRow + NFiles - 1, iBaseCol - 1)).FormulaR1C1 = "=MID(R[0]C[1],LEN(R[0]C[1])-14,8)"
End Sub


Private Sub ImportFileContent()
Dim i As Integer, j As Integer
Dim shTarget As Worksheet
Dim iBaseRow As Integer, iBaseCol As Integer
Dim shSource As Worksheet
Dim sActiveFilename As String
Dim sFullPath As String
Dim iRowCount As Integer
Dim iTargetColumn As Integer
Dim rSourceRange As Range

    'Store base data for the target worksheet we are building'
    Set shTarget = Application.ActiveSheet
    iBaseRow = Range("BaseCell").Row
    iBaseCol = Range("BaseCell").Column
    
    
    'Now process each filename just imported to the worksheet'
    Application.ScreenUpdating = False
    Application.WindowState = xlMinimized
    Application.Calculation = xlCalculationManual
    Application.Visible = False
            
    'Setup progress bar'
    Load frmProgress
    frmProgress.ProgressBar.Max = NFiles
    frmProgress.ProgressBar.Value = 0
    frmProgress.Show
    
    For i = 1 To NFiles
        sActiveFilename = sFilename(i)
        sFullPath = sFolder & "\" & sActiveFilename
        
        'Import delimited text file'
        Workbooks.OpenText Filename:=sFullPath, _
        Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
        xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
        Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 1), _
        TrailingMinusNumbers:=True
        
        'Store the sheet name and format / add formulas required'
        Set shSource = Application.ActiveSheet
        iRowCount = Application.WorksheetFunction.CountA(Columns("A:A"))
        shSource.Range(Cells(1, 2), Cells(iRowCount, 2)).FormulaR1C1 = "=FIND("":"",R[0]C[-1])"
        shSource.Range(Cells(1, 3), Cells(iRowCount, 3)).FormulaR1C1 = "=IF(LEFT(R[0]C[-2],7)=""Comment"",61,VALUE(MID(R[0]C[-2],2,R[0]C[-1]-2)))"
        shSource.Range(Cells(1, 4), Cells(iRowCount, 4)).FormulaR1C1 = "=RIGHT(R[0]C[-3],LEN(R[0]C[-3])-R[0]C[-2]-1)"

        'Make sure the cells are updated with the current data (only needed if calculation is set to manual)'
        shSource.Calculate
        
        'Now copy the resulting parsed data into the correct cells on the target worksheet'
        For j = 1 To iRowCount
            iTargetColumn = CInt(shSource.Cells(j, 3).Value)
            Set rSourceRange = shSource.Cells(j, 4)
            shTarget.Cells(iBaseRow + i - 1, iBaseCol + iTargetColumn).Value = rSourceRange.Value
        Next j
        
        'Finally close the source workbook file since we are finished with it'
        Workbooks(sActiveFilename).Close SaveChanges:=False
        
        frmProgress.ProgressBar.Value = i
        frmProgress.Repaint
        
    Next i
    
    shTarget.Columns("C:BK").EntireColumn.AutoFit
    
    frmProgress.Hide
    Application.Calculation = xlCalculationAutomatic
    Application.Calculate
    Application.ScreenUpdating = True
    Application.WindowState = xlMaximized
    Application.Visible = True
End Sub
Random Solutions  
 
programming4us programming4us