Created
April 13, 2015 10:11
-
-
Save hymkor/6b8a2838dc3dc7e0dddd to your computer and use it in GitHub Desktop.
nyagos 内蔵Luaで AWK っぽいことをする(例:「nuawk.cmd "printf('%03d %s\n',NR,S[0])" 適当なファイル」←「cat -n」と同じ) ※要nyagos 4.0.7_1 or later
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: | |
@nyagos -f "%~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