Regex 使用sed删除带有公共前缀的单词

Regex 使用sed删除带有公共前缀的单词,regex,bash,sed,awk,Regex,Bash,Sed,Awk,我正试图从源代码中提取信息,以创建一个供其他人使用的API。我可以对文件进行grep,以获得具有公共签名的变量列表,但有些变量是多态的,因此我无法很好地清除它们 例如: 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 Fo

我正试图从源代码中提取信息,以创建一个供其他人使用的API。我可以对文件进行grep,以获得具有公共签名的变量列表,但有些变量是多态的,因此我无法很好地清除它们

例如:

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$  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
我想简化为:

bar    123    Bar
baz    222    Baz
fBar   135    Bar
fBaz   256    Baz
目前为止,我已经做到了:

grep "public static Foo" file.java |  tr '(' ' ' | tr ')' ' ' | sed "s/public\ static\ //g"
这就给了我:

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" ;

当我尝试将它与
sed“s/Foo*\//g”
链接时,它不会删除FooBar和FooBaz这两个词。我怎样才能纠正这个问题呢?还是有一种更优雅的方式来实现我想做的事情?

我想你正在寻找

sed 's/Foo[A-Za-z]*//g'
在正则表达式中,
*
是一个后缀运算符,因此
Foo*
匹配
Fo
,然后再重复0次或更多次
o


要匹配“任何内容”,您可以使用
*
,但无需附加约束,这将通过字符串结尾匹配(点
是一个正则表达式元字符,它匹配除换行符以外的任何一个字符)。

以下sed脚本完成整个任务:

sed -ne '/^public static/s/.* \([^ ][^ ]*\) *= *new *[^ (]* *(\([0-9]*\),.*"\([^"]*\)"[^"]*$/\1 \2 \3/p'

我在班轮上想出了一个awk:

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

对于构造函数,您只需选择第一个和最后一个参数?太棒了,这正是我需要的!非常感谢。正确的!这是怎么逃脱我的!我真的知道这个模式!:/谢谢对我来说似乎有点复杂,而且似乎不符合示例文件:X谢谢!对不起,有一对
\(\)
太多了。更正。