awk - Dynamically changing RS and FS

Hi,
I am trying to write an awk program in which I dynamically change the RS and FS variables.
I don't know if this is even possible.
Basically, what I am trying to do is this (this is the method that works):

[ICODE]awk 'BEGIN{RS="*/";FS="/\\*"}{printf $1}' stam.txt | awk -F"//" '{print $1}'[/ICODE]

Meaning I am splitting the file into records separated by "/*" and the field separator is "*/".
i then print only the first field.
This actually eliminates one type of comments is C.
I now want to remove the second type of comments so I need the RS to become "newline" and the FS to become "//", I then print only the first field of each line.
The problem with this solution is that I have to go through the shell to do it, and i want it to be pure awk.

I would love any suggestions, including completely doing it differently, if someone has a better solution.

Thank you.

Comments

  • How about using the C preprocessor to remove the comments?

    (-fpreprocessed)

    What are you ultimately trying to achieve?
  • scottn wrote:
    How about using the C preprocessor to remove the comments?

    (-fpreprocessed)

    What are you ultimately trying to achieve?

    Good one :icon_cheesygrin:
    I am not actually using C, I am doing it for verilog (a less common language) which uses the same comment syntax.
    I am writing a simple script to parse verilog files for my needs.
    Since comments can include every character and keyword, I must remove them in order to be able to identify the correct syntax of the language and not comments disguised as a syntax.

    Thank you.
  • If you want to do this using a script, sed is probably your best bet:

    Consider this input:
    [ rm_comment $] cat comments.v
    
    
    // a single line comment
    
    some other text (not verilog, but ok for the test:
    
                     reg singleregister; /* a sensible comment */
    
                     reg /* a silly comment */ [10] multi ;
    
                     reg /* another silly comment */ [10] /**/multi ;
    
                     reg /*a multiline
                     comment */ what_is_going_on ;
    
    
    
                     reg /*a multiline
                     comment */ [16] /*a pathalogical
                                      comment*/ what_is_going_on ;
    
    
                     [ rm_comment $]
    
    

    and the script:
    
    [ rm_comment $] cat rmc.sed
    #!/bin/sed  -f
    
    # remove everything from // to the end of the line:
    s|//.*$||;
    
    /\/\*/{
    :loopstart
    N
    /\*\//{b loopend}
    b loopstart
    :loopend
    s|\/\*.*\*\/||g
    /\/\*/{b loopstart}
    }
    
    [ rm_comment $] 
    [ rm_comment $] chmod +x rmc.sed
    [ rm_comment $] ./rmc.sed comments.v
    
    
    
    
    some other text (not verilog, but ok for the test:
    
                     reg singleregister;
    
                     reg  [10] multi ;
    
                     reg multi ;
    
                     reg  what_is_going_on ;
    
    
    
                     reg  [16]  what_is_going_on ;
    
    
                     [ rm_comment $]
    
    

    Better late than never, I suppose.

    :S
Sign In or Register to comment.