我有一个大文本文件// dbname.filename.json[{"some":"very long", "...":"...", "JSON":"string"}]
// dbname.other_filename.json[{"sdfs":"dfdv", "...":"...", "osifu":"cvvcb"}]
// dbname.yet_another_filename.json[{"vcv":"t54tr", "...":"...", "yugf":"hft"}]
,其中包含
filename.json
我想使用Bash将大文件拆分为JSON文件,每个文件名都在行的开头。在上面的例子中,这将是grep '^\/\/ dbname\.' large.txt | sed -r 's#// dbname\.##'
。我才到达这里
filename.json
有没有办法拆分文本行,并使用一个部分作为文件名,另一部分作为文件内容?
结果将是,例如
[{"some":"very long", "...":"...", "JSON":"string"}]
仅包含字符串other_filename.json
[{"sdfs":"dfdv", "...":"...", "osifu":"cvvcb"}]
仅包含字符串{{1}} 答案 0 :(得分:1)
扩展您的管道,我们得到:
grep '^\/\/\s*dbname\.' large.txt | sed -r 's#//\s*dbname\.##;s/\[/>[/1' | awk -F '>' '{for(i=2;i<=NF;i++) print $i >> $1}'
输出:
$ cat large.txt
// dbname.filename.json[{"some":"very long", "...":"...", "JSON":"string"}]
// dbname.other_filename.json[{"sdfs":"dfdv", "...":"...", "osifu":"cvvcb"}]
// dbname.yet_another_filename.json[{"vcv":"t54tr", "...":"...", "yugf":"hft"}]
$ ls
large.txt
$ grep '^\/\/\s*dbname\.' large.txt | sed -r 's#//\s*dbname\.##;s/\[/>[/1' | awk -F '>' '{for(i=2;i<=NF;i++) print $i >> $1}'
$ ls
filename.json large.txt other_filename.json yet_another_filename.json
$ cat filename.json
[{"some":"very long", "...":"...", "JSON":"string"}]
$ cat other_filename.json
[{"sdfs":"dfdv", "...":"...", "osifu":"cvvcb"}]
$ cat yet_another_filename.json
[{"vcv":"t54tr", "...":"...", "yugf":"hft"}]
答案 1 :(得分:1)
您可以试试awk
:
awk -F'[' '{sub(/^\/\/\s*dbname\./,"",$0); f=$1; $1=FS; print $0 > f }' file
答案 2 :(得分:0)
您可以将此简单的awk命令与自定义字段分隔符一起使用:
awk -F '\\.json' 'NF==2{sub(/.*\./, "", $1); print $2 > $1 ".json"}' file
使用您的示例数据,它将创建3个.json
个文件:
yet_another_filename.json
other_filename.json
filename.json