Last active
August 29, 2015 14:06
-
-
Save hymkor/8ff1911b6bc79e9c9492 to your computer and use it in GitHub Desktop.
Lua を AWK 的に使う支援バッチ:(例)luawk "printf('%03d %s\n',NR,S[0])" ファイル名
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