0

I want to create json holding file name, size and version of files inside a directory. Here version number is a string written inside every file (each file is a lua file containing function like

function M.version()
    return "1.0"
end

will tel the version number) I need a bash script that will create json consisting of file name, size and it's corresponding version(taken from inside the file)

eg: I've some lua files, at "/home/anudeep/source" are afile.lua bfile.lua,cfile.lua,dfile.lua,efile.lua,ffile.lua,gfile.lua and inside every lua file there's version function like

function M.version() return "1.0" end

or

function gfile.version() return "1.0" end

my json file shuld be file_info.json containing

{"avaible_files":
   [
      ["afile.lua",2056,"1.0"],
      ["bfile.lua",948,"2.0"],
      ["cfile.lua",1054,"1.0.1"],
      ["dfile.lua",3085,"3.0"],
      ["efile.lua",9685,"1.0.0"],
      ["efile.lua",6985,"1.0.2"],
      ["ffile.lua",9996,"3.1.0"],
      ["gfile.lua",785,"2.0.0"]
    ]
}

I don't know much about shell/bash scripting and satrted learning. In that process, tried this command to get file name and version

grep -rs -A1 version .| awk '/return/ {print $ response is ./afile.lua-"1.0"

getting this respone

./bfile.lua-"2.0" ./cfile.lua-"1.0.0.1" ./dfile.lua-"3.0" ./efile.lua-"1.0.0" ./ffile.lua-"1.0.2" ./gfile.lua-"2.0.0"

But can't able to write script to create json. Small help also appriciated thanks in advance

2
  • Your awk command doesn't appear to do what you say it does - as well, it's not clear whether you want to create the JSON structure before, during, or after the split operation. Please edit your question to clarify. May 19, 2022 at 11:28
  • needed json after spliting or before not an issue, there mentioned commands to let know what exactly I want(regarding file version as it's a data inside a file and i thought grep will let know that I'm accessing the data inside a file )
    – Abhi
    May 19, 2022 at 12:12

1 Answer 1

0

I'm still not sure exactly what you are asking for, however you could consider something like

#!/bin/bash

shopt -s nullglob

chunk=1000

for f in *.txt; do 
  size=$(stat -c %s "$f")
  parts=$(( size / chunk + (size % chunk > 0) ))
  vers=$(grep -sm1 -Po 'return\s+\K\S+' "$f")

  printf '["%s",%d,%s,%d]\n' "$f" "$size" "$vers" "$parts"

done | jq -n '{"available_files": [inputs]}'

You could incorporate the actual split command into the same loop, as split -b "$chunk" -d "$f" "$f"

2
  • Hope this edit will clarify my issue clearly and thank you for your patience
    – Abhi
    Jun 4, 2022 at 8:10
  • Hi @steeldriver thank you it's very helpfull to me and only one thing not working fine that is getting version, can't able to get version properly. There are multiple functions with return statements in every file, but I need only the string return by version function inside every file. could you please help me with this
    – Abhi
    Jul 16, 2022 at 5:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .