Awk (A spice netlist parser) 
Author Message
 Awk (A spice netlist parser)

Hi all,

        I need to write an awk program that will search for the values of
variables named "W" & "L" in a text file. The file format is

blah blah blah W = 32    L =  45
blah blah blah W=3.2    L=  85
blah blah blah W =68.654    L        =9786

The real problem is that there is an arbitrary number of spaces before
or after the '=' sign. Is there an easy way to do this?

Thank you.



Thu, 07 Jul 2005 01:54:49 GMT  
 Awk (A spice netlist parser)

Quote:
> Hi all,

>    I need to write an awk program that will search for the values of
> variables named "W" & "L" in a text file. The file format is

> blah blah blah W = 32    L =  45
> blah blah blah W=3.2    L=  85
> blah blah blah W =68.654    L        =9786

> The real problem is that there is an arbitrary number of spaces before
> or after the '=' sign. Is there an easy way to do this?

> Thank you.

awk '/W *= */ || /L *= */ { print; }' FILENAME


Thu, 07 Jul 2005 03:27:03 GMT  
 Awk (A spice netlist parser)

Quote:


> > Hi all,

> >       I need to write an awk program that will search for the values of
> > variables named "W" & "L" in a text file. The file format is

> > blah blah blah W = 32    L =  45
> > blah blah blah W=3.2    L=  85
> > blah blah blah W =68.654    L        =9786

> > The real problem is that there is an arbitrary number of spaces before
> > or after the '=' sign. Is there an easy way to do this?

> > Thank you.

> awk '/W *= */ || /L *= */ { print; }' FILENAME

Cool, but this prints all the line, I want it to pritn only the values
of W and L, that is :

32 45
3.2 85
68.654 9786



Thu, 07 Jul 2005 19:09:30 GMT  
 Awk (A spice netlist parser)

Quote:

> > Hi all,

> > I need to write an awk program that will search for the values of
> > variables named "W" & "L" in a text file. The file format is

> > blah blah blah W = 32    L =  45
> > blah blah blah W=3.2    L=  85
> > blah blah blah W =68.654    L        =9786

> > The real problem is that there is an arbitrary number of spaces
before
> > or after the '=' sign. Is there an easy way to do this?

> > Thank you.

> awk '/W *= */ || /L *= */ { print; }' FILENAME

