Created
April 13, 2015 06:46
-
-
Save hymkor/e26bbc8fef4ac76f6490 to your computer and use it in GitHub Desktop.
lua を awk っぽく使うバッチ($n → S[n]で、 NR、FNR、FILENAME、printf などはだいたいそのまま)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
::rem:: --[[ vim:set ft=lua: | |
@lua "%~f0" %* & exit /b 0 | |
]]-- | |
-- requires Lua 5.3 | |
if #arg < 1 then | |
print( string.format( [[Usage: %s "SCRIPT" files... | |
S[n] = $n on AWK | |
NR = NR on AWK | |
FNR = FNR on AWK | |
FILENAME = FILENAME on AWK | |
printf(...) = io.write(string.format(...))]] , arg[0]) ) | |
os.exit(1) | |
end | |
local function split(line) | |
local S = {} | |
for p in string.gmatch(line,"%S+") do | |
S[#S+1] = p | |
end | |
S[0] = line | |
return S | |
end | |
function printf(...) | |
io.write(string.format(...)) | |
end | |
local onelinear = assert( load(arg[1] ) ) | |
NR = 0 | |
if #arg >= 2 then | |
for i=2,#arg do | |
FNR = 0 | |
FILENAME = arg[i] | |
for line in io.lines(FILENAME) do | |
NR = NR + 1 | |
FNR = FNR + 1 | |
S = split(line) | |
onelinear() | |
end | |
end | |
else | |
FNR = 0 | |
FILENAME="" | |
for line in io.lines() do | |
NR = NR + 1 | |
FNR = FNR + 1 | |
S = split(line) | |
onelinear() | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment