Using sed to remove words with common prefix

I'm trying to extract information from source code to create an API for others to use. I can grep the file to get a list of variables with common signatures, but some variables are polymorphic, so I can't clean them out as nicely.

For example:

public static Foo bar = new Foo(123, "Bar");
public static Foo baz = new Foo(222, "Baz");
public static FooBar fBar = new FooBar(135, "Foo", "Bar");
public static FooBaz fBaz = new FooBaz(256, "Baz", "Badger", "Baz");

I would like to simplify down to:

bar    123    Bar
baz    222    Baz
fBar   135    Bar
fBaz   256    Baz

Currently, I've done this so far:

grep "public static Foo" file.java |  tr '(' ' ' | tr ')' ' ' | sed "s/public\ static\ //g"

Which gives me this:

Foo bar = new Foo 123, "Bar" ;
Foo baz = new Foo 222, "Baz" ;
FooBar fBar = new FooBar 135, "Foo", "Bar" ;
FooBaz fBaz = new FooBaz 256, "Baz", "Badger", "Baz" ;

When I try to chain it with sed "s/Foo*\ //g", it doesn't remove the words FooBar and FooBaz. How can I come about correcting this? Or is there a more elegant way to achieve what I want to do?

Answers


I came up with an awk on-liner:

awk -F'\\s*=[^(]*\\(|,\\s*"|"\\);' '{x=split($1,a," +"); print a[x], $2, $(NF-1)}' file

with your example:

kent$  cat file
public static Foo bar = new Foo(123, "Bar");
public static Foo baz = new Foo(222, "Baz");
public static FooBar fBar = new FooBar(135, "Foo", "Bar");
public static FooBaz fBaz = new FooBaz(256, "Baz", "Badger", "Baz");

kent$  awk -F'\\s*=[^(]*\\(|,\\s*"|"\\);' '{x=split($1,a," +"); print a[x], $2, $(NF-1)}' file
bar 123 Bar
baz 222 Baz
fBar 135 Bar
fBaz 256 Baz

Need Your Help

Html canvas scroll

javascript html

I have a web page with a single element: a canvas of 3000x3000 and I'd like to center the browser scroll in a point (with variable coordinates) . I'm starter with html and javascript, how can I do ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.