int foo(int a, void * b, int c);
int foo(int, void *, int);
Want to use "," as pattern to match. If "," pattern is matched then remove the previous word i.e.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
You could use lookahead (sometimes called a zero-length assertion) if the regex engine you are using supports it e.g. using perl, to match and remove any non-zero sequences of word-characters if and only if followed by
perl -pe 's/\w+(?=,)//g'
If you want to include other characters in the assertion - such as the terminating right parenthesis
int c you can add that as part of a
[...] character list
perl -pe 's/\w+(?=[,)])//g'
If you also want to remove any leading whitespace, try
perl -pe 's/\s*\w+(?=[,)])//g'
Putting it all together we get
$ echo 'int foo(int a, void * b, int c);' | perl -pe 's/\s*\w+(?=[,)])//g' int foo(int, void *, int);
Like newer versions of sed, perl can be made to operate on files 'in-place' by adding a
-i command-line switch.
This should help:
sed -e 's/[^ ]\+\([,)]\)/\1/g'
and if you want to remove this ugly spaces:
sed -e 's/ [^ ]\+\([,)]\)/\1/g'