1

how to get only .json file names in string array to iterate over filename

Problem: I have 12 .json files at path /side/containers_automation/sc/2021-05/ This path can have different file extensions as well.

find /side/containers_automation/sc/2021-05 -type f -name "*.json"

/side/containers_automation/sc/2021-05/ci-userstory-tlm.json
/side/containers_automation/sc/2021-05/ci-userstory-spe.json
/side/containers_automation/sc/2021-05/ci-e2e-tlm.json
/side/containers_automation/sc/2021-05/ci-userstory-dm-inbound.json
/side/containers_automation/sc/2021-05/cucumber-ci-e2e-ta-dm.json
/side/containers_automation/sc/2021-05/cucumber-ci-e2e-tlm.json
/side/containers_automation/sc/2021-05/bvt-dm.json
/side/containers_automation/sc/2021-05/ci-userstory-dm-outbound-execution.json
/side/containers_automation/sc/2021-05/ci-e2e-dm.json
/side/containers_automation/sc/2021-05/cucumber-ci-userstory-tlm.json
/side/containers_automation/sc/2021-05/ci-userstory-dm-outbound.json
/side/containers_automation/sc/2021-05/uat-dm.json

want to copy just filenames of type .json and append a keyword (ex: automation) before these. ex: automation-ci-userstory-tlm automation-cucumber-ci-e2e-ta-dm automation-uat-dm etc. and store these names in a string array so that i can iterate over this.

I am new to shell scripting. Would need your help on this. Thank you. My thought ->

  1. Get file names by command

    (find /side/containers_automation/sc/2021-05 -type f -name "*.json" > conf_search)

  2. trim initial path (

    /side/containers_automation/sc/2021-05) and file format (.json)

/side/containers_automation/sc/2021-05/uat-dm.json 3. just get uat , ci-userstory-tlm, ci-userstory-tlm , put this in string array 4. iterate over this I know it is complex and lengthy solution.

  • Do the answers to [this question](https://stackoverflow.com/questions/20796200/how-to-loop-over-files-in-directory-and-change-path-and-add-suffix-to-filename/) cover what you want to do? – Gordon Davisson Jun 10 '21 at 20:22

1 Answers1

2

You just want all the JSON files in a particular directory in an array, minus the path and adding automation- to the beginning of the name?

Easy to do with bash parameter substitution to manipulate the elements of an array that starts out as the full filenames:

# Populate the array with the full paths to the files
filenames=( /side/containers_automation/sc/2021-05/*.json )
# Remove the path from each element
filenames=( "${filenames[@]##*/}" )
# Remove the .json extension from each element
filenames=( "${filenames[@]/%.json}" )
# Prepend automation- to each element
filenames=( "${filenames[@]/#/automation-}" )
# And show the final results for the sake of the example
declare -p filenames

Alternatively, using an explicit loop to add one file at a time to the array, and basename(1) to strip the paths and extension in a single go instead of two steps:

declare -a filenames
for file in /side/containers_automation/sc/2021-05/*.json; do
    filenames+=( "automation-$(basename "$file" .json)" )
done
declare -p filenames
Shawn
  • 47,241
  • 3
  • 26
  • 60