-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcut.bat
More file actions
16 lines (16 loc) · 739 Bytes
/
cut.bat
File metadata and controls
16 lines (16 loc) · 739 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@echo off
:: cut -d DELIM -f FIELDS file
:: Supports: -d <delim>, -f <1,2,5> (no ranges)
setlocal
set "del=,"
set "fields="
:parse
if "%~1"=="" goto done
if /I "%~1"=="-d" (set "del=%~2" & shift & shift & goto parse)
if /I "%~1"=="-f" (set "fields=%~2" & shift & shift & goto parse)
goto done
:done
if "%fields%"=="" (echo Usage: cut -d ^<delim^> -f ^<fields^> ^<file^> & exit /b 1)
if "%~1"=="" (echo Usage: cut -d ^<delim^> -f ^<fields^> ^<file^> & exit /b 1)
powershell -NoLogo -NoProfile -Command "$f='%~1'; $d='%del%'; $idx=('%fields%'.Split(',') | ForEach-Object { [int]$_ - 1 }); Get-Content -- $f | ForEach-Object { $p = $_ -split [regex]::Escape($d); ($idx | ForEach-Object { if($_ -lt $p.Count){ $p[$_] } }) -join $d }"
endlocal