PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 19, 2022

[FIXED] How to process file content differently for each line using shell script?

 November 19, 2022     bash, scripting, shell     No comments   

Issue

I have a file which has this data -

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 have to read the file and store the contents in different variables.

viewData=$(sed  '/view/,/tables/!d;/tables/q' $file|sed '$d')
tableData=$(sed  '/tables/,/end/!d;/end/q' $file|sed '$d')

echo $viewData

view:
  schema1.view1:/some-path/view1.sql
  schema2.view2:/some-path/view2.sql

echo $tableData

tables:
  schema1.table1:/some-path/table1.sql
  schema2.table2:/some-path/table2.sql
dataArray=("$viewData" "$tableData")

I need to use a for loop over dataArray so that I get all the components in 4 different variables.

Lets say for $viewData, the loop should be able to print like this -

objType=view
schema=schema1
view=view1
fileLoc=some-path/view1.sql
objType=view
schema=schema2
view=view2
fileLoc=some-path/view2.sql

I have tried sed and cut commands but that is not working properly. And I need to do this using shell script only. Any help will be appreciated. Thanks!


Solution

remark: If you add a space character between the : and / in the input then you would be able to use YAML-aware tools for parsing it robustly.

Given your sample input you, can use this awk for generating the expected blocks:

awk '
    match($0,/[^[:space:]]+:/) {
        key = substr($0,RSTART,RLENGTH-1)
        val = substr($0,RSTART+RLENGTH)
        if (i = index(key,".")) {
            print "objType=" type
            print "schema=" substr(key,1,i-1)
            print "view=" substr(key,i+1)
            print "fileLoc=" val
            printf "%c", 10
        } else
            type = key
    }
' data.txt
objType=view
schema=schema1
view=view1
fileLoc=/some-path/view1.sql

objType=view
schema=schema2
view=view2
fileLoc=/some-path/view2.sql

objType=tables
schema=schema1
view=table1
fileLoc=/some-path/table1.sql

objType=tables
schema=schema2
view=table2
fileLoc=/some-path/table2.sql



Answered By - Fravadona
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing