Hi, I am trying to extract some directive filags in a build output file. I want to extract all the flags starting with -D and ending with a blank. For Ex:
cc -g -mcpu=800 -type -nostdinc -DFLAG1 -DFLAG2-float -fno-builtin cc -g -mcpu=800 -type -nostdinc -DFLAG1 -D_FLAG2 -fvolatile -fno-built +in -I
So my flags are -DFLAG1, -DFLAG2, -D_FLAG2. That means I pick all the flags and keep in an array where no flag repeats. For this my code is
#!/usr/local/bin/perl $file = shift @ARGV; chomp $file; open(BUILD,"$file") || die "File $file not found \n!"; @recs = <BUILD>; $count = 0; foreach(@recs) { $str = m/.\-D/; if ($str) { @flags = split(" ",$_); } foreach (@flags) { $str2 = m/^\-D/; if ($str2) { $finalflags[$count] = $_; $count++; } } } @sortflags = sort @finalflags; foreach(@sortflags) { print"$_\n"; }
But my problem is it finds repetition of flags. i.e -DFLAG1, -DFLAG2, -DFLAG!, -D_FLAG2. Finally I wrote a shell script like this:
rm out1.txt rm final.txt perl dir.pl > out1.txt #tells the flags repeated uniq -d out1.txt > final.txt #tells uniq flags uniq -u out1.txt >> final.txt
My all flags will be in final.txt which are -DFLAG1, -DFLAG2, -D_FLAG2. I know that this can be written in better manner. I tried to grep like:
if ($found = grep(/\b$_\b/,@finalflags) donot store inarray else store in array
But could not succeed. I know that code can be better than this. Can you pl. give your ideas? Thanks Ashok

In reply to Regular Expression by ashok

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.