Create text files dynamically from master textfile with sed

Discussion in 'Programming/Scripts' started by mangecelle, Apr 18, 2009.

  1. mangecelle

    mangecelle New Member

    Hi,

    I am trying to accomplish the following task with help of bash scripting. From a programming tool i use i can export objects to a text file. They can be exported separetely or together. The programming tool generates for me one text file with the diferent objects exported as text. The structure is the following.


    OBJECT Table 3 Table name
    {
    object properties
    }


    OBJECT Table 4 Table name
    {
    object properties
    }


    OBJECT Form 10 Form name
    {
    object properties
    }


    As you can see there can be different object types e.g. Table/Form/Codeunit etc.

    I would like to send this file to a script and have that script to generate separete files for me. The files should be named according to the object type. "Object Type"_"number".txt. So in the example above the script should generate 3 files named: Table_3.txt, Table_4.txt and Form_10.txt.

    I have been looking at for example sed to help me accomplish this. But i am stucked and don´t know how to proceede. I have looked in to the sed manual. There is a way to match a start and an end caracter.

    Code:
    #!/bin/bash
    
    FileName=$1
    
    for object in $(sed -n -e '/^OBJECT Table/,/^}/p' $FileName)
    do
      ... do stuff here
    done
    With this code how can i now that one start/end cycle has finished?

    Thanks in advance

    Mange
     
  2. cfajohnson

    cfajohnson New Member

    Use awk, not sed:

    Code:
    awk '
       BEGIN  { filename = "/dev/null" }
    /^OBJECT/ { filename = $2 "_" $3 }
              { print > filename }
    ' "$FILE"
    
     

Share This Page