0

有没有一种简单的方法,最好是使用脚本语言或可以通过批处理文件调用的小工具,对文本文件进行操作,标记偏移量,并将偏移量之后的所有内容放入新文件中?

我有一个文本文件添加到 nightly,我想让它标记文件的结尾,然后在添加新数据后,只处理偏移量和结尾之间的数据。我不能只用字符串或分隔符来做到这一点,因为它是 blob 数据。

编辑:文本文件是通过从计划任务中运行 ms 访问宏来创建的,该宏将数据导出为 csv 文件。在考虑帕特里克的建议时,我想知道是否可以在文件名中添加通配符,例如日期,以始终拥有不同的文件。然后这个文件将被 scp'd 到一个 linux 服务器,在那里它将被加载到一个 mysql 数据库中。

4

3 回答 3

2

用python很简单:

import sys

def divide_file(fname, mark):
    mark_found = 0
    f = file(fname, 'r')
    for line in f.readlines():
        if mark in line:
            mark_found = 1
        if mark_found:
            print line.rstrip()
    f.close()

divide_file(sys.argv[1], sys.argv[2])

用法和输出示例:

c:\tmp>divide_file.py divide_file.py close
        f.close()

divide_file(sys.argv[1], sys.argv[2])
于 2009-01-28T13:19:22.237 回答
1

我可以从类 Unix 系统中想到tailbash和其他实用程序。您可以通过最低限度地安装MSYS在 Windows 上获得这些。引用这些实用程序的文档和示例很容易找到。而且bash的东西比 Windows 批处理文件要强大得多。脚本看起来像这样:

#!/bin/bash

PREV_SIZE=`du -b text_file`
write_something_to_file text_file
CURR_SIZE=`du -b text_file`
let NUM=$PREV_SIZE-$CURR_SIZE
tail -c $NUM > new_text_file
于 2009-01-28T13:34:19.820 回答
1

假设您当前已经使用脚本从 Access 数据库中导出数据:

@echo OFF

:: Force a new line and add a marker; assuming your file is data.txt.
@echo. >> data.txt
@echo **MARKER** >> data.txt

:: Run your export here: these lines just simulate the export.
@echo Test Line 1 >> data.txt
@echo Test Line 2 >> data.txt

:: Find line number of last marker:
for /f "usebackq delims=:" %%I in (`findstr /N "**MARKER**" data.txt`) do (
    set LAST_MARKER=%%I
)

:: Get all the lines after the last marker
for /f "skip=%LAST_MARKER% tokens=*" %%L in (data.txt) do (
    @echo %%L >> new_data.txt
)

中的输出new_data.txt将是:

测试线 1
测试线 2

于 2009-01-28T16:05:26.350 回答