Extracting columns from text file using Perl one-liner: similar to Unix cut Extracting columns from text file using Perl one-liner: similar to Unix cut unix unix

Extracting columns from text file using Perl one-liner: similar to Unix cut


Here is a Perl one-liner to print the first 3 whitespace-delimited columns of a file. This can be run on Windows (or Unix). Refer to perlrun.

perl -ane "print qq(@F[0..2]\n)" file.txt


you can download GNU windows and use your normal cut/awk etc..Or natively, you can use vbscript

Set objFS = CreateObject("Scripting.FileSystemObject")Set objArgs = WScript.ArgumentsstrFile = 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 sLoop

save the above as mysplit.vbs and on command line

c:\test> cscript //nologo mysplit.vbs file

Or just simple batch

@echo offfor /f "tokens=1,2,3 delims= " %%a in (file) do (echo %%a %%b %%c)

If you want a Python one liner

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:

for line in open("my file"):    parts = line.split(" ")    print " ".join(parts[0:3])