Windows 上的文件偏移量

发布于 2024-07-13 00:55:20 字数 331 浏览 7 评论 0原文

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

我有一个文本文件添加到 nightly,我想标记文件末尾,然后添加新数据后,仅处理偏移量和末尾之间的数据。 我不能仅使用字符串或分隔符来执行此操作,因为它是 blob 数据。

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

Is there an easy way, preferably with a scripting language or a small tool that could be called through a batch file, to operate on a text file, mark an offset, and put everything after the offset into a new file?

I have a text file added to nightly, and I would like to make it so that the end of the file is marked, then after new data is added, only data between the offset and the end is processed. I can not do this with just strings or delimiters as it is blob data.

edit: The text file is created by running a ms access macro from a scheduled tasks, which exports the data as a csv file. In considering Patricks suggestion, I would like to know if it would be possible to add a wildcard such as the date to the filename, to always have a different file. This file will then be scp'd to a linux server, where it will be loaded into a mysql database.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨的味道风的声音 2024-07-20 00:55:20

使用 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])

It is simple with 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])

Usage & output example:

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

divide_file(sys.argv[1], sys.argv[2])
虫児飞 2024-07-20 00:55:20

假设您当前已使用脚本从 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

Assuming you're currently exporting the data from the Access database with a script already:

@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
)

The output in new_data.txt will be:

Test Line 1
Test Line 2

你的呼吸 2024-07-20 00:55:20

我可以想到 tailbash 以及来自类 Unix 系统的其他实用程序。 您可以通过最少安装 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

I could think of tail, bash and other utilities from Unix-like systems. You could get those on Windows by minimally installing MSYS. Documentation and examples referring to these utilities are quite easy to find. And bash stuff is way stronger than Windows batch files. The script would look something like this:

#!/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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文