我相信有人会知道如何解决这个问题。我有一个宠物项目,我试图根据下面的几个文本构建一个数据库。记录按每 6 行(空白)进行分割。字段由连续的空格和形成记录的五行末尾分隔。有些字段确实有空格。
我已经尝试过 DataStage 和 SPSS - 但似乎无法安静地得到结果。我还尝试过 Altova Mapforce,这让我很接近。我选择的数据库可能是 MySQL(因为这是我业余时间的一个项目)
SUNCOR ET AL MEDHAT 9-17-15-4 0416613 ALBERTA CROWN 753.3M
100/09-17-015-04W4/00 S 543.4M W 167.6M MEDICINE HAT 656.8M
DEV (NC) MEDICINE HAT FISH SCALE ZONE
VERTICAL NEW PRODUCTION GAS
SUNCOR ENERGY INC. 09-17-015-04W4
CVE HOUSE 3-23-83-17 0416614 ALBERTA CROWN 536.17M
1AB/03-23-083-17W4/00 N 281.3M E 686.8M BONNYVILLE 283.7M
OV (C) HOUSE MCMURRAY FM
VERTICAL NEW OIL SAND EVALUATION CRUDE BITUMEN
CENOVUS ENERGY INC. 03-23-083-17W4
CVE GRANOR 11-27-82-18 0416615 ALBERTA CROWN 554.69M
1AA/11-27-082-18W4/00 S 756.7M E 677.6M BONNYVILLE 409.2M
OV (C) GRANOR GROSMONT FM
VERTICAL NEW OIL SAND EVALUATION CRUDE BITUMEN
CENOVUS ENERGY INC. 11-27-082-18W4
SUNCOR ET AL MEDHAT 4-17-15-4 0416616 ALBERTA CROWN 750.9M
100/04-17-015-04W4/00 N 320.1M E 317.1M MEDICINE HAT 646.4M
DEV (NC) MEDICINE HAT FISH SCALE ZONE
VERTICAL NEW PRODUCTION GAS
SUNCOR ENERGY INC.
04-17-015-04W4
某些字段确实包含单个空格,但绝不会包含多个连续空格。
最佳答案
当您使用 Windows 时,我已将 awk 和 sed 的内容重新设计为可以在 Windows 上运行而无需它们的东西。直到今天为止,我一生中从未编写过一行 VBScript,因此可能还有其他更简单的方法可以做到这一点:
'###############################################################################
' File: process.vbs
' Author: Mark Setchell
'
' VBScript to process companies file.
'
' Use as follows:
' cscript /nologo process.vbs < file
'
' Or, to save to an output file, use as follows:
' cscript /nologo process.vbs < file > results.txt
'###############################################################################
Dim rxp, inp
Set rxp = new RegExp
rxp.Global = True
rxp.Multiline = False
Do While Not WScript.StdIn.AtEndOfStream
inp = WScript.StdIn.ReadLine()
' Regular expression to match any upper case letter
rxp.Pattern="[A-Z]"
' If there are any letters on the input line
if rxp.Test(inp) Then
' Replace multiple spaces with a single comma
rxp.Pattern=" +"
inp = rxp.Replace(inp, ",")
' Remove leading and trailing commas off line
rxp.Pattern="^,|,$"
WScript.StdOut.Write rxp.Replace(inp, "")
Else
' Write a blank line since there was nothing on input line
WScript.StdOut.WriteBlankLines(1)
End If
Loop
关于mysql - 分隔一系列 txt 并将其放入 csv 或数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20253610/