Combine for loop with cat and json2html

Multi tool use
Combine for loop with cat and json2html
I try to convert json files to html files. So I can later import them to the website. My idea is run json2table to create a table and write then to the html directory. But I find not out how can redirecting the output of json2table.
for file in $(ls json/*.json); do cat $file | json2table > html/$file.html; done
But this end with
bash: html/json/26.json.html: No such file or directory
Is there someone which can help to fix the error? Thank you
Silvio
$file
html/$file.html
3 Answers
3
Your ${file}
includes the json
directory. When you don't have a directory html/json
the redirection will fail.
I think you have a subdir html
and stripping the path from ${file}
will work:
${file}
json
html/json
html
${file}
for file in json/*.json; do
cat "${file}" | json2table > html/"${file#*/}".html
# OR avoid cat with
# json2table < "${file}" > html/"${file#*/}".html
done
As a bonus I removed the ls
, added quotes for filenames with spaces and showed in a comment how to avoid cat
.
ls
cat
I suggest to replace
${file%/*}
with ${file#*/}
.– Cyrus
Jul 1 at 20:39
${file%/*}
${file#*/}
@Cyrus Good idea, tx.
– Walter A
Jul 1 at 21:25
+1 for removing
ls
but I think he needs to use something like "$(basename $file)"
instead of ${file#*/}
to get the filename only. Otherwise that's will try to create some files with html/json/{filename}.html
insteaf of html/{filename}.html
as path.– Idriss Neumann
Jul 1 at 21:43
ls
"$(basename $file)"
${file#*/}
html/json/{filename}.html
html/{filename}.html
file=json/example.json; echo "${file#*/}"
will return example.json
. The file
returned by json/*.json
will have one slash, never two. You might want to replace all json
with cat "${file} | json2table > "${file//json/html}"
, but this will fail for json/json_in_basename.json
. notpossible/example.json– Walter A
Jul 1 at 22:05
file=json/example.json; echo "${file#*/}"
example.json
file
json/*.json
json
cat "${file} | json2table > "${file//json/html}"
json/json_in_basename.json
Indeed, I tried with a subdir
json
and it works with this loop. I think it remain a lot more safer to use basename
(which is a builtin in bash) to get the filename only. Anyway, I've given +1 to this answer :p– Idriss Neumann
Jul 2 at 7:18
json
basename
I want give all the complete script if people have same problem / project or how will call it.
The first loop generates an HTML table from the JSON files and stores it in an HTML file. The second loop combines the result of the first loop with a template, so that you have a complete page. In the body tag, the include is simply searched for, deleted and filled with the table.
#!/bin/bash
jdata="json"
hdata="html"
template="tpl/tpl.html"
tmp="tmp"
# convert json to html
# https://stackoverflow.com/questions/51126226/combine-for-loop-with-cat-and-json2html/51126732#51126732
for file in $jdata/*.json;
do
# php
#php json.php $file > tmp/${file#*/}.html
# ruby
json2table < "${file}" > $hdata/"${file#*/}".html
done
# write html file
# https://unix.stackexchange.com/questions/32908/how-to-insert-the-content-of-a-file-into-another-file-before-a-pattern-marker
for file in html/*.html;
do
# extract Project Number for newfile
filename="$(basename -- "$file")"
extension="${filename#*.}"
filename="${filename%.*}"
# save the project number
NO="$(grep "No" $jdata/$filename | sed 's/[^0-9]*//g')"
OUT="$NO.html"
# write new html file
sed 's/include/$x/g' tpl/tpl.html | x="$(<$file)" envsubst '$x' > html/$OUT
done
Thank you for help & Nice day
Silvio
Your ls command returns the directory as well as the file name.
Ex. json/1.json json/2.json json/3.json
So do:
for file in $(/bin/ls json/*.json)
do
cat $file | json2table >html/$(basename $file).html
done
ls
It's a bad idea to use
ls
in a for
loop like that. Here's why.– Idriss Neumann
Jul 1 at 21:37
ls
for
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Check content of
$file
andhtml/$file.html
in your loop.– Cyrus
Jul 1 at 19:27