Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2004
    Posts
    70

    Unanswered: Shell Script to parse a file

    Any chance someone knows of a simple example of a unix shell script that parses a simple input file?

    I am given an input file that looks like this:
    ---------
    label1 = somedata
    label2 = moredata
    ... a bunch more of the same format
    label9 = evenmoredata
    SpecialLabel = DataLabel
    dataR1C1 dataR1C2 dataR1C3 dataR1C4 dataR1C5
    dataR2C1 dataR2C2 dataR2C3 dataR2C4 dataR2C5
    dataR3C1 dataR3C2 dataR3C3 dataR3C4 dataR3C5
    dataR4C1 dataR4C2 dataR4C3 dataR4C4 dataR4C5
    dataR5C1 dataR5C2 dataR5C3 dataR5C4 dataR5C5
    dataR6C1 dataR6C2 dataR6C3 dataR6C4 dataR6C5
    ---------

    What I have to do is call another shell script and pass in the data in that file as arguements.

    What I was thinking of doing is reading in each label and data set and storing the data into a variable then calling the other shell script and passing it that $variable. but I have no idea how to do that.
    The only tricky part, well to me, is the "SpecialLabel = DataLabel" just means the format changes from "label = data" to that table. The only thing I need from that table is the third column. If I do awk ' {print $3 }' input.file then I get all the data I need since it just gets the third arguement for each line, but how do I put all that data into variables?
    I have to take each value in the third column and turn it into a single string delimited by commas, im guessing something like this: nawk -v RS='' -v OFS="," '$1=$1' input.inp, but that just does the whole file.
    Well one more tricky part is I will only need some of the "label = data" lines depending on what label really is. So if label = "SERVER" i want to set the SERVER variable to the data. The order is never guaranteed either =\
    Last edited by JamesAvery22; 11-15-04 at 16:30.

  2. #2
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    Quote Originally Posted by JamesAvery22
    Any chance someone knows of a simple example of a unix shell script that parses a simple input file?

    So say the file looks like this:
    label1=data1
    label2=data2
    ...


    I just need to get data into a variables to pass to another shell script. I have to call that other script within my script and pass the data via arguements.
    say your file is var.txt and it contains
    label1=data1
    label2=data2
    and your script is avery.ksh and you call other.ksh script with arguments l1 and l2
    Code:
    #!/bin/ksh
    
    echo "whatever is here"
    
    # 'source' the content of the 'var.txt' - all the  variable definitions from 
    # var.txt become available as variables in the context of 'avery.ksh' 
    # invokation.
    . var.txt
    
    other.ksh -l1 "${label1}" -l2 "${label2}"
    something like that?
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  3. #3
    Join Date
    Jan 2004
    Posts
    70
    Quote Originally Posted by vgersh99
    say your file is var.txt and it contains


    and your script is avery.ksh and you call other.ksh script with arguments l1 and l2
    Code:
    #!/bin/ksh
    
    echo "whatever is here"
    
    # 'source' the content of the 'var.txt' - all the  variable definitions from 
    # var.txt become available as variables in the context of 'avery.ksh' 
    # invokation.
    . var.txt
    
    other.ksh -l1 "${label1}" -l2 "${label2}"
    something like that?
    That makes it very easy... Guessing it doesnt matter there are sometimes spaces and/or tabs between "label = data?" and I just edited my post a lot =\ how do I deal with that table?

    Thanks a lot btw.

    edit---

    also, the name of the file is given into my shell script as an argument, can I just do ". $1" if the name of the file is the first argument instead of ". var.txt"?
    Last edited by JamesAvery22; 11-15-04 at 16:44.

  4. #4
    Join Date
    Jan 2004
    Posts
    70
    Hrmmm the ". input.inp" didnt work. It just says "not found" for every line.

    So for this line : label1 = data

    it says "label1: not found"

  5. #5
    Join Date
    Jan 2004
    Posts
    70
    If someone could help me out Id appreciate it. What I wanted to do was loop through the input.inp file line by line like this

    Code:
    while [ true ]
    	do
    	read X#read a line
    	if [ $? -ne 0 ] #Break if EOF
    		then
    		break 
    	fi
    	export testcase=`echo $X | cut -f3-3 -d" "`
    	if [ $testcase == "DataLabel" ] #Break if EOF or you hit SpecialLabel = DataLabel, DataLabel is a special word, so if the third word is DataLabel then stop
    		then
    		break 
    	fi
    	echo export $X
    done < $1 
    
    echo $0: Executing command: other.cmd "${JOBNUMBER}" "${SERVER}" "${DATABASE}" "${USERID}" "${PASSWORD}" "${ENVIR}" "${REPORTNAME}" "${REPORTTITLE}" "${PAYDATE}"
    How can I remove whitespace from $X?

    And I still have no idea how to handle the table part.

  6. #6
    Join Date
    Jan 2004
    Posts
    70
    Well I got it working pretty much:

    Code:
    #!/bin/ksh
    
    export REMICDATA='notfound' 
    export serials='NULL' 
    
    while read line 
    	do
    	if [ $? -ne 0 ] #Break if EOF 
    		then
    		break 
    	fi
    	export testremic=`echo $line | tr -d " \t"i`
    	if [ $testremic == "DATA=REMIC" ] #Break if you hit DATA = REMIC
    		then
    		REMICDATA="found" 
    	fi
    	if [ $REMICDATA == "found" ]
    		then
    		if [ $testremic != "DATA=REMIC" ] 
    			then
    			if [ $serials == "NULL" ] 
    				then
    				export serials=`echo $line | cut -f3-3 -d" "` 
    			else
    				export serials=$serials,`echo $line | cut -f3-3 -d" "`
    			fi
    		fi
    	else
    		export exportstring=`echo $line | tr -d " \t"i` 
    		export $exportstring
    	fi
    done < $1 
    
    
    echo $0: Executing command: other.cmd "${JOBNUMBER}" "${SERVER}" "${DATABASE}" "${USERID}" "${PASSWORD}" "${ENVIR}" "${REPORTNAME}" "${REPORTTITLE}" "${PAYDATE}" ${serials}
    But it goes in an infinite loop if I dont pass the file name, how can I check to make sure a file is there and I can open it?

    And this thing is verrry slow. Im guessing I shouldnt be doing that loop or something. If someone could please point me towards the correct way on doing this I would greatly appreciate it. Thanks
    Last edited by JamesAvery22; 11-16-04 at 12:47.

  7. #7
    Join Date
    Jan 2004
    Posts
    70
    Would someone mind looking at my while loop? I cant see anything wrong with it and its taking forever. Are loops typically a bad thing in shell scripting?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •