<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: grep like functionality in Perl - what are my options? in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/grep-like-functionality-in-perl-what-are-my-options/m-p/4919435#M104271</link>
    <description>Thanks guys&lt;BR /&gt;&lt;BR /&gt;I much appreciate your input.</description>
    <pubDate>Tue, 23 Aug 2005 00:56:52 GMT</pubDate>
    <dc:creator>Daavid Turnbull</dc:creator>
    <dc:date>2005-08-23T00:56:52Z</dc:date>
    <item>
      <title>grep like functionality in Perl - what are my options?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/grep-like-functionality-in-perl-what-are-my-options/m-p/4919429#M104265</link>
      <description>I need to parse some field from a file.  I can use back tick and grep to pick off the line I want and parse it with split which works but I figger there will be better ways to do it in Perl.  (They say "There is more than one way to do it!")&lt;BR /&gt;&lt;BR /&gt;(I think my real problem is that I have been playing with shells for too many years and it is stopping me from getting into and efficient Perl mind set.)&lt;BR /&gt;&lt;BR /&gt;Conceptually I can think of a number of approches and I am seeking some advice on their relative merits.&lt;BR /&gt;&lt;BR /&gt;Consider a text file that looks a bit like this:&lt;BR /&gt;_____________&lt;BR /&gt;&lt;BR /&gt;Line 1&lt;BR /&gt;Line 2&lt;BR /&gt;Line 3&lt;BR /&gt;Line of interest:Key [field of interest]&lt;BR /&gt;line 5&lt;BR /&gt;line 6&lt;BR /&gt;_____________&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;In ksh I would grep for (say) ":Key" and use awk to extract "field of interest".  Basically this is using successive filters to extract the field of interest.&lt;BR /&gt;&lt;BR /&gt;Using built in Perl functions I could sort of do the same thing, reading every line and checking it with regular expressions for ":Key" and use split to extract "field of interest" into a variable.  To me this seems clumbsy and possibly inefficient.&lt;BR /&gt;&lt;BR /&gt;I could also read the whole file, seek to ":Key [" and extract up to "]" into my variable or just split out the variable based on the patterns.&lt;BR /&gt;&lt;BR /&gt;Producing readable code is probably more important than effientcy but Ideally I want both ;-)&lt;BR /&gt;&lt;BR /&gt;Is there a "best" way to do this?&lt;BR /&gt;&lt;BR /&gt;Are there other approaches that I might find useful.</description>
      <pubDate>Thu, 18 Aug 2005 22:53:58 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/grep-like-functionality-in-perl-what-are-my-options/m-p/4919429#M104265</guid>
      <dc:creator>Daavid Turnbull</dc:creator>
      <dc:date>2005-08-18T22:53:58Z</dc:date>
    </item>
    <item>
      <title>Re: grep like functionality in Perl - what are my options?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/grep-like-functionality-in-perl-what-are-my-options/m-p/4919430#M104266</link>
      <description>You can use perl as,&lt;BR /&gt;&lt;BR /&gt;perl -ne '$,=" ";if ( /:Key/ ) { y/[]//d;@str=(split (/\s+/))[3..5];print @str; }' &lt;FILENAME&gt;&lt;BR /&gt;&lt;BR /&gt;$, = Output Field separator to " "&lt;BR /&gt;Remote [] in input line&lt;BR /&gt;Get 3,4,5 fields and store into an arrary @str&lt;BR /&gt;print @str&lt;BR /&gt;&lt;BR /&gt;hth.&lt;/FILENAME&gt;</description>
      <pubDate>Fri, 19 Aug 2005 01:28:21 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/grep-like-functionality-in-perl-what-are-my-options/m-p/4919430#M104266</guid>
      <dc:creator>Muthukumar_5</dc:creator>
      <dc:date>2005-08-19T01:28:21Z</dc:date>
    </item>
    <item>
      <title>Re: grep like functionality in Perl - what are my options?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/grep-like-functionality-in-perl-what-are-my-options/m-p/4919431#M104267</link>
      <description>You can use awk as,&lt;BR /&gt;&lt;BR /&gt;# awk '/:Key/ { split($4,a,"[");split($6,b,"]");print a[2]" "$5" "b[1] }' &lt;FILENAME&gt;&lt;BR /&gt;&lt;BR /&gt;or &lt;BR /&gt;&lt;BR /&gt;with awk + tr as,&lt;BR /&gt;&lt;BR /&gt;# awk '/:Key/ { print $4" "$5" "$NF }' &lt;FILENAME&gt; | tr -d '[]'&lt;BR /&gt;&lt;BR /&gt;hth.&lt;/FILENAME&gt;&lt;/FILENAME&gt;</description>
      <pubDate>Fri, 19 Aug 2005 01:30:01 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/grep-like-functionality-in-perl-what-are-my-options/m-p/4919431#M104267</guid>
      <dc:creator>Muthukumar_5</dc:creator>
      <dc:date>2005-08-19T01:30:01Z</dc:date>
    </item>
    <item>
      <title>Re: grep like functionality in Perl - what are my options?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/grep-like-functionality-in-perl-what-are-my-options/m-p/4919432#M104268</link>
      <description>You can as well as use sed to this as,&lt;BR /&gt;&lt;BR /&gt;# sed -e '/:Key/!d;{s/^.*\[//;s/\]$//g;!d;}' &lt;FILENAME&gt;&lt;BR /&gt;&lt;BR /&gt;or&lt;BR /&gt;&lt;BR /&gt;# sed -e '/:Key/!d;{s/^.*\[\(.*\)\]$/\1/g;!d;}' &lt;FILENAME&gt;&lt;BR /&gt;&lt;BR /&gt;hth.&lt;/FILENAME&gt;&lt;/FILENAME&gt;</description>
      <pubDate>Fri, 19 Aug 2005 01:41:46 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/grep-like-functionality-in-perl-what-are-my-options/m-p/4919432#M104268</guid>
      <dc:creator>Muthukumar_5</dc:creator>
      <dc:date>2005-08-19T01:41:46Z</dc:date>
    </item>
    <item>
      <title>Re: grep like functionality in Perl - what are my options?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/grep-like-functionality-in-perl-what-are-my-options/m-p/4919433#M104269</link>
      <description>The Perl answer is very neat and works well.  (Though I am still groping my camel book to understand it.)&lt;BR /&gt;&lt;BR /&gt;I have not looked at the awk and sed options.  (This is a small part of a range of functionality required by the script the remainder of which has Perl written all over it - hence the specific request for Perl hints.)&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Sun, 21 Aug 2005 20:02:48 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/grep-like-functionality-in-perl-what-are-my-options/m-p/4919433#M104269</guid>
      <dc:creator>Daavid Turnbull</dc:creator>
      <dc:date>2005-08-21T20:02:48Z</dc:date>
    </item>
    <item>
      <title>Re: grep like functionality in Perl - what are my options?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/grep-like-functionality-in-perl-what-are-my-options/m-p/4919434#M104270</link>
      <description>&lt;BR /&gt;One of many other perl solutions, but perhaps more into that 'other mindset':&lt;BR /&gt;&lt;BR /&gt;perl -ne 'print "$1\n" if (/Key \[(.*)\]/)' x&lt;BR /&gt;&lt;BR /&gt;In this regexpr we look for ":Key [" to get going.&lt;BR /&gt;However, the [ needs to be escaped.&lt;BR /&gt;Then we start remembering: (&lt;BR /&gt;Anything: .*&lt;BR /&gt;Untill: )&lt;BR /&gt;We see a "]": \]   (again escaped)&lt;BR /&gt;If this matches then print the first string remembered: $1&lt;BR /&gt;&lt;BR /&gt;If the field of interest is everything after the ":Key " then the solution becomes:&lt;BR /&gt;&lt;BR /&gt;perl -ne 'print "$1\n" if (/:Key\s+(.*)$/)' x&lt;BR /&gt;&lt;BR /&gt;The \s+ represents any aount of whitespace.&lt;BR /&gt;&lt;BR /&gt;hth,&lt;BR /&gt;Hein.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Hein.&lt;BR /&gt;</description>
      <pubDate>Sun, 21 Aug 2005 21:15:10 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/grep-like-functionality-in-perl-what-are-my-options/m-p/4919434#M104270</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2005-08-21T21:15:10Z</dc:date>
    </item>
    <item>
      <title>Re: grep like functionality in Perl - what are my options?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/grep-like-functionality-in-perl-what-are-my-options/m-p/4919435#M104271</link>
      <description>Thanks guys&lt;BR /&gt;&lt;BR /&gt;I much appreciate your input.</description>
      <pubDate>Tue, 23 Aug 2005 00:56:52 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/grep-like-functionality-in-perl-what-are-my-options/m-p/4919435#M104271</guid>
      <dc:creator>Daavid Turnbull</dc:creator>
      <dc:date>2005-08-23T00:56:52Z</dc:date>
    </item>
  </channel>
</rss>

