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