Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2004
    Posts
    1

    Unanswered: awk/sed delete pattern from and to

    Hi,

    Can any one help me out, My exact requirement is

    For example
    I need to read the file called abc.cpp and write it into new file called xyz.cpp while I write it should remove all comment line from abc.cpp.

    Exp :

    abc.cpp

    include <stdio.h>
    /* This is program is for
    Identify Author access level */
    void main()
    {
    int ja_Key; /* key for ALOT */
    public rePaid()
    {
    cout<<”Test Script”;
    } /* comment may come here */

    }

    In the above program

    /* */ are comment line

    My xyz.cpp program should be like that.


    include <stdio.h>
    void main()
    {
    int ja_Key;
    public rePaid()
    {
    cout<<”Test Script”;
    }
    }

    Thanks & Regards

    K.Swaminathan


    I have tryed with this command

    sed

    sed -e ':^/*:,:*e$:d' abc.cpp > xyz.cpp
    error is :
    sed: 0602-417 The label :^/*:,:*e$:d is greater than eight characters.

    awk

    $ cat RK
    stuff I do not want
    [string1]
    I just want
    these two lines
    [string2]
    more stuff I do not want
    $ awk '/^\[string1\]/,/^\[string2\]/' rk
    [string1]
    I just want
    these two lines
    [string2]

    I want to delete the content How to do that in awk

    but I want to sort out /* .. */ string

  2. #2
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138
    HI swamijc,
    try this :

    # change to the path where your file exist
    #cd
    #cd bin
    cat abc.ccp | awk ' BEGIN { FS="\n"; Comment="OFF" }
    {
    i=0
    row=""
    lang=length($1)
    while ( i <= lang )
    {
    i=i+1
    if ( substr($1,i,1) == "/" )
    {
    if ( substr($1,i+1,1) == "*" )
    {
    Comment="ON"
    i=i+2
    }
    }
    if ( substr($1,i,1) == "*" )
    {
    if ( substr($1,i+1,1) == "/" )
    {
    Comment="OFF"
    i=i+2
    }
    }
    if ( Comment=="OFF" )
    {
    row=row""substr($1,i,1)
    }
    }
    if ( row != "" )
    {
    print row > "xyz.ccp"
    }
    }'
    Greetings from germany
    Peter F.

Posting Permissions

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