Reading text from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading text from a file
# 1  
Old 05-09-2007
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  
Old 05-09-2007
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  
Old 05-09-2007
Thanks very much aigles I didn't expect the full solution posted.
# 4  
Old 05-09-2007
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 10:01 AM..
# 5  
Old 05-09-2007
*) ... ;;
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  
Old 05-10-2007
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  
Old 05-10-2007
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

How to write in other language in text/xml file by reading english text/xml file using C++?

Hello Team, I have 2 files.one contains english text and another contains Japanese. so i have to read english text and replace the text with Japanesh text in third file. Basically, I need a help to write japanese language in text/xml file.I heard wstring does this.Not sure how do i write... (2 Replies)
Discussion started by: SA_Palani
2 Replies

2. UNIX for Dummies Questions & Answers

Reading Xml file and print the values into the text file in columnwise?

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (4 Replies)
Discussion started by: sravanreddy
4 Replies

3. Shell Programming and Scripting

Reading colums from a text file

Hi all, I have a text file that has (4) columns. There are about 300 lines on this file. It is a plain text file. I am looking to write a simple script that will read each line from the file and generate another text file. The file looks something like this: These are the columns: ... (4 Replies)
Discussion started by: adamw
4 Replies

4. Shell Programming and Scripting

Reading the text file for particular format

Hi All, Need your help!! I have particular host file with below format: 172.34.45.67 Host1 Host2 134.45.56.67 Host3 Host4 Host5 I need shell script snippet which read this file and change the format of the file to the below format 172.34.45.67 Host1 172.34.45.67 ... (9 Replies)
Discussion started by: sharsour
9 Replies

5. Shell Programming and Scripting

Reading a value from another text file

I am having a text file best = 100 genre = 75 group = 53 . . and so on I need to read those values (100,75,53,...) from my shell script. I have to do the same function for all the variables. So in my script i used for loop to do the same. for { a=best b=100 } Video tutorial on... (3 Replies)
Discussion started by: kishorekumar87
3 Replies

6. Programming

reading a text file in c++

hello all , im trying to read a text file and display its contents. While i got the code running and the output was displayed perfectly for sometime , i started getting Abort(core dump) error . Am i missing something here ? im using HP-UX. #include <iostream.h> #include <fstream.h> #include... (1 Reply)
Discussion started by: vishy_85
1 Replies

7. Shell Programming and Scripting

Help with Reading Text from file

Hello, I am having a tough time reading data from an input text file. The input file has lines of data which is comma seperated. Each line represents a record/row, but unfortunately the data in the individual column/fields have spaces and `cat filename`is not returning entire line ( to read... (4 Replies)
Discussion started by: yajaykumar
4 Replies

8. UNIX for Dummies Questions & Answers

Help with reading text file

How can i have a while loop as follows while read inputline do <task> done < name_list and also store the values (delimited) on each line to temp variables so as to print them on screen as follows while read inputline do set name | cut -d "," -f1 name_list # #i know this is not... (1 Reply)
Discussion started by: bilal05
1 Replies

9. Shell Programming and Scripting

Reading from Text file.....

Hi Im MZ.... please help me with my requirements..... Requirement: I have a text file named information.txt which contains information about Oracle Instances, I want to fetch data's from that text file and want to display an output using shell script. Explanation : i.e. when I execute that... (0 Replies)
Discussion started by: user__user3110
0 Replies

10. Shell Programming and Scripting

reading text file

I have a text file with 50 munbers. I wanna read these numbers and append "@yahoo.com" and send emails to them using shell scripting.......... How do i read the inetegres from the text file. (1 Reply)
Discussion started by: jaan
1 Replies
Login or Register to Ask a Question