I have a shell script to read the data from a YAML file and then do some processing. This is how the YAML file is -
view:
schema1.view1:/some-path/view1.sql
schema2.view2:/some-path/view2.sql
tables:
schema1.table1:/some-path/table1.sql
schema2.table2:/some-path/table2.sql
end
I want the output as -
schema:schema1
object:view1
fileloc:/some-path/view1.sql
schema:schema2
object:view2
fileloc:/some-path/view2.sql
schema:schema1
object:table1
fileloc:/some-path/table1.sql
schema:schema2
object:table2
fileloc:/some-path/table2.sql
This is how I'm reading the YAML file using the shell script -
#!/bin/bash
input=./file.yaml
viewData=$(sed '/view/,/tables/!d;/tables/q' $file|sed '1d;$d')
tableData=$(sed '/tables/,/end/!d;/end/q' $file|sed '1d;$d')
so viewData will have this data -
schema1.view1:/some-path/view1.sql
schema2.view2:/some-path/view2.sql
and tableData will have this data -
schema1.table1:/some-path/table1.sql
schema2.table2:/some-path/table2.sql
And then I'm using a for loop to separate the schema, object and SQL file -
for line in $tableData; do
field=`echo $line | cut -d: -f1`
schema=`echo $field | cut -d. -f1`
object=`echo $field | cut -d. -f2`
fileLoc=`echo $line | cut -d: -f2`
echo "schema=$schema"
echo "object=$object"
echo "fileloc=$fileLoc"
done
But I'll have to do the same thing again for the view. Is there any way in shell script like using an array or something else so that I can use the same loop to get data for both view and tables.
Any help would be appreciated. Thanks!