0

Hello I am trying to write unix script/command where I have to list out all filenames from given directory with filename format string-{number}.txt(eg: filename-1.txt,filename-2.txt) from which I have to form a json object. any pointers would be helpful.

 [{
        "filenumber": "1",
        "name": "filename-1.txt"
    },
    {
        "filenumber": "2",
        "name": "filename-2.txt"
    }
 ]

In the above json file-number should be read from {number} format of the each filename

4
  • That doesn't look like valid JSON (incorrect quoting); and would you really want the numbers to be strings? Have you tried something, and where did you encounter problems? Commented Sep 27, 2021 at 9:12
  • Now the field name for the filenames is inconsistent: Should it be "url" or "name"? Commented Sep 27, 2021 at 9:33
  • Hello Sorry for the incorrect json it should be corrected now. Commented Sep 27, 2021 at 9:37
  • I am trying to use echo "[" >> test.json for dir in "${array[@]}"; do file=$(find "$dir" -maxdepth 1 -type f -iname '*.txt' | awk "NR==$i") [[ -n $file ]] && printf '%b{ "filename": "%s" }' $prefix "$file" >> test.json prefix=",\n" done echo echo "]" >> test.json Commented Sep 27, 2021 at 9:39

4 Answers 4

1

Intuitively, I'd say you can do this with jq. However, in practice I've rarely been able to achieve what I wanted with jq :-)

With some lunch break puzzling, I've come up with this beauty:

ls | jq -R '{filenumber:input_line_number, name:.}' | jq -s .

Instead of ls you could use any other command that produces a newline separated list of strings.

Sign up to request clarification or add additional context in comments.

4 Comments

is there some way I can grab filenumber from the name of the file after - for eg: filenumber as 1 for filename-1.txt
That's an entirely different problem and it shows the shortcomings of specification by example. If you want to extract a number from the filename, you need to specify exactly what happens with files without numbers, with multiple numbers, numbers written as words, etc. All that is missing from your requirements.
I always have files with that format name-{number}.txt. I want to extract that number.
Then please edit your question to reflect this. And I don't think jq will be the right tool then, you may need to write some code in a real programming language.
1

A single call to jq should suffice :

shopt -s extglob
printf "%s\0" *-+([0-9]).txt | \
    jq -sR 'split("\u0000") |
            map({filenumber:capture(".*-(?<n>.*)\\.txt").n,
                 name:.})'

Comments

1

Very easy for the command-line JSON-parser and its integrated EXPath File Module:

$ xidel -se 'array{for $x in file:list(.,false(),"*-[0-9]+.txt") return {"filenumber":extract($x,".*-(\d+)\.txt",1),"name":$x}}'
$ xidel -se '
  array{
    for $x in file:list(.,false(),"*-[0-9]+.txt")
    return {
      "filenumber":extract($x,".*-(\d+)\.txt",1),
      "name":$x
    }
  }
'

Comments

0

I have tried with multiple examples to achieve exact use case of mine and finally found this working fine exactly how I wanted Thanks

 for file in $(ls *.txt); do file_version=$(echo $file | sed 's/\(^.*-\)\(.*\)\(.txt.*$\)/\2/'); jq -n --arg name "$file_version" --arg path "$file" '{name: $name, name: $path}'; done | jq -n '.urls |= [inputs]'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.