Evaluating function to a variable?

Good Morning, everyone,

I'm working on an awk script to parse xml. I want to strip the xml wrappers and store the contained values as variables. I found a trim function on github (thank you, andrewcollins) and modified the expression to strip xml braces like this.
##Functions grabbed off of github many thanks to andrewcollins
#Added the <> to trim the xml wrappers
#OK that worked very well.  these functions need to go into my toolbox
function ltrim(s) { sub(/^[ \t\r\n<>]+/, "", s); return s }
function rtrim(s) { sub(/[ \t\r\n<>]+$/, "", s); return s }
function trim(s) { return rtrim(ltrim(s)); }
#Functions from Github

then, to test operation, I tried this:
/<.*>/ {print "container. " trim($0) 
	next
	}

Which works fine. it strips whitespace and the xml braces and prints the "unwrapped" data on stdout.

The problem comes when I try to pack the output into a variable. This:
/<.*>/  {data=trim($0) 
        print $data
	next
	}

Returns $0 without any modification. I've tried sprintf also with the same result. It seems like the function doesn't evaluate prior to packing the variable.

Has anybody run into this kind of thing?

Thank you in advance,

Polaris

Comments

  • hi,

    it shouldn't be $data to be printed, which evaluates to 0 as data is not a number (strtonum("anything") outputs 0), but data.
  • That was most helpful. Thank you very much.

    It's been awhile since I used awk and, as you mention, the "$" is only used to resolve variables from ARGV. $data evaluated to $0 because all strings degenerate to 0 when used in an arithmetic operation.

    That was an easy one to forget. I hardly ever awk, anymore :(

    It's not fixed, yet. but this is a good first step. Thank you, sir.
  • Ok, the final code:
    ##Functions grabbed off of github many thanks to andrewcollins
    #Added the <> to trim the xml wrappers
    #OK that worked very well.  these functions need to go into my toolbox
    function ltrim(s) { sub(/^[ \t\r\n<>]+/, "", s); return s }
    function rtrim(s) { sub(/[ \t\r\n<>]+$/, "", s); return s }
    function trim(s) { return rtrim(ltrim(s)); }
    
    
    /#.*/ {
    #      Ignore Comments
    	next
    	}
    /^$/ { 
    #      Ignore Blank Lines
    	next
    	}
    /<.*>/ {container = sprintf("%s", trim($0))
    # strip xml heading and place verbage in variable "conatiner"
    	next
    	}
    
    /.*/ { 
    ##WHATEVER YOU WANTED BASED ON THE <xml> HEADER...
    	next} 
    

    It works very nice if anybody's interested in some basic xml parsing for use by a shell script.
Sign In or Register to comment.