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. a, b & c.

  • 4
    int c doesn't have a ,. – cuonglm Apr 27 '14 at 15:59

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 ) after 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'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.