Linux 将 bash `ls` 输出转换为 json 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10234327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert bash `ls` output to json array
提问by Jeroen
Is it possible to use a bash script to format the output of the ls
to a json array? To be valid json, all names of the dirs and files need to be wrapped in double quotes, seperated by a comma, and the entire thing needs to be wrapped in square brackets. I.e. convert:
是否可以使用 bash 脚本将 的输出格式化ls
为 json 数组?要成为有效的 json,目录和文件的所有名称都需要用双引号括起来,用逗号分隔,并且整个东西都需要用方括号括起来。即转换:
jeroen@jeroen-ubuntu:~/Desktop$ ls
foo.txt bar baz
to
到
[ "foo.txt", "bar", "baz" ]
edit: I strongly prefer something that works across all my Linux servers; hence rather not depend on python, but have a pure bash solution.
编辑:我非常喜欢可以在我所有的 Linux 服务器上运行的东西;因此,而不是依赖于 python,而是有一个纯 bash 解决方案。
采纳答案by Dave
Use perl as the encoder; it's guaranteed to be non-buggy, is everywhere, and with pipes, it's still reasonably clean:
使用perl作为编码器;它保证没有问题,无处不在,而且有管道,它仍然相当干净:
ls | perl -e 'use JSON; @in=grep(s/\n$//, <>); print encode_json(\@in)."\n";'
回答by Erwald
Personnaly, I would code script that would run the command ls, send the output to a file of you choice while parsing the output to make format it to a valid JSON format.
Personnaly,我会编写运行命令 ls 的脚本,将输出发送到您选择的文件,同时解析输出以将其格式化为有效的 JSON 格式。
I'm sure that a simple Bash file will do the work.
我确信一个简单的 Bash 文件就可以完成这项工作。
回答by Ignacio Vazquez-Abrams
Yes, but the corner cases and Unicode handling will drive you up the wall. Better to delegate to a scripting language that supports it natively.
是的,但是极端情况和 Unicode 处理会让您望而却步。最好委托给原生支持它的脚本语言。
$ ls
あ a "a" à a b 私
$ python -c 'import os, json; print json.dumps(os.listdir("."))'
["\u00e0", "\"a\"", "\u79c1", "a b", "\u3042", "a"]
回答by DonCallisto
回答by TaoJoannes
Should be pretty easy.
应该很容易。
$ cat ls2json.bash
#!/bin/bash
echo -n '['
for FILE in $(ls | sed -e 's/"/\"/g')
do
echo -n \"${FILE}\",
done
echo -en \b']'
then run:
然后运行:
$ ./ls2json.bash > json.out
but python would be even easier
但是python会更容易
import os
directory = '/some/dir'
ls = os.listdir(directory)
dirstring = str(ls)
print dirstring.replace("'",'"')
回答by Lou Franco
Here's a bash line
这是一个 bash 行
echo '[' ; ls --format=commas|sed -e 's/^/\"/'|sed -e 's/,$/\",/'|sed -e 's/\([^,]\)$/\"\]/'|sed -e 's/, /\", \"/g'
Won't properly deal with "
, \
or some commas in the name of the file. Also, if ls
puts newlines between filenames, so will this.
无法正确处理"
,\
或文件名中的一些逗号。此外,如果ls
在文件名之间放置换行符,也会如此。
回答by Tronix117
Hello you can do that with sed and awk:
你好,你可以用 sed 和 awk 做到这一点:
ls | awk ' BEGIN { ORS = ""; print "["; } { print "\/\@"use JSON;
my @ls_output = `ls`; ## probably better to use a perl module to do this, like DirHandle
print encode_json( @ls_output );
"\/\@"; } END { print "]"; }' | sed "s^\"^\\\"^g;s^\/\@\/\@^\", \"^g;s^\/\@^\"^g"
EDIT: updated to solve the problem with "
and spaces. I use /@
as replacement pattern for "
, since /
is not a valid character for filename.
编辑:更新以解决"
和空格的问题。我/@
用作 的替换模式"
,因为/
它不是文件名的有效字符。
回答by gpojd
Don't use bash, use a scripting language. Untested perl example:
不要使用 bash,使用脚本语言。未经测试的 perl 示例:
$ tree --dirsfirst --noreport -n -X -i -s -D -f -o my.xml
回答by Roelof Berkepeis
I was also searching for a way to output a Linux folder / file tree to some JSON or XML file. Why not use this simple terminal command:
我也在寻找一种将 Linux 文件夹/文件树输出到一些 JSON 或 XML 文件的方法。为什么不使用这个简单的终端命令:
python -c 'import os, json; print json.dumps(os.listdir("/yourdirectory"))'
so, just the linux tree command, and config your own parameters. Here -X
gives XML output! For me, that's OK, and i guess there's some script to convert XML to JSON ..
所以,只需 linux tree 命令,并配置您自己的参数。这里-X
给出了 XML 输出!对我来说,没关系,我想有一些脚本可以将 XML 转换为 JSON ..
NOTE: I think thiscovers the same question.
注意:我认为这涵盖了相同的问题。
回答by Adeel Ahmad
Most of the Linux machine already has python. all you have to do is:
大多数Linux机器已经有python了。你所要做的就是:
##代码##This is for . directory , you can add any path.
这是为了 . directory ,您可以添加任何路径。