Search code examples
regexperlparsingtext-parsingstring-parsing

Perl: print a string between quotes only on lines that match a certain regexp


EDIT: I'm not just trying to solicit people writing this code for me.....I have been trying at this for about a day, and despite having a perl textbook, I still can't figure it out. Like I said, I CAN do it using grep/awk/sed, and also by piping together multiple perl one-liners....I just can't figure out how to do it in one perl invocation.

I'm new to Perl so this one will be quick and easy to answer.

I'm trying to write a script that will parse a file line-by-line and print the first word on a line that is contained between quotes, but only on lines that contain a specific string at the EOL (string is passed as an argument). The file in a C header file that, along with lots of other junk in it, contains a struct definition in the format below, which has the strings I want to extract.

struct Foo_t foo
{

    /* str_HELLO */
    { {5,4,8,7,9},
      {     "HELLO!",      // English 
            "BONJOUR!",    // French
            "Hallo!",       // German 
            "BONJOURNO!",   // Italian
            "HOLA!"         // Spanish
    } }, 

    /* str_GOODBYE */
    { {15,3,3,3,3},
      {     "GOODBYE!",     // English 
            "AU REVOUIR!",   // French
            "TSCHUSS!",      // German 
            "CIAO!",         // Italian
            "ADIOS!"      // Spanish
    } }, 


    /* str_FOO */
    { {15,3,3,3,3},
      {     "FOO",      // English 
            "BAR",      // French
            "NOO",      // German 
            "BAZ",      // Italian
            "OOF"       // Spanish
    } },


    // lots more of these....
    // .... 

And to get the desired output, I would want the invocation to be

bash~$: myscript.pl -language=english file_to_be_parsed.h 

I can easily do this by piping together greps, but I really wanna get this in perl. I've tried to do this simple task via a script and also with a one-liner, but none of have worked. Since this is so quick, if any perl wizards could show me the light, along with an explanation for the matching stuff, it would be much appreciated.

Thanks in advance!!


Solution

  • answer from @melomene in the comments did the trick.

    I went from

    perl -wnle '/english/i and ($_ =~ /\"(\w)*\"/ and print $&);' file.h | perl -wlne 's/[",]//g and print;' 
    

    to

    perl -wnle '/english/i and ($_ =~ /\"(\w*)\"/ and print $1);` file.h