Board logo

标题: sed 命令能否分割TXT文档? [打印本页]

作者: youaoyi     时间: 2008-7-11 17:47    标题: sed 命令能否分割TXT文档?
请教各位大侠,在txt文本行数未知的情况下,
sed能否按照每6万条数据来分割TXT文档呢?

作者: lxmxn     时间: 2008-7-13 00:44
这个你应该用split来分隔更方便。

作者: youaoyi     时间: 2008-7-13 01:42
split 只能按照容量分割啊?
这样数据会被断行的
而且也达不到要取60000行的要求.

作者: lxmxn     时间: 2008-7-13 02:18
你怎么就知道不可以呢?
split --help

作者: youaoyi     时间: 2008-7-14 00:18
斑竹,几天前就查过了。只有一个按 容量划分的像 -100k 这样,还有一个按照软盘容量划分的 -a 参数,没有别的了啊?

作者: youaoyi     时间: 2008-7-14 01:50
难道是自己用的split 0.92 版本太低?不支持 split -100 a.txt这种格式?

换用下面的代码了,可惜速度不是很快...45秒仅能处理1万条。

@echo off
for /f %%i in ('sed -n "$=" a.txt') do set number=%%i
:loop
set /a num+=1
set /a n=(%num%-1)*100+1
set /a m=100*%num%
sed "%n%,%m%w a_%num%.txt" a.txt
if %m% LSS %number% goto :loop

Last edited by youaoyi on 2008-7-14 at 01:53 AM ]

作者: lxmxn     时间: 2008-7-14 14:11
D:\lxmxn\work\Other>(echo hello,world&echo.this is the second line of the output&echo.cn-dos.net&echo\OK,let's go&echo/Thanks a lot) | split -l 2

D:\lxmxn\work\Other>dir /b x*
xaa
xab
xac

D:\lxmxn\work\Other>type x*

xaa


hello,world
this is the second line of the output

xab


cn-dos.net
OK,let's go

xac


Thanks a lot

D:\lxmxn\work\Other>split --version
split (textutils) 2.1
Written by Torbjorn Granlund and Richard M. Stallman.

Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

D:\lxmxn\work\Other>
我用的win32版本,并非DOS版本,应该是又DOS版本的。

win32 下载地址:http://www.vkill.net/tools/split_.rar

作者: qzwqzw     时间: 2008-7-14 17:25
是有个叫 csplit.exe 的 GNU Unix Util
可以这里下载到它的Win32移植版本
http://unxutils.sourceforge.net/

http://unxutils.sourceforge.net/UnxUtils.zip
http://unxutils.sourceforge.net/UnxUpdates.zip

里面也包含了sed/gawk/agrep/egrep/fgrep等常用与非常用的Unix命令移植版

作者: ngd     时间: 2008-7-15 20:38
试一试 awk

gawk "{n=int((NR-1)/100)+1;{print $0>>\"a_\"n\".txt\"}}" a.txt

win32版 gawk下载地址:http://www.vkill.net/tools/gawk_3.1.5.rar

Last edited by ngd on 2008-7-16 at 04:42 PM ]

作者: ngd     时间: 2008-7-15 22:48
不借助外部工具

忽略空行

@echo off & setlocal enabledelayedexpansion
set m=1
for /f "delims=" %%a in (a.txt) do (
if !n! GEQ 100 set n=0 & set/a m+=1
echo %%a>>a_!m!.txt
set/a n+=1
)

保留空行

@echo off & setlocal enabledelayedexpansion
set m=1
for /f "delims=" %%a in ('findstr /n .* a.txt') do (
if !n! GEQ 100 set n=0 & set/a m+=1
for /f "tokens=1* delims=:" %%i in ("%%a") do echo.%%j>>a_!m!.txt
set/a n+=1
)