The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reading a binary file in text or ASCII format Nagendra High Level Programming 3 12-03-2008 06:11 PM
Reading a mailbox and sending new messages to a text file rsw626 Shell Programming and Scripting 1 04-17-2008 01:35 PM
Help with reading text file bilal05 UNIX for Dummies Questions & Answers 1 04-12-2008 03:46 PM
Reading from Text file..... user__user3110 Shell Programming and Scripting 0 04-11-2008 05:18 AM
reading text file jaan Shell Programming and Scripting 1 02-16-2004 08:43 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-09-2007
LiquidChild LiquidChild is offline
Registered User
  
 

Join Date: Jul 2005
Location: Belfast
Posts: 49
Reading text from a file

Guys,

I am trying to read text from a file, into arrays.

The format of the file is:

@DATABASE
femotest
@PACKAGE_SPECS
/usr/home/oracle92/sosa/scripts/test.pks
/usr/home/oracle92/sosa/scripts/rep.pks
@PACKAGE_BODIES
/usr/home/oracle92/sosa/scripts/rep.pkb
@PROCEDURES
@FUNCTIONS
@TRIGGERS
@VIEWS
@SQL_SCRIPTS


Where there can be any number of lines between each entry, I want to read these all into seperate arrays and a single variable for the database sid. I know how to grep for particular parts in the file, but its more getting the lines between them, could anyone give me some suggestions on how this can be done?

Again thanks.
  #2 (permalink)  
Old 05-09-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
The following script read all data into arrays (array name is entry name minus @) :
Code:
# Read alle ntries into arrays

entry=GARBAGE
while read record
do
   case "$record" in
      @*) entry=`expr substr "$record" 2 9999` ;;
      *) eval ${entry}[\${#${entry}[*]}]='$record' ;;
   esac
done < input_file

# Display arrays content

for array in DATABASE PACKAGE_SPECS PACKAGE_BODIES PROCEDURES FUNCTIONS TRIGGERS VIEWS SQL_SCRIPTS
do
   eval count=\${#${array}[*]}
   if [ $count -eq 0 ]
   then
      echo "${array} -> Empty"    
   else
      i=0
      while [ $i -lt $count ]
      do
         eval echo \"${array}[${i}]=\${${array}[${i}]}\"
         i=`expr $i + 1`
      done
   fi
done
Inputfile :
Code:
@DATABASE
femotest
@PACKAGE_SPECS
/usr/home/oracle92/sosa/scripts/test.pks
/usr/home/oracle92/sosa/scripts/rep.pks
@PACKAGE_BODIES
/usr/home/oracle92/sosa/scripts/rep.pkb
@PROCEDURES
procedure 1
procedure 2
@FUNCTIONS
function 1
function 2
@TRIGGERS
trigger 1
@VIEWS
view 1
view 2
@SQL_SCRIPTS
sql 1
sql 2
sql 3
Output:
Code:
DATABASE[0]=femotest
PACKAGE_SPECS[0]=/usr/home/oracle92/sosa/scripts/test.pks
PACKAGE_SPECS[1]=/usr/home/oracle92/sosa/scripts/rep.pks
PACKAGE_BODIES[0]=/usr/home/oracle92/sosa/scripts/rep.pkb
PROCEDURES[0]=procedure 1
PROCEDURES[1]=procedure 2
FUNCTIONS[0]=function 1
FUNCTIONS[1]=function 2
TRIGGERS[0]=trigger 1
VIEWS[0]=view 1
VIEWS[1]=view 2
SQL_SCRIPTS[0]=sql 1
SQL_SCRIPTS[1]=sql 2
SQL_SCRIPTS[2]=sql 3
Note: Leading and trailing spaces are not preserved.
If you want to keep them, try :
Code:
while IFS='' read record

Jean-Pierre.
  #3 (permalink)  
Old 05-09-2007
LiquidChild LiquidChild is offline
Registered User
  
 

Join Date: Jul 2005
Location: Belfast
Posts: 49
Thanks very much aigles I didn't expect the full solution posted.
  #4 (permalink)  
Old 05-09-2007
LiquidChild LiquidChild is offline
Registered User
  
 

Join Date: Jul 2005
Location: Belfast
Posts: 49
Aigles

Could you explain this section a little further?

*) eval ${entry}[\${#${entry}[*]}]='$record' ;;

and tbh the second line i am kinda lost with.

-- Edit, figured the first line out, i think!

Actually think i have figured them both out, thanks!

Last edited by LiquidChild; 05-09-2007 at 09:01 AM..
  #5 (permalink)  
Old 05-09-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
*) ... ;;
Default case -> line doesn't start with @ -> Entry data
*) eval ${entry}[\${#${entry}[*]}]='$record' ;;
Generate and execute array assigment.
For example : FUNCTIONS[${#FUNCTIONS[*]}='input line'
${#FUNCTIONS[*]} is the actual number of elements of the array. Since the index of array start with 0, it's also the value of the next array element.


Jean-Pierre.
  #6 (permalink)  
Old 05-10-2007
LiquidChild LiquidChild is offline
Registered User
  
 

Join Date: Jul 2005
Location: Belfast
Posts: 49
I have put the following:

readReleaseFile()
{
#Reads the contents of the release file

#Populate the arrays as required
init

entry=

while read record
do
case $record in
@*) entry=`expr substr $record 2 999`;;
*) eval ${entry}[\${#${entry}[*]}]=$record;;
esac
done < $releaseFile
}


But when I run it I get an error:

expr: syntax error
./release.sh[16]: [${#[*]}]=femotest: bad substitution

I know why the second line is giving the error, but the expr: syntax error I am not sure about.

I have also tried:

@*) entry=`expr substr "$record" 2 999`;;

Anyone know what syntax is wrong here?

Thanks
  #7 (permalink)  
Old 05-10-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
Quote:
expr: syntax error
./release.sh[16]: [${#[*]}]=femotest: bad substitution
This portion of code is generated by the eval command :
Code:
*) eval ${entry}[\${#${entry}[*]}]='$record' ;;
Looks like variable entry not assigned.
  • Initialize entry with a GARBAGE value to catch lines found before an @ entry.
    Code:
    entry=GARBAGE
    while read record
    do
  • the syntax of the expr command seems valid.
    Add a trace option in the readReleaseFile function, that will permit to see the expr command that is executed.
    Code:
    readReleaseFile()
    {
       set -x

Jean-Pierre.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0