简体   繁体   English

在定义数量的符号“ |”之后解析文本文件

[英]Parsing text file after defined number of symbol “|”

I have a text file that looks like this: 我有一个看起来像这样的文本文件:

442342|442342|213123|5345233|5432543|5|5435|345345|345345|345345 etc. 442342 | 442342 | 213123 | 5345233 | 5432543 | 5 | 5435 | 345345 | 345345 | 345345等

I want to parse it to input a line break after every 5th symbol "|" 我想解析它以在每第5个符号“ |”之后输入换行符 and and import to Excel with | 并使用|导入到Excel as delimiter. 作为分隔符。

So the output would be Excel file with 5 cells in each row containing the data from txt file. 因此,输出将是Excel文件,每行包含5个单元格,其中包含txt文件中的数据。

My idea was to parse it in macro in VB in Word or as batch file and then use import function of Excel to get it into Excel. 我的想法是将其解析为Word中的VB宏或批处理文件,然后使用Excel的导入功能将其导入Excel。 Unfortunately - I failed miserably in both cases. 不幸的是-我在这两种情况下都失败了。

Any ideas how this could be done fast and efficiently? 有什么想法可以快速有效地做到这一点吗?

Transforming the text file into multiple lines with 5 columns each can be easily done using a hybrid JScript/batch utility called REPL.BAT . 使用称为REPL.BAT的混合JScript / batch实用程序,可以轻松地将文本文件转换为多行(每行5列)。 It performs a regex find/replace on stdin and writes the results to stdout. 它在stdin上执行正则表达式查找/替换,并将结果写入stdout。 It is pure script that will run natively on any modern Windows machine from XP onward. 它是纯脚本,可​​以从XP开始在任何现代Windows计算机上本地运行。

Assuming your text contains only one line: 假设您的文本仅包含一行:

<test.txt repl "((.*?\|){4}.*?)\|" "$1\r\n" x >new.txt

If your text file already contains multiple lines, then you can use one more REPL to first remove the newlines and replace them with | 如果您的文本文件已经包含多行,则可以使用一个以上的REPL首先删除换行符,然后将它们替换为| .

<test.txt repl "\|?\r?\n" "|" m | repl "((.*?\|){4}.*?)\|" "$1\r\n" x >new.txt

This assumes that your input data is in a single long text string: 这假定您的输入数据在单个长文本字符串中:

Sub GetAndparse()
    Dim TextLine As String, I As Long, J As Long
    Close #1
    Open "C:\TestFolder\TestFile.txt" For Input As #1
    Line Input #1, TextLine
    ary = Split(TextLine, "|")
    I = 1
    J = 1
    For Each a In ary
        Cells(I, J).Value = a
        J = J + 1
        If J = 6 Then
            J = 1
            I = I + 1
        End If
    Next a
    Close #1
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM