lua-shell(lsh)是为不喜欢bash 脚本,不想学习sed, awk的程序员设计的。
相较bash,它有两个特点:
1,以lua作为脚本语言。
2,提供一个内建的vim解析器,用来替代sed, awk做文本处理。
下面是几个demo,看一遍就知道这个shell的大概了:
DEMO1
找出当前目录下的文件名结尾是".cp"的文件, 并在删除前询问delete?
代码: 全选
for filename in lfs.dir("./") do
if string.match(filename, ".cp") then
print("delete " .. filename .. "?" )
if(io.read() ~= "y" )
`rm $(filename);
end
end
假如我们有很多文件,它们是按照统一格式命名的:xxx_rgb_xxx.png。 现在,我们想遍历这些文件,并且提取出"rgb"(像比是0xff0000)字段,以便在后续操作中使用。
代码: 全选
for filename in lfs.dir("./") do
vim:load(filename)
:f_lvf_hy: --copy to clipboard for later use
... ...
print(vim:clipboard()) --for example
end
假如我们有两个文件,分别是:
$ cat abc.txt:
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc
$ cat 123.txt:
111 222 333
111 222 333
111 222 333
现在,我们想合并两个文件,得到"result.txt":
aaa 111 bbb 222 ccc 333
aaa 111 bbb 222 ccc 333
aaa 111 bbb 222 ccc 333
那么,对应的lsh脚本是:
代码: 全选
vim:open("1.txt")
vi2 = Vi:new()
vi2:open("2.txt")
vim:share_clipboard(vi2)
for i = 0, vi2.lmax do
vim:Gn(i)
vi2:Gn(i)
repeat
vi2:normal("vey")
:ea :
:p
until vi2:normal("w_") == 0
end
vim:write("result.txt")