Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi All,

I am not familiar with the technical codes. need you valuable assistance.
I have a note pad file in below format and I need to import the same in Excel :-

[u0039853@sam ~]$ssh 10.35.9.1
<l1pe-bor-11>scre 0 temp
Info: The configuration takes effect on the current user terminal interface only.
<l1pe-bor-11>dis mac-address dynamic verbose
MAC address table of slot 1:
-------------------------------------------------------------------------------
MAC Address: 4c1f-cc47-4030     VLAN/VSI/SI   : RNC-CHA-01-1208                
Port       : Tun0/0/4           Type          : dynamic                        
PEVLAN     : -                  CEVLAN        : -                              
TrustFlag  : 0                  TrustPort     : -                              
Peer IP    : 212.224.147.4      VC-ID         : 1258004001                     
Aging time : 300                LSP/MAC_Tunnel: 1/50778                        
TimeStamp  : 408468        

MAC Address: 4c1f-cc47-4030     VLAN/VSI/SI   : RNC-CHA-01-1208
Port       : Tun0/0/4           Type          : dynamic
PEVLAN     : -                  CEVLAN        : -
Peer IP    : 212.224.147.4      VC-ID         : 1258004001
Aging time : 300                LSP/MAC_Tunnel: 1/50778
TimeStamp  : 408468

and soo on ....................

and I need to rearrange the data itself in Notepad so that all the headers automatic re-arrange in Nodepad(by comma seperated) and after that I can easily import the same via test to column function.


Note : PE name <l1pe-bor-11>scre 0 temp always needs to be read as soon as in any test showing "scre 0 temp" and repeat the same as soon as another "scre 0 temp" not repeat.

Thanks in advance for your valuable support or feedback.


Rajender
Posted
Updated 17-Feb-15 8:27am
v2
Comments
Zoltán Zörgő 17-Feb-15 11:48am    
Sorry, but your english is quite impossible to understand.
Richard MacCutchan 17-Feb-15 11:49am    
You can import direct into Excel from the File menu.
Zoltán Zörgő 17-Feb-15 11:57am    
No import setting will handle text formatted this way. He will need some regexp, but the requirements are unintelligible.
Richard MacCutchan 17-Feb-15 12:08pm    
True, but it would probably be easier in Excel unless OP is a programmer.
Sergey Alexandrovich Kryukov 17-Feb-15 16:22pm    
There is no such thing as "notepad file"...
Overall, this is off-topic question, not related to software development. You need to find more appropriate forum.
—SA

I personally would not choose to process such large files with VBA - as you have hinted I would pre-process the text file (I would use C# as my preference) to produce a CSV file that could then just be loaded directly into Excel.

However, you have tagged VBA and you have also said that you are not familiar with it, so here is a simple method of loading your file using VBA.
You will need to add in specific information about which parts of the file you want and where to put them in the spreadsheet. Unfortunately you haven't given us enough detail, but hopefully I've made it clear what goes where - look out for "TODO:"

Note: This is not very performant. There are also more elegant ways of doing this but I'm trying to keep it simple.

1. Create the code that follows in a module in Excel (Alt-F11 will bring up the Visual Basic editor. In the left-hand pane you will see your workbook name, right-click on that and select "Insert" then select "Module"

2. You are going to open your text file and read it line by line. For each line you will determine whether or not it is required (or can be discarded), if so then you will copy data from the line into the spreadsheet ... here is the code to read the file from end to end
Sub ReadFile()

    Dim fileName As String
        
    Close   'close any open files
        
    'TODO: Change this line to locate your file
    fileName = "c:\temp\example.txt"
    
    Dim fileNum As Integer
    fileNum = FreeFile
    
    Open fileName For Input As #fileNum
    Dim aLine As String
    Dim rowNum As Integer
    
    rowNum = 3  'TODO: the row number you want the data to start on
    
    Do While Not EOF(fileNum)
    
        Line Input #fileNum, aLine
        If WantLine(aLine) Then ProcessTheLine aLine, rowNum
   
    Loop
    Close #fileNum

End Sub


3. The function WantLine will determine whether or not you can ignore the line or whether you want to process it ...
VB
Function WantLine(aLine As String) As Boolean

    Dim retValue As Boolean
    retValue = False

    If Mid$(aLine, 1, 12) = "MAC Address:" Then retValue = True
    If Mid$(aLine, 1, 12) = "Port       :" Then retValue = True
    If Mid$(aLine, 1, 12) = "TimeStamp  :" Then retValue = True

    'TODO: Insert other checks here

    WantLine = retValue
End Function


4. The subroutine ProcessTheLine starts pulling a line of text apart for the data ...
VB
Sub ProcessTheLine(aLine As String, rowNum As Integer)

    If Mid$(aLine, 1, 12) = "MAC Address:" Then
        'MAC Address will appear in column 1(A)
        'The actual data starts in column 14 for a max of 15 chars
        SetDataForCol 1, rowNum, aLine, 14, 15
        'VLAN/VSI/SI will appear in column 2(B)
        'The actual data starts in column 49 for a max of 20 chars
        SetDataForCol 2, rowNum, aLine, 49, 20
    End If

    If Mid$(aLine, 1, 12) = "Port       :" Then
        SetDataForCol 3, rowNum, aLine, 14, 15
        SetDataForCol 4, rowNum, aLine, 49, 20
    End If

    'TODO: ... etc. Insert code to process the other types of data. Change the locations of the data above if necessary

    'increment the row only when we've processed a "block"
    'note this only works if "Timestamp" is a required line in WantLine
    If Mid$(aLine, 1, 12) = "TimeStamp  :" Then rowNum = rowNum + 1

End Sub


5.Finally, the subroutine SetDataForCol will extract the data from the line at the position provided for the length provided and insert it into the spreadsheet at the location provided
VB
Sub SetDataForCol(col As Integer, rowNum As Integer, aLine As String, start As Integer, slen As Integer)
    Dim data As String
    data = Trim$(Mid$(aLine, start, slen))
    'TODO: You might need to select a sheet or workbook here depending on your needs
    Cells(rowNum, col) = data
End Sub
 
Share this answer
 
Hi Rajendar,

I see that you submitted the question here as well :)
For those who seek answer, you can install Power Query Add-in and follow the answer in TechNet forum here.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900