I think what Ahmad wanted were the values, not just the line.
In the example W and L appear on all lines.  It's easy to do
in sed (less easy in most awks since capturing of text using
backreferences isn't supported in REs).

Assuming that W and L are always in the same order on the line,
and that = doesn't appear in the "blah" parts of the line, then:

$ cat Ahmad01.txt
blah blah blah W = 32    L =  45
blah blah blah W=3.2    L=  85
blah blah blah W =68.654    L        =9786

$ sed 's/^[^=]*= *\([0-9]\+\)[^=]*= *\([0-9]\+\).*/\1 \2/' Ahmad01.txt
32 45
3 85
68 9786

$

The sed expression can be simplified if digits can't appear
in the line other than after W = or L =.

HTH
--
Peter S Tillier
"Who needs perl when you can write dc and sokoban in sed?"



Thu, 07 Jul 2005 14:19:06 GMT  
 Awk (A spice netlist parser)
Quote:



> > > Hi all,

> > > I need to write an awk program that will search for the values of
> > > variables named "W" & "L" in a text file. The file format is

> > > blah blah blah W = 32    L =  45
> > > blah blah blah W=3.2    L=  85
> > > blah blah blah W =68.654    L        =9786

> > > The real problem is that there is an arbitrary number of spaces
> before
> > > or after the '=' sign. Is there an easy way to do this?

> > > Thank you.

> > awk '/W *= */ || /L *= */ { print; }' FILENAME

> I think what Ahmad wanted were the values, not just the line.
> In the example W and L appear on all lines.  It's easy to do
> in sed (less easy in most awks since capturing of text using
> backreferences isn't supported in REs).

> Assuming that W and L are always in the same order on the line,
> and that = doesn't appear in the "blah" parts of the line, then:

> $ cat Ahmad01.txt
> blah blah blah W = 32    L =  45
> blah blah blah W=3.2    L=  85
> blah blah blah W =68.654    L        =9786

> $ sed 's/^[^=]*= *\([0-9]\+\)[^=]*= *\([0-9]\+\).*/\1 \2/' Ahmad01.txt
> 32 45
> 3 85
> 68 9786

> $

> The sed expression can be simplified if digits can't appear
> in the line other than after W = or L =.

Ooops, too early in the morning I think!  The [0-9] should be
[0-9.] - sorry.

$ sed 's/[^=]*= *\([0-9.]\+\)[^=]*= *\([0-9.]\+\).*/\1 \2/' Ahmad01.txt
32 45
3.2 85
68.654 9786

HTH
--
Peter S Tillier
"Who needs perl when you can write dc and sokoban in sed?"



Thu, 07 Jul 2005 14:36:39 GMT  
 Awk (A spice netlist parser)
In message


Quote:

>> Hi all,

>>      I need to write an awk program that will search for the values of
>> variables named "W" & "L" in a text file. The file format is

>> blah blah blah W = 32    L =  45
>> blah blah blah W=3.2    L=  85
>> blah blah blah W =68.654    L        =9786

>> The real problem is that there is an arbitrary number of spaces before
>> or after the '=' sign. Is there an easy way to do this?

>> Thank you.

>awk '/W *= */ || /L *= */ { print; }' FILENAME

Fair enough if the original poster only wants to print out the lines
containing the W and L values but what if he want to extract the values
to do something with them.

The regular expressions need to be improved if there is a possibility of
another variable name ending in W or L

/(^W| W) *= */
/(^L| L) *= */

and if both spaces and tabs can be used as separators then these become

/(^W|[ \t]W)[ \t]*=[ \t]*/
/(^L|[ \t]L)[ \t]*=[ \t]*/

So I suggest this program:-

{
  #do stuff
  if (match($0,/(^W|[ \t]W)[ \t]*=[ \t]*/)) {
    W=0+substr($0,RSTART+RLENGTH)
    print "W=" W
  }
  #do some more stuff
  if (match($0,/(^L|[ \t]L)[ \t]*=[ \t]*/)) {
    L=0+substr($0,RSTART+RLENGTH)
    print "L=" L
  }
  #yet more stuff

Quote:
}

Or alternatively:-

match($0,/(^W|[ \t]W)[ \t]*=[ \t]*/) {
  W=0+substr($0,RSTART+RLENGTH)
  print "W=" W

Quote:
}

match($0,/(^L|[ \t]L)[ \t]*=[ \t]*/) {
   L=0+substr($0,RSTART+RLENGTH)
   print "L=" L

Quote:
}

Using the sample data supplied these examples give the same output:-

C:\My Documents\Alan\Awk>awk -f WandL.awk WandL.dat
W=32
L=45
W=3.2
L=85
W=68.654
L=9786

Hope this helps
--
Alan Linton



Thu, 07 Jul 2005 20:08:27 GMT  
 Awk (A spice netlist parser)

Quote:

> So I suggest this program:-

> {
>   #do stuff
>   if (match($0,/(^W|[ \t]W)[ \t]*=[ \t]*/)) {
>     W=0+substr($0,RSTART+RLENGTH)
>     print "W=" W
>   }
>   #do some more stuff
>   if (match($0,/(^L|[ \t]L)[ \t]*=[ \t]*/)) {
>     L=0+substr($0,RSTART+RLENGTH)
>     print "L=" L
>   }
>  #yet more stuff
> }

> Or alternatively:-

> match($0,/(^W|[ \t]W)[ \t]*=[ \t]*/) {
>   W=0+substr($0,RSTART+RLENGTH)
>   print "W=" W
> }

> match($0,/(^L|[ \t]L)[ \t]*=[ \t]*/) {
>    L=0+substr($0,RSTART+RLENGTH)
>    print "L=" L
> }

Big thanks to everyone who contributed a solution to my problem,
although most solutions are sed based, and I really have to use awk
coz I need to count the number of 'W's found.

Anyway, I will concentrate on Alan's sol because it's based on awk.
Please Alan, can you explain your script (I leaned awk 5 days ago).
Also can I get the values of W and L beside themselves on one line.
Like that.
10 58
45 66

Thank you.



Fri, 08 Jul 2005 19:34:52 GMT  
 Awk (A spice netlist parser)


Quote:

>> So I suggest this program:-

>> {
>>   #do stuff
>>   if (match($0,/(^W|[ \t]W)[ \t]*=[ \t]*/)) {
>>     W=0+substr($0,RSTART+RLENGTH)
>>     print "W=" W
>>   }
>>   #do some more stuff
>>   if (match($0,/(^L|[ \t]L)[ \t]*=[ \t]*/)) {
>>     L=0+substr($0,RSTART+RLENGTH)
>>     print "L=" L
>>   }
>>  #yet more stuff
>> }

>> Or alternatively:-

>> match($0,/(^W|[ \t]W)[ \t]*=[ \t]*/) {
>>   W=0+substr($0,RSTART+RLENGTH)
>>   print "W=" W
>> }

>> match($0,/(^L|[ \t]L)[ \t]*=[ \t]*/) {
>>    L=0+substr($0,RSTART+RLENGTH)
>>    print "L=" L
>> }

>Big thanks to everyone who contributed a solution to my problem,
>although most solutions are sed based, and I really have to use awk
>coz I need to count the number of 'W's found.

>Anyway, I will concentrate on Alan's sol because it's based on awk.
>Please Alan, can you explain your script (I leaned awk 5 days ago).
>Also can I get the values of W and L beside themselves on one line.
>Like that.
>10 58
>45 66

>Thank you.

My pleasure.

Quoting from The GNU Awk User's Guide:-

match(string, regexp [, array])

The match function searches string for the longest, leftmost substring
matched by the regular expression, regexp. It returns the character
position, or index, at which that substring begins (one, if it starts at
the beginning of string). If no match is found, it returns zero.
The order of the first two arguments is backwards from most other string
functions that work with regular expressions, such as sub and gsub. It
might help to remember that for match, the order is the same as for the
~ operator: string ~ regexp.

The match function sets the built-in variable RSTART to the index. It
also sets the built-in variable RLENGTH to the length in characters of
the matched substring. If no match is found, RSTART is set to zero, and
RLENGTH to -1.

substr(string, start [, length])
This returns a length-character-long substring of string, starting at
character number start. The first character of a string is character
number one. For example, substr("washington", 5, 3) returns "ing".
If length is not present, this function returns the whole suffix of
string that begins at character number start. For example,
substr("washington", 5) returns "ington". The whole suffix is also
returned if length is greater than the number of characters remaining in
the string, counting from character start.

Here endeth the quotation - thanks Arnold.

I use match(string, regexp) to see if the current line contains a W or L
value. The return value of match(string, regexp) is non-zero (or True)
if match(string, regexp) finds that string matches regexp. The
match(string, regexp) function also sets variable RSTART and RLENGTH
which I can use to find the location of the W or L value - it starts at
the (RSTART+RLENGTH)th character in $0. Then substr($0,RSTART+RLENGTH)
returns the substring of $0 from position RSTART+RLENGTH to the end of
the line, which begins with the first character of the required value.

I calculate 0+substr($0,RSTART+RLENGTH) to force a conversion of the
substring to a number. This uses all the leading characters that make
sense as part of a number and discards the rest of the line. Awk
variables may contain either string or numeric values. if you do
arithmetic with an awk variable it will automatically be converted to a
numeric value before evaluation of the arithmetic expression.

To print W and L values on the same output line:-

{
  #do stuff
  if (match($0,/(^W|[ \t]W)[ \t]*=[ \t]*/)) {
    W=0+substr($0,RSTART+RLENGTH)
    printf "%g ", W
  }
  #do some more stuff
  if (match($0,/(^L|[ \t]L)[ \t]*=[ \t]*/)) {
    L=0+substr($0,RSTART+RLENGTH)
    printf "%g", L
  }
  print ""
  #yet more stuff

Quote:
}

This uses printf instead of print because printf does not automatically
output a newline. The print "" statement outputs a newline after the W
and L values have been printed.

C:\My Documents\Alan\Awk>awk -f WandL.awk WandL.dat
32 45
3.2 85
68.654 9786

Hope this helps
--
Alan Linton



Sat, 09 Jul 2005 06:03:50 GMT  
 Awk (A spice netlist parser)
Thanks again man. Your reply is really informative : )


Sat, 09 Jul 2005 16:06:27 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. How can i get the spice netlist from the synthesized netlist (vhdl netlist)

2. verilog netlist -> spice netlist

3. verilog to SPICE netlist conversion

4. spice netlist --> verilog conversion tool

5. Need EDIF to Spice netlist translator...Help!

6. verilog netlist parsers

7. Verilog netlist parser

8. EDIF netlist file to BLIF netlist file..

9. VHDL-netlist to Verilog-netlist Translator ?

10. New SIOD update, with -*-parser:fasl-*- and -*-parser:read-*-

11. New SIOD update, with -*-parser:fasl-*- and -*-parser:read-*-

12. SPICE vs CMM vs DSDM

 

 
Powered by phpBB® Forum Software