Home > Mobile >  How can I create and process an array of variables in shell script?
How can I create and process an array of variables in shell script?

Time:11-16

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!

CodePudding user response:

Using (g)awk:

awk -F "[:.]" '/:$/{ s=$1 }{ gsub(" ",""); if($3!=""){ print "schema="$1; print "object="$2; print "fileloc="$3 }}' yaml
  • -F "[:.]" reads input, and separates this on : or . (But using the regular expression [:.].)
  • /:$/{ s=$1 } This will store the group (view or tables) you are currently reading. This is not used anymore, so can be ignored.
  • gsub(" ",""); This will delete all spaced in the input line.
  • if... When you have three fields, checked by a not empty third field, print the info.

output:

schema=schema1
object=view1
fileloc=/some-path/view1
schema=schema2
object=view2
fileloc=/some-path/view2
schema=schema1
object=table1
fileloc=/some-path/table1
schema=schema2
object=table2
fileloc=/some-path/table2

EDIT: Adding the objectType to the output:

awk -F "[:.]" '/:$/{ s=$1 }{ gsub(" ",""); if($3!=""){ print "objectType="$s; "schema="$1; print "object="$2; print "fileloc="$3 }}' yaml

But I do see that I made a mistake....

  • Related