0

It's the next part of another problem I have just yesterday Awk doesn't match all match all my entries. I want to replace awk with perl and my first goal is to extract my declarations of functions in a .c and put it in a .h. So I came up with this :

cat dict3.c | perl -ne '/([a-z_*]+[[:space:]]+[a-z_*]+[[:space:]]*\(.*?\))/i; print $1 . ";\n"'

But the output is that :

    ;
;
;
;
;
;
;
;
dictent_t* dictentcreate(const char * key, const char * val);
dictent_t* dictentcreate(const char * key, const char * val);
dictent_t* dictentcreate(const char * key, const char * val);
dictent_t* dictentcreate(const char * key, const char * val);
dictent_t* dictentcreate(const char * key, const char * val);
dictent_t* dictentcreate(const char * key, const char * val);
dictent_t* dictentcreate(const char * key, const char * val);
dictent_t* dictentcreate(const char * key, const char * val);
dictent_t* dictentcreate(const char * key, const char * val);
dictent_t* dictentcreate(const char * key, const char * val);
dictent_t* dictentcreate(const char * key, const char * val);
dictent_t* dictentcreate(const char * key, const char * val);
dictent_t* dictentcreate(const char * key, const char * val);
dictent_t* dictentcreate(const char * key, const char * val);
dictent_t* dictentcreate(const char * key, const char * val);
dictent_t* dictentcreate(const char * key, const char * val);
dictent_t* dictentcreate(const char * key, const char * val);
dictent_t* dictentcreate(const char * key, const char * val);
dict_t* dictcreate();
dict_t* dictcreate();
dict_t* dictcreate();
dict_t* dictcreate();
dict_t* dictcreate();
dict_t* dictcreate();
dict_t* dictcreate();
dict_t* dictcreate();
dict_t* dictcreate();
dict_t* dictcreate();
dict_t* dictcreate();
dict_t* dictcreate();
dict_t* dictcreate();
dict_t* dictcreate();
dict_t* dictcreate();
dict_t* dictcreate();
void dictdestroy(*dict_t d);
void dictdestroy(*dict_t d);
void dictdestroy(*dict_t d);
void dictdestroy(*dict_t d);
void dictdestroy(*dict_t d);
void dictdestroy(*dict_t d);
void dictdestroy(*dict_t d);
void dictdestroy(*dict_t d);
void dictdestroy(*dict_t d);
void dictdestroy(*dict_t d);
void dictdestroy(*dict_t d);
void dictdestroy(*dict_t d);
void dictdestroy(*dict_t d);
void dictdestroy(*dict_t d);
void dictdestroy(*dict_t d);
void dictdestroy(*dict_t d);
void dictdump(dict_t *d);
void dictdump(dict_t *d);
void dictdump(dict_t *d);
void dictdump(dict_t *d);
void dictdump(dict_t *d);
void dictdump(dict_t *d);
void dictdump(dict_t *d);
void dictdump(dict_t *d);
void dictdump(dict_t *d);
void dictdump(dict_t *d);
void dictdump(dict_t *d);
void dictdump(dict_t *d);
int dictlook(dict_t *d, const char * key);
int dictlook(dict_t *d, const char * key);
int dictlook(dict_t *d, const char * key);
int dictlook(dict_t *d, const char * key);
int dictlook(dict_t *d, const char * key);
int dictlook(dict_t *d, const char * key);
int dictlook(dict_t *d, const char * key);
int dictlook(dict_t *d, const char * key);
int dictlook(dict_t *d, const char * key);
int dictlook(dict_t *d, const char * key);
int dictlook(dict_t *d, const char * key);
int dictlook(dict_t *d, const char * key);
int dictlook(dict_t *d, const char * key);
int dictlook(dict_t *d, const char * key);
int dictget(char* s, dict_t *d, const char *key);
int dictget(char* s, dict_t *d, const char *key);
int dictget(char* s, dict_t *d, const char *key);
int dictget(char* s, dict_t *d, const char *key);
int dictget(char* s, dict_t *d, const char *key);
int dictget(char* s, dict_t *d, const char *key);
int dictget(char* s, dict_t *d, const char *key);
int dictget(char* s, dict_t *d, const char *key);
int dictget(char* s, dict_t *d, const char *key);
int dictget(char* s, dict_t *d, const char *key);
int dictget(char* s, dict_t *d, const char *key);
int dictget(char* s, dict_t *d, const char *key);
int dictget(char* s, dict_t *d, const char *key);
int dictget(char* s, dict_t *d, const char *key);
int dictget(char* s, dict_t *d, const char *key);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t* dictadd(dict_t* d, const char * key, const char * val);
dict_t dictup(dict_t d, const char * key, const char * newval);
dict_t dictup(dict_t d, const char * key, const char * newval);
dict_t dictup(dict_t d, const char * key, const char * newval);
dict_t dictup(dict_t d, const char * key, const char * newval);
dict_t dictup(dict_t d, const char * key, const char * newval);
dict_t dictup(dict_t d, const char * key, const char * newval);
dict_t dictup(dict_t d, const char * key, const char * newval);
dict_t dictup(dict_t d, const char * key, const char * newval);
dict_t dictup(dict_t d, const char * key, const char * newval);
dict_t dictup(dict_t d, const char * key, const char * newval);
dict_t dictup(dict_t d, const char * key, const char * newval);
dict_t dictup(dict_t d, const char * key, const char * newval);
dict_t dictup(dict_t d, const char * key, const char * newval);
dict_t dictup(dict_t d, const char * key, const char * newval);
dict_t dictup(dict_t d, const char * key, const char * newval);
dict_t dictup(dict_t d, const char * key, const char * newval);
dict_t dictup(dict_t d, const char * key, const char * newval);
dict_t dictup(dict_t d, const char * key, const char * newval);
dict_t dictup(dict_t d, const char * key, const char * newval);
dict_t dictup(dict_t d, const char * key, const char * newval);
dict_t dictup(dict_t d, const char * key, const char * newval);
dict_t dictup(dict_t d, const char * key, const char * newval);
dict_t dictup(dict_t d, const char * key, const char * newval);
dict_t* dictrm(dict_t* d, const char * key);
dict_t* dictrm(dict_t* d, const char * key);
dict_t* dictrm(dict_t* d, const char * key);
dict_t* dictrm(dict_t* d, const char * key);
dict_t* dictrm(dict_t* d, const char * key);
dict_t* dictrm(dict_t* d, const char * key);
dict_t* dictrm(dict_t* d, const char * key);
dict_t* dictrm(dict_t* d, const char * key);
dict_t* dictrm(dict_t* d, const char * key);
dict_t* dictrm(dict_t* d, const char * key);
dict_t* dictrm(dict_t* d, const char * key);
dict_t* dictrm(dict_t* d, const char * key);
dict_t* dictrm(dict_t* d, const char * key);
dict_t* dictrm(dict_t* d, const char * key);
dict_t* dictrm(dict_t* d, const char * key);
dict_t* dictrm(dict_t* d, const char * key);
dict_t* dictrm(dict_t* d, const char * key);
dict_t* dictrm(dict_t* d, const char * key);
dict_t* dictrm(dict_t* d, const char * key);
dict_t* dictrm(dict_t* d, const char * key);
dict_t* dictrm(dict_t* d, const char * key);
dict_t* dictrm(dict_t* d, const char * key);
dict_t* dictrm(dict_t* d, const char * key);
dict_t* dictrm(dict_t* d, const char * key);

Whereas there's just one declaration for each function in the file. I have no clue what it could be because the number of repetitions is not the same for each printed line so it can't be just one loop...

Community
  • 1
  • 1
  • Is that the output or is that the input, or a combination of both ? –  Oct 15 '15 at 19:39

1 Answers1

4

Because you're not making the print command conditional upon matching the regex. Try this:

perl -ne '/([\w*]+\s+[\w*]+\s*\(.*?\))/i and print $1 . ";\n"' dict3.c
# .......................................^^^

For every line, you were printing the matched text from the last successful match.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Ok, as I am new too perl I didn't know that syntax, but now I have the Llama with me ! Thank you – Nicolas Scotto Di Perto Oct 15 '15 at 20:04
  • @NicolasScottoDiPerto - You probably don't need to learn a lot for this. It's just a shorthand way of saying `if ( $_ =~ /.../ ) { print $1,"\n";}` –  Oct 15 '15 at 20:42