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.

On Unix I could do this:

cut -d " " -f 1-3 <my file>

How can I do this on Windows?

Answers


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.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

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

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

The easiest way to do it would be to install Cygwin and use the Unix cut command.


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:

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.


Need Your Help

How to detect hitting the bottom of a grid within a scrollview in Xamarin Forms

xaml xamarin xamarin.forms

I am using Xamarin.Forms to display Items from a Websource in a Grid (within a ScrollView).

Successful AJAX request breaks jQuery funtion

javascript jquery ruby-on-rails ajax

First of all I have read about Event Delegation in several stackoverflow posts and articles like this: Understanding Event Delegation, and it makes sense, it just isn't working in this instance. I am