<?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: Help with script to extract lines from file. in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/help-with-script-to-extract-lines-from-file/m-p/5126300#M687788</link>
    <description>Hi:&lt;BR /&gt;&lt;BR /&gt;# cat ./mypl&lt;BR /&gt;#!/usr/bin/perl&lt;BR /&gt;use strict;&lt;BR /&gt;use warnings;&lt;BR /&gt;my @F;&lt;BR /&gt;my @prev;&lt;BR /&gt;my ( $sum, $first ) = ( 0, 1 );&lt;BR /&gt;while (&amp;lt;&amp;gt;) {&lt;BR /&gt;    @F = split;&lt;BR /&gt;    if ( $first == 1 ) {&lt;BR /&gt;        $first = 0;&lt;BR /&gt;        (@prev) = (@F);&lt;BR /&gt;    }&lt;BR /&gt;    if ( $F[0] eq $prev[0] ) {&lt;BR /&gt;        if ( $F[1] =~ m{\-(\d+\.\d+)} ) {&lt;BR /&gt;            $sum -= ($1);&lt;BR /&gt;        }&lt;BR /&gt;        elsif ( $F[1] =~ m{\+?(\d+\.\d+)} ) {&lt;BR /&gt;            $sum += ($1);&lt;BR /&gt;        }&lt;BR /&gt;    }&lt;BR /&gt;    else {&lt;BR /&gt;        $prev[1] = $sum;&lt;BR /&gt;        print "@prev\n";&lt;BR /&gt;        (@prev) = (@F);&lt;BR /&gt;        $sum = $F[1];&lt;BR /&gt;    }&lt;BR /&gt;}&lt;BR /&gt;END {&lt;BR /&gt;    $prev[1] = $sum;&lt;BR /&gt;    print "@prev\n";&lt;BR /&gt;}&lt;BR /&gt;1;&lt;BR /&gt;&lt;BR /&gt;...run as:&lt;BR /&gt;&lt;BR /&gt;# ./mypl file&lt;BR /&gt;&lt;BR /&gt;...using your sample data, this yields:&lt;BR /&gt;&lt;BR /&gt;# ./mypl file&lt;BR /&gt;12345678 48.4 2008-08-15UFSY705226&lt;BR /&gt;87654321 81 2008-08-15UFSY710415&lt;BR /&gt;43215678 16.15 2008-08-15UFSY710026&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
    <pubDate>Mon, 25 Aug 2008 23:55:47 GMT</pubDate>
    <dc:creator>James R. Ferguson</dc:creator>
    <dc:date>2008-08-25T23:55:47Z</dc:date>
    <item>
      <title>Help with script to extract lines from file.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/help-with-script-to-extract-lines-from-file/m-p/5126298#M687786</link>
      <description>Hi, &lt;BR /&gt;&lt;BR /&gt;Having an issue trying to figure out the following.&lt;BR /&gt;I have a file with lots of columns in it.&lt;BR /&gt;I need to capture column 130, 169, 3, 191, and 200.&lt;BR /&gt;&lt;BR /&gt;My output looks similar to this when I capture the above.&lt;BR /&gt;&lt;BR /&gt;12345678 +27.50 2008-08-15UFSY705226&lt;BR /&gt;12345678 -13.25 2008-08-15UFSY705226&lt;BR /&gt;12345678 +4.15 2008-08-15UFSY705226&lt;BR /&gt;12345678 +30.00 2008-08-15UFSY705226&lt;BR /&gt;87654321 +120.55 2008-08-15UFSY710415&lt;BR /&gt;87654321 -50.50 2008-08-15UFSY710415&lt;BR /&gt;87654321 +10.95 2008-08-15UFSY710415&lt;BR /&gt;43215678 +11.50 2008-08-15UFSY710026&lt;BR /&gt;43215678 -5.35 2008-08-15UFSY710026&lt;BR /&gt;43215678 +10.00 2008-08-15UFSY710026&lt;BR /&gt;&lt;BR /&gt;Final output is supposed to look like this.&lt;BR /&gt;The column 2 output addition is where I am stuck.&lt;BR /&gt;&lt;BR /&gt;12345678 +48.40 2008-08-15UFSY705226&lt;BR /&gt;87654321 +81.00 2008-08-15UFSY710415&lt;BR /&gt;43215678 +16.15 2008-08-15UFSY710026&lt;BR /&gt;&lt;BR /&gt;TIA&lt;BR /&gt;D. Jackson</description>
      <pubDate>Mon, 25 Aug 2008 20:51:51 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/help-with-script-to-extract-lines-from-file/m-p/5126298#M687786</guid>
      <dc:creator>D. Jackson_1</dc:creator>
      <dc:date>2008-08-25T20:51:51Z</dc:date>
    </item>
    <item>
      <title>Re: Help with script to extract lines from file.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/help-with-script-to-extract-lines-from-file/m-p/5126299#M687787</link>
      <description>What defines your "columns"?  Are they space delimited?  Delimited by some other character?&lt;BR /&gt;&lt;BR /&gt;If space delimited, something like:&lt;BR /&gt;&lt;BR /&gt;# awk '{print $130, $169, $3, $191, $200}' somefilename&lt;BR /&gt;&lt;BR /&gt;should work.</description>
      <pubDate>Mon, 25 Aug 2008 22:05:29 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/help-with-script-to-extract-lines-from-file/m-p/5126299#M687787</guid>
      <dc:creator>Patrick Wallek</dc:creator>
      <dc:date>2008-08-25T22:05:29Z</dc:date>
    </item>
    <item>
      <title>Re: Help with script to extract lines from file.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/help-with-script-to-extract-lines-from-file/m-p/5126300#M687788</link>
      <description>Hi:&lt;BR /&gt;&lt;BR /&gt;# cat ./mypl&lt;BR /&gt;#!/usr/bin/perl&lt;BR /&gt;use strict;&lt;BR /&gt;use warnings;&lt;BR /&gt;my @F;&lt;BR /&gt;my @prev;&lt;BR /&gt;my ( $sum, $first ) = ( 0, 1 );&lt;BR /&gt;while (&amp;lt;&amp;gt;) {&lt;BR /&gt;    @F = split;&lt;BR /&gt;    if ( $first == 1 ) {&lt;BR /&gt;        $first = 0;&lt;BR /&gt;        (@prev) = (@F);&lt;BR /&gt;    }&lt;BR /&gt;    if ( $F[0] eq $prev[0] ) {&lt;BR /&gt;        if ( $F[1] =~ m{\-(\d+\.\d+)} ) {&lt;BR /&gt;            $sum -= ($1);&lt;BR /&gt;        }&lt;BR /&gt;        elsif ( $F[1] =~ m{\+?(\d+\.\d+)} ) {&lt;BR /&gt;            $sum += ($1);&lt;BR /&gt;        }&lt;BR /&gt;    }&lt;BR /&gt;    else {&lt;BR /&gt;        $prev[1] = $sum;&lt;BR /&gt;        print "@prev\n";&lt;BR /&gt;        (@prev) = (@F);&lt;BR /&gt;        $sum = $F[1];&lt;BR /&gt;    }&lt;BR /&gt;}&lt;BR /&gt;END {&lt;BR /&gt;    $prev[1] = $sum;&lt;BR /&gt;    print "@prev\n";&lt;BR /&gt;}&lt;BR /&gt;1;&lt;BR /&gt;&lt;BR /&gt;...run as:&lt;BR /&gt;&lt;BR /&gt;# ./mypl file&lt;BR /&gt;&lt;BR /&gt;...using your sample data, this yields:&lt;BR /&gt;&lt;BR /&gt;# ./mypl file&lt;BR /&gt;12345678 48.4 2008-08-15UFSY705226&lt;BR /&gt;87654321 81 2008-08-15UFSY710415&lt;BR /&gt;43215678 16.15 2008-08-15UFSY710026&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Mon, 25 Aug 2008 23:55:47 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/help-with-script-to-extract-lines-from-file/m-p/5126300#M687788</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2008-08-25T23:55:47Z</dc:date>
    </item>
    <item>
      <title>Re: Help with script to extract lines from file.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/help-with-script-to-extract-lines-from-file/m-p/5126301#M687789</link>
      <description>here is what I have using your data 'pasted' into a file x and #paste x x &amp;gt; y&lt;BR /&gt;&lt;BR /&gt;$ perl -lane '$k=$F[3]; $v{$k} += $F[1]; $r{$k}=$F[2].$F[5]}{ for (sort keys %v) {print "$_ $v{$_} $r{$_}"' y&lt;BR /&gt;12345678 48.4 2008-08-15UFSY7052262008-08-15UFSY705226&lt;BR /&gt;43215678 16.15 2008-08-15UFSY7100262008-08-15UFSY710026&lt;BR /&gt;87654321 81 2008-08-15UFSY7104152008-08-15UFSY710415&lt;BR /&gt;&lt;BR /&gt;For the real file that should be&lt;BR /&gt;$ perl -lane '$k=$F[130]; $v{$k} += $F[169]; $r{$k}=$F[3].$F[191].$F[200]}{ for (sort keys %v) {print "$_ $v{$_} $r{$_}"' file&lt;BR /&gt;&lt;BR /&gt;$k = key  &lt;BR /&gt;%v = values&lt;BR /&gt;%r = rest of columns&lt;BR /&gt;&lt;BR /&gt;Many questions/suggestions though... &lt;BR /&gt;&lt;BR /&gt;1) How should the rest of the columns be put together? just glue or 'join' with delimitor&lt;BR /&gt;&lt;BR /&gt;2) What if the other columns change? Ignore? Take the first values? Take the last values? (example) Report as issue?&lt;BR /&gt;&lt;BR /&gt;3) If the last column in the input is selected, then add a 'chomp' to the input loop&lt;BR /&gt;&lt;BR /&gt;4) How should the output be ordered? Ignore?Order of arrival? Order of key (example)? Order of summed value?&lt;BR /&gt;&lt;BR /&gt;5) The work perl has to do can be reduced by conditionalizing the storing of 'the rest' based on presence of the key in the value array.&lt;BR /&gt;&lt;BR /&gt;Enjoy,&lt;BR /&gt;Hein.&lt;BR /&gt;</description>
      <pubDate>Tue, 26 Aug 2008 03:06:37 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/help-with-script-to-extract-lines-from-file/m-p/5126301#M687789</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2008-08-26T03:06:37Z</dc:date>
    </item>
    <item>
      <title>Re: Help with script to extract lines from file.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/help-with-script-to-extract-lines-from-file/m-p/5126302#M687790</link>
      <description>&lt;!--!*#--&gt;&lt;BR /&gt;Note... in case that was not clear, JRF's example reports in order of arrival and typically requires the input to be pre-sorted or could report multiple lines with the same key. For now it does not handle selected columns for the 'rest' but that is just SMOP.&lt;BR /&gt;&lt;BR /&gt;The reason I make this comment is to highlight the particual output order.&lt;BR /&gt;&lt;BR /&gt;btw... Unless I am missing something real subbtle, it can also be simplyfied a great deal when realizing the perl will automagically do the + versus - when adding split field.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;use strict;&lt;BR /&gt;use warnings;&lt;BR /&gt;my ( @F, $sum, $key, $text, $previous_text );&lt;BR /&gt;my $previous_key = '';&lt;BR /&gt;my $format = qq(%s %6.2f %s\n);&lt;BR /&gt;while (&amp;lt;&amp;gt;) {&lt;BR /&gt;  chomp;&lt;BR /&gt;  @F = split;&lt;BR /&gt;  $key = $F[3];&lt;BR /&gt;  if ($key eq $previous_key) {&lt;BR /&gt;    $sum += $F[1];&lt;BR /&gt;  } else {&lt;BR /&gt;    printf ($format, $previous_key, $sum, $previous_text) if $previous_key;&lt;BR /&gt;    $sum  = $F[1];&lt;BR /&gt;    $previous_key = $key;&lt;BR /&gt;    $previous_text = $F[0].$F[0];&lt;BR /&gt;  }&lt;BR /&gt;}&lt;BR /&gt;printf ($format, $key, $sum, $F[0].$F[0]);&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;For non-sorted input, and still retaining the order of arrival, you might consider something much similar to my prior one-liner:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/perl&lt;BR /&gt;use strict;&lt;BR /&gt;use warnings;&lt;BR /&gt;my (@F, $key, @keys, %value, %text);&lt;BR /&gt;while (&amp;lt;&amp;gt;) {&lt;BR /&gt;  chomp;&lt;BR /&gt;  @F = split;&lt;BR /&gt;  my $key = $F[3];&lt;BR /&gt;  if (!defined $value{$key}) {&lt;BR /&gt;    push @keys, $key;&lt;BR /&gt;    $value{$key}  = $F[1];&lt;BR /&gt;    $text{$key}   = $F[2].$F[5];  # Alter!&lt;BR /&gt;  } else {&lt;BR /&gt;    $value{$key} += $F[1];&lt;BR /&gt;  }&lt;BR /&gt;}&lt;BR /&gt;for $key (@keys) {&lt;BR /&gt;  printf ("%s %6.2f %s\n", $key, $value{$key}, $text{$key});&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;Note... the field numbers XXX in $F[XXX] need to be adjusted to the actual data.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;hth,&lt;BR /&gt;Hein.&lt;BR /&gt;</description>
      <pubDate>Tue, 26 Aug 2008 03:51:51 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/help-with-script-to-extract-lines-from-file/m-p/5126302#M687790</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2008-08-26T03:51:51Z</dc:date>
    </item>
    <item>
      <title>Re: Help with script to extract lines from file.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/help-with-script-to-extract-lines-from-file/m-p/5126303#M687791</link>
      <description>&lt;!--!*#--&gt;Keeping Hein's comments in mind, you can use this awk script.  Here I have assumed you have already rearranged columns 130, 169, 3, 191, and 200.  Also, I assume that 3, 191 and 200 are the same if 130 is the same.  So it just adds up the new column 2, if the first column is the same:&lt;BR /&gt;awk '&lt;BR /&gt;BEGIN {&lt;BR /&gt;   getline # first&lt;BR /&gt;   S1 = $1&lt;BR /&gt;   S2 = $2&lt;BR /&gt;   S3 = $3&lt;BR /&gt;}&lt;BR /&gt;{&lt;BR /&gt;if ($1 == S1) {&lt;BR /&gt;   S2 += $2 # add&lt;BR /&gt;} else {&lt;BR /&gt;   printf "%s %.2f %s\n", S1, S2, S3&lt;BR /&gt;   S1 = $1 # save&lt;BR /&gt;   S2 = $2&lt;BR /&gt;   S3 = $3&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;END { printf "%s %.2f %s\n", S1, S2, S3 } ' file</description>
      <pubDate>Tue, 26 Aug 2008 05:12:47 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/help-with-script-to-extract-lines-from-file/m-p/5126303#M687791</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2008-08-26T05:12:47Z</dc:date>
    </item>
    <item>
      <title>Re: Help with script to extract lines from file.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/help-with-script-to-extract-lines-from-file/m-p/5126304#M687792</link>
      <description>Thanks to all who replied. Your input helped me in resolving my issues.&lt;BR /&gt;&lt;BR /&gt;Very much appreciated.&lt;BR /&gt;&lt;BR /&gt;D. Jackson</description>
      <pubDate>Tue, 26 Aug 2008 13:33:19 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/help-with-script-to-extract-lines-from-file/m-p/5126304#M687792</guid>
      <dc:creator>D. Jackson_1</dc:creator>
      <dc:date>2008-08-26T13:33:19Z</dc:date>
    </item>
    <item>
      <title>Re: Help with script to extract lines from file.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/help-with-script-to-extract-lines-from-file/m-p/5126305#M687793</link>
      <description>Thread Closed...</description>
      <pubDate>Tue, 26 Aug 2008 13:33:57 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/help-with-script-to-extract-lines-from-file/m-p/5126305#M687793</guid>
      <dc:creator>D. Jackson_1</dc:creator>
      <dc:date>2008-08-26T13:33:57Z</dc:date>
    </item>
  </channel>
</rss>

