Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.os.linux.misc > #4089 > unrolled thread

sed - please help me

Started bySzyk <szyk100@o2.pl>
First post2012-02-03 21:56 +0100
Last post2012-02-05 11:07 -0200
Articles 5 — 3 participants

Back to article view | Back to comp.os.linux.misc


Contents

  sed - please help me Szyk <szyk100@o2.pl> - 2012-02-03 21:56 +0100
    Re: sed - please help me Baho Utot <baho-utot@invlaid.com> - 2012-02-03 17:59 -0500
    Re: sed - please help me Alexandre Aguiar <gnuhealth@asaguiar.net> - 2012-02-03 22:42 -0200
    Re: sed - please help me Szyk <szyk100@o2.pl> - 2012-02-04 15:18 +0100
      Re: sed - please help me Alexandre Aguiar <gnuhealth@asaguiar.net> - 2012-02-05 11:07 -0200

#4089 — sed - please help me

FromSzyk <szyk100@o2.pl>
Date2012-02-03 21:56 +0100
Subjectsed - please help me
Message-ID<jghhms$duc$1@mx1.internetia.pl>
Hi

I want extract class names from C++ file sources. So I think sed will 
perfeclty fit my needs. I wrote two almos identical sed commands, but 
second completly does not work. Please tell me why?

First command:
$ sed -n '/^class[[:space:]]\+[[:alpha:]]\+.*$/p' ./User.h
class RoomState
class UserBase
class UserData : public UserBase
class User : public UserBase

And similiar but with simple transformation:
$ sed -n 's/^class[[:space:]]\+\([[:alpha:]]\+\).*$/\1\n/' ./User.h
[no output]


thank you
Szyk

[toc] | [next] | [standalone]


#4091

FromBaho Utot <baho-utot@invlaid.com>
Date2012-02-03 17:59 -0500
Message-ID<6f5uv8-pqt.ln1@crazy-horse.bildanet.com>
In reply to#4089
Szyk wrote:

> Hi
> 
> I want extract class names from C++ file sources. So I think sed will
> perfeclty fit my needs. I wrote two almos identical sed commands, but
> second completly does not work. Please tell me why?
> 
> First command:
> $ sed -n '/^class[[:space:]]\+[[:alpha:]]\+.*$/p' ./User.h
                                                 ^


> class RoomState
> class UserBase
> class UserData : public UserBase
> class User : public UserBase
> 
> And similiar but with simple transformation:
> $ sed -n 's/^class[[:space:]]\+\([[:alpha:]]\+\).*$/\1\n/' ./User.h
                                                           ^
> [no output]
> 
> 
> thank you
> Szyk


Missing /p at the end

[toc] | [prev] | [next] | [standalone]


#4092

FromAlexandre Aguiar <gnuhealth@asaguiar.net>
Date2012-02-03 22:42 -0200
Message-ID<2175560.srcp5QEg08@asaguiar>
In reply to#4089
on Sex 03 Fev 2012 18:56, Szyk said to people at <alt.comp.linux>:
> And similiar but with simple transformation:
> $ sed -n 's/^class[[:space:]]\+\([[:alpha:]]\+\).*$/\1\n/' ./User.h

Not sure why this doesn't do what you want. But definetely it must be doing
what you you told it to do. :-)

However, your method assumes too much about code formatting and that may
frustrate future use of your script. Below it is how I would do it.

cat ./somefile.h | \
        # do not neglect lines with leading white spaces; these
        # can be added by automatic indentation or intentionally
        # get lines that (really) start with 'class'
        grep -e '\(^ |^\t\)*class' | \
        # now print the word class and the classname no matter
        # how much space exists between them
        awk '{ print $1 " " $2 }'

This will work with whatever combination of leading spaces and tabs and any
combination of them between words. This will *not* work in the case the
classname is not in the same line. Two sed scripts can resolve this.

cat ./somefile.h | \
        sed -e 's/^[ \t]*//;s/[ \t]*$//;/^$/d' | \
        sed -e :a -e '/class$/N; s/\n/ /; ta' | \
        awk '{ print $1 " " $2 }'

The sed scripts above do:
1. delete all leading and trailing whitespaces;
2. delete all empty lines
3. if a line ends with 'class', append a space and the next line to it

There is the possible declaration of a class in the same line where another
statement ends. Actually, a commonly seen blurring technique is to put a
big number of statements in long lines. This can be eliminated simply by
replacing every ';' with '\n' just before removal of empties. This also
removes trailing ';' that can be with the class name like in 'class X;' and
avoiding this trailing ';' is included in the final output.

cat ./somefile.h | \
        sed 's/;/\n/g' | \
        sed -e 's/^[ \t]*//;s/[ \t]*$//;/^$/d' | \
        sed -e :a -e '/class$/N; s/\n/ /; ta' | \
        awk '{ print $1 " " $2 }'

Note that you can use this same script to extract any other type
declarations. All you have to do is to subst the desired type into 'class'
in the last sed script.

The most bizarre blurring of code won't resist confessing all of its types
declarations. :-)

HTH.

Please, let me know if you find some bug or uncovered possibility.

--

Alexandre

[toc] | [prev] | [next] | [standalone]


#4103

FromSzyk <szyk100@o2.pl>
Date2012-02-04 15:18 +0100
Message-ID<jgjenh$g2l$2@mx1.internetia.pl>
In reply to#4089
Thank you all!
I found solution of my problem: Grep was needed and sed must be calling 
without -n!

$ grep -i -E "^class[[:space:]]+[[:alnum:]|_]+.*$" ./User.h|sed 
's/^class[[:space:]]\+\([[:alnum:]|_]\+\).*$/\1/'
Room_State
UserBase
UserData
User

[toc] | [prev] | [next] | [standalone]


#4117

FromAlexandre Aguiar <gnuhealth@asaguiar.net>
Date2012-02-05 11:07 -0200
Message-ID<3019514.Cjtoq2itGR@asaguiar>
In reply to#4103
on Sáb 04 Fev 2012 12:18, Szyk said to people at <alt.comp.linux>:

> I found solution of my problem: Grep was needed and sed must be calling
> without -n!

Great.

> $ grep -i -E "^class[[:space:]]+[[:alnum:]|_]+.*$" ./User.h|sed

As C and C++ are case sensitive, you may not need '-i' grep option.

--

Alexandre

[toc] | [prev] | [standalone]


Back to top | Article view | comp.os.linux.misc


csiph-web