<?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 Extracting field using perl in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/extracting-field-using-perl/m-p/3913455#M96901</link>
    <description>Hi guys,&lt;BR /&gt;&lt;BR /&gt;  I will like to get the field from a line of string, using perl. &lt;BR /&gt;say the string is this:&lt;BR /&gt;"Process_name      core kernel " and i will like to extract "core kernel from it" what is the best way to do this? Thanks in advance. &lt;BR /&gt;&lt;BR /&gt;Best regards&lt;BR /&gt;Henry&lt;BR /&gt;</description>
    <pubDate>Thu, 14 Dec 2006 21:04:07 GMT</pubDate>
    <dc:creator>Henry Chua</dc:creator>
    <dc:date>2006-12-14T21:04:07Z</dc:date>
    <item>
      <title>Extracting field using perl</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extracting-field-using-perl/m-p/3913455#M96901</link>
      <description>Hi guys,&lt;BR /&gt;&lt;BR /&gt;  I will like to get the field from a line of string, using perl. &lt;BR /&gt;say the string is this:&lt;BR /&gt;"Process_name      core kernel " and i will like to extract "core kernel from it" what is the best way to do this? Thanks in advance. &lt;BR /&gt;&lt;BR /&gt;Best regards&lt;BR /&gt;Henry&lt;BR /&gt;</description>
      <pubDate>Thu, 14 Dec 2006 21:04:07 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extracting-field-using-perl/m-p/3913455#M96901</guid>
      <dc:creator>Henry Chua</dc:creator>
      <dc:date>2006-12-14T21:04:07Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting field using perl</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extracting-field-using-perl/m-p/3913456#M96902</link>
      <description>Hi Henry:&lt;BR /&gt;&lt;BR /&gt;# echo "Process_name core kernel"|perl -nle 'print $1 if m/(core kernel)/'&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Thu, 14 Dec 2006 21:11:15 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extracting-field-using-perl/m-p/3913456#M96902</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-12-14T21:11:15Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting field using perl</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extracting-field-using-perl/m-p/3913457#M96903</link>
      <description>Thanks James, &lt;BR /&gt;&lt;BR /&gt;  But what if I want to print the field after the first field i.e. for:&lt;BR /&gt;&lt;BR /&gt;Process_name        data 1  &lt;BR /&gt;&lt;BR /&gt;This will print "data 1" &lt;BR /&gt;&lt;BR /&gt;Thanks. &lt;BR /&gt;&lt;BR /&gt;Best regards&lt;BR /&gt;Henry</description>
      <pubDate>Thu, 14 Dec 2006 21:18:53 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extracting-field-using-perl/m-p/3913457#M96903</guid>
      <dc:creator>Henry Chua</dc:creator>
      <dc:date>2006-12-14T21:18:53Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting field using perl</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extracting-field-using-perl/m-p/3913458#M96904</link>
      <description>Hi (again) Henry:&lt;BR /&gt;&lt;BR /&gt;OK, then:&lt;BR /&gt;&lt;BR /&gt;# echo "Process_namedata 1"|perl -nle 'print $2 if m/(Process_name)\s+(.+)/'&lt;BR /&gt;&lt;BR /&gt;data 1&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Thu, 14 Dec 2006 21:30:07 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extracting-field-using-perl/m-p/3913458#M96904</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-12-14T21:30:07Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting field using perl</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extracting-field-using-perl/m-p/3913459#M96905</link>
      <description>&lt;BR /&gt;If 'Process_name' has to be the first field on the line, then I would recommend indicating that with the BOL 'anchor' : "^"&lt;BR /&gt;&lt;BR /&gt;That make the match quicker, and prevents false matched in case the string appears somewhere else. No need to save 'process_name' in $1 through () either. &lt;BR /&gt;So that would give:&lt;BR /&gt;&lt;BR /&gt;echo "Process_name data 1"|perl -ne 'print $1 if m/^Process_name\s+(.+)/'&lt;BR /&gt;&lt;BR /&gt;Hein.</description>
      <pubDate>Fri, 15 Dec 2006 00:03:17 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extracting-field-using-perl/m-p/3913459#M96905</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2006-12-15T00:03:17Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting field using perl</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extracting-field-using-perl/m-p/3913460#M96906</link>
      <description>Hi guys, &lt;BR /&gt;&lt;BR /&gt; If I am reading lines from a file using perl.. how does the above applies to this?&lt;BR /&gt;&lt;BR /&gt;Regards&lt;BR /&gt;Henry</description>
      <pubDate>Fri, 15 Dec 2006 01:00:19 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extracting-field-using-perl/m-p/3913460#M96906</guid>
      <dc:creator>Henry Chua</dc:creator>
      <dc:date>2006-12-15T01:00:19Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting field using perl</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extracting-field-using-perl/m-p/3913461#M96907</link>
      <description>&lt;BR /&gt;We used the 'echo' for easy one-line demo.&lt;BR /&gt;&lt;BR /&gt;With a file you probably also need to&lt;BR /&gt;- loop&lt;BR /&gt;- remove the new-line&lt;BR /&gt;- decide whether you want a first occurence, a last or all.&lt;BR /&gt;&lt;BR /&gt;For example:&lt;BR /&gt;&lt;BR /&gt;perl -ne 'chomp; if (/^Process_name\s+(.+)/) {print $1; last}' &lt;YOUR-INPUT-FILE&gt;&lt;BR /&gt;&lt;BR /&gt;The -n requests perl to create a loop, Not printing each line. -p will loop and Print.&lt;BR /&gt;&lt;BR /&gt;The chomp is not needed for a simple display, but probably is if you are sticking the result in a variable.&lt;BR /&gt;&lt;BR /&gt;Hein.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/YOUR-INPUT-FILE&gt;</description>
      <pubDate>Sat, 16 Dec 2006 09:31:19 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extracting-field-using-perl/m-p/3913461#M96907</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2006-12-16T09:31:19Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting field using perl</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extracting-field-using-perl/m-p/3913462#M96908</link>
      <description>&lt;!--!*#--&gt;Hi Henry:&lt;BR /&gt;&lt;BR /&gt;To add to Hein's explanation a bit, consider this.&lt;BR /&gt;&lt;BR /&gt;Using the '-n' or '-p' switch tells Perl to create a loop around your code, here, the argument of the '-e' switch enclosed in single quotes.  Thus, instead of:&lt;BR /&gt;&lt;BR /&gt;# perl -nle 'chomp;print $1 if m/^Process_name\s+(.+)/' filename&lt;BR /&gt;&lt;BR /&gt;I could have written:&lt;BR /&gt;&lt;BR /&gt;# cat .findpat&lt;BR /&gt;#/usr/bin/perl&lt;BR /&gt;while (&amp;lt;&amp;gt;) {&lt;BR /&gt;    chomp;&lt;BR /&gt;    print "$1\n" if if m/^Process_name\s+(.+)/' &lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;# ./findpat filename&lt;BR /&gt;&lt;BR /&gt;I used the '-l' switch on the commandline to cause print statements to have a newline character appended automatically to the output.&lt;BR /&gt;&lt;BR /&gt;The read using "&amp;lt;&amp;gt;" is Perl's way of saying open the filename found in $ARGV and read it.  If no @ARGV arguments are supplied, STDIN is used.  Thus, using 'echo" piped to our Perl program is merely our Perl program reading STDIN.  In every case, the read loop runs until there is no more input.  A one-line 'echo' piped to our program is nothing more than a read of a file with one line.  &lt;BR /&gt;&lt;BR /&gt;If you want to read three (3) lines with a piped 'echo' you could do something as degenerate as:&lt;BR /&gt;&lt;BR /&gt;# echo "line1\nline2\nline3"| perl -ple 'chomp'&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Sat, 16 Dec 2006 11:29:34 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extracting-field-using-perl/m-p/3913462#M96908</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-12-16T11:29:34Z</dc:date>
    </item>
  </channel>
</rss>

