[英]Extracting columns from text file using Perl one-liner: similar to Unix cut
I'm using Windows, and I would like to extract certain columns from a text file using a Perl, Python, batch etc. one-liner. 我正在使用Windows,我想使用Perl,Python,批处理等一行提取文本文件中的某些列。
On Unix I could do this: 在Unix上,我可以这样做:
cut -d " " -f 1-3 <my file>
How can I do this on Windows? 我怎样才能在Windows上执行此操作?
you can download GNU windows and use your normal cut/awk etc.. Or natively, you can use vbscript 你可以下载GNU窗口并使用你的普通剪切/ awk等。或者原生,你可以使用vbscript
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
strFile = objArgs(0)
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfLine
strLine=objFile.ReadLine
sp = Split(strLine," ")
s=""
For i=0 To 2
s=s&" "&sp(i)
Next
WScript.Echo s
Loop
save the above as mysplit.vbs and on command line 将上面保存为mysplit.vbs并在命令行中保存
c:\test> cscript //nologo mysplit.vbs file
Or just simple batch 或者只是简单的批次
@echo off
for /f "tokens=1,2,3 delims= " %%a in (file) do (echo %%a %%b %%c)
If you want a Python one liner 如果你想要一个Python一个班轮
c:\test> type file|python -c "import sys; print [' '.join(i.split()[:3]) for i in sys.stdin.readlines()]"
That's rather simple Python script: 这是相当简单的Python脚本:
for line in open("my file"):
parts = line.split(" ")
print " ".join(parts[0:3])
最简单的方法是安装Cygwin并使用Unix cut
命令。
If you are dealing with a text file that has very long lines and you are only interested in the first 3 columns, then splitting a fixed number of times yourself will be a lot faster than using the -a
option: 如果您正在处理具有很长行的文本文件而您只对前3列感兴趣,那么自己分割固定次数将比使用-a
选项快得多:
perl -ne "@F = split /\\s/, $_, 4; print qq(@F[0..2]\\n)" file.txt
rather than 而不是
perl -ane "print qq(@F[0..2]\\n)" file.txt
This is because the -a
option will split on every whitespace in a line, which potentially can lead to a lot of extra splitting. 这是因为-a
选项将在一行中的每个空格上拆分,这可能会导致大量额外拆分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.