regex - Using sed to remove words with common prefix -
i'm trying extract information source code create api others use. can grep file list of variables common signatures, variables polymorphic, can't clean them out 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 simplify down to:
bar 123 bar baz 222 baz fbar 135 bar fbaz 256 baz
currently, i've done 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 try chain sed "s/foo*\ //g"
, doesn't remove words foobar , foobaz. how can come correcting this? or there more elegant way achieve want do?
i came awk on-liner:
awk -f'\\s*=[^(]*\\(|,\\s*"|"\\);' '{x=split($1,a," +"); print a[x], $2, $(nf-1)}' file
with 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
Comments
Post a Comment