<?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: Getting the third line above the pattern matching line and combine in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/getting-the-third-line-above-the-pattern-matching-line-and/m-p/4621236#M675868</link>
    <description>I believe Dennis just showed us state of the art awk scripting :-)&lt;BR /&gt;&lt;BR /&gt;Horia.</description>
    <pubDate>Wed, 21 Apr 2010 07:38:44 GMT</pubDate>
    <dc:creator>Horia Chirculescu</dc:creator>
    <dc:date>2010-04-21T07:38:44Z</dc:date>
    <item>
      <title>Getting the third line above the pattern matching line and combine</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/getting-the-third-line-above-the-pattern-matching-line-and/m-p/4621233#M675864</link>
      <description>Hi Experts,&lt;BR /&gt;&lt;BR /&gt;In the below file, i want to match "New risk found" line and once it get matched i need to get the "Server name" line which is present exactly above 2 lines from "New risk found" line and after that i need to combine both these lines and print in a single line. &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;------=_Part_22499_14758952.1271060277342&lt;BR /&gt;Content-Type: text/plain; charset=UTF-8&lt;BR /&gt;Content-Transfer-Encoding: 7bit&lt;BR /&gt;&lt;BR /&gt;Message from:&lt;BR /&gt;Server name: ng171tdsep1&lt;BR /&gt;Server IP: 10.5.21.238&lt;BR /&gt;&lt;BR /&gt;New risk found: WindowsAntivirusPro. &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;I tried using the below logic, but it just prints the current line and previous line as pasted below. My requirement is to retrieve the third line which is present above the "New risk found: WindowsAntivirusPro." line and then combine both these lines and print in a single line.&lt;BR /&gt;&lt;BR /&gt;shcscp18:/clocal/cschpov/user/t1982ra $ awk '/New/{print s "\n" $0}{s=$0}' /tmp/testing&lt;BR /&gt;&lt;BR /&gt;New risk found: WindowsAntivirusPro.&lt;BR /&gt;&lt;BR /&gt;Please advise.&lt;BR /&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;Ramkumar A.</description>
      <pubDate>Wed, 21 Apr 2010 03:22:38 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/getting-the-third-line-above-the-pattern-matching-line-and/m-p/4621233#M675864</guid>
      <dc:creator>RamkumarA</dc:creator>
      <dc:date>2010-04-21T03:22:38Z</dc:date>
    </item>
    <item>
      <title>Re: Getting the third line above the pattern matching line and combine</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/getting-the-third-line-above-the-pattern-matching-line-and/m-p/4621234#M675865</link>
      <description>Hello,&lt;BR /&gt;&lt;BR /&gt;You could do it in 2 steps:&lt;BR /&gt;&lt;BR /&gt;In first step you will get the line numbers matching the string "New risk found" (whatever)&lt;BR /&gt;&lt;BR /&gt;$STRING="New risk found" &lt;BR /&gt;grep -n "$STRING" my_file.txt&lt;BR /&gt;&lt;BR /&gt;In the second step you extract the lines that you need.&lt;BR /&gt;&lt;BR /&gt;Each line from the first step will be preceeded by line number and ':' like in&lt;BR /&gt;&lt;BR /&gt;25:New risk found&lt;BR /&gt;&lt;BR /&gt;So you need the line no. 22&lt;BR /&gt;&lt;BR /&gt;cat -n my_file.txt |grep 22 |awk '{print $3}'&lt;BR /&gt;&lt;BR /&gt;$3 will be the name of the server (without "Server Name: " - which is $2)&lt;BR /&gt;&lt;BR /&gt;Of course in your script you must read 25 (from this sample) in one variable and from that variable extract 3&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Horia.</description>
      <pubDate>Wed, 21 Apr 2010 04:59:26 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/getting-the-third-line-above-the-pattern-matching-line-and/m-p/4621234#M675865</guid>
      <dc:creator>Horia Chirculescu</dc:creator>
      <dc:date>2010-04-21T04:59:26Z</dc:date>
    </item>
    <item>
      <title>Re: Getting the third line above the pattern matching line and combine</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/getting-the-third-line-above-the-pattern-matching-line-and/m-p/4621235#M675867</link>
      <description>&lt;!--!*#--&gt;&amp;gt;I want to match "New risk found" line and once it get matched I need to get the "Server name" &lt;BR /&gt;&lt;BR /&gt;awk '&lt;BR /&gt;/Server name:/ {&lt;BR /&gt;   server=$0&lt;BR /&gt;   next&lt;BR /&gt;}&lt;BR /&gt;/New risk found:/ {&lt;BR /&gt;   print server, $0&lt;BR /&gt;}' file&lt;BR /&gt;&lt;BR /&gt;Outputs:&lt;BR /&gt;Server name: ng171tdsep1 New risk found: WindowsAntivirusPro.</description>
      <pubDate>Wed, 21 Apr 2010 06:05:19 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/getting-the-third-line-above-the-pattern-matching-line-and/m-p/4621235#M675867</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2010-04-21T06:05:19Z</dc:date>
    </item>
    <item>
      <title>Re: Getting the third line above the pattern matching line and combine</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/getting-the-third-line-above-the-pattern-matching-line-and/m-p/4621236#M675868</link>
      <description>I believe Dennis just showed us state of the art awk scripting :-)&lt;BR /&gt;&lt;BR /&gt;Horia.</description>
      <pubDate>Wed, 21 Apr 2010 07:38:44 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/getting-the-third-line-above-the-pattern-matching-line-and/m-p/4621236#M675868</guid>
      <dc:creator>Horia Chirculescu</dc:creator>
      <dc:date>2010-04-21T07:38:44Z</dc:date>
    </item>
    <item>
      <title>Re: Getting the third line above the pattern matching line and combine</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/getting-the-third-line-above-the-pattern-matching-line-and/m-p/4621237#M675870</link>
      <description>Hi Ramkumar:&lt;BR /&gt;&lt;BR /&gt;# perl -ne 'm{Server name: (.+)} and $name=$1;m{New risk} and print "$name $_"'    file&lt;BR /&gt;ng171tdsep1 New risk found: WindowsAntivirusPro.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;BR /&gt;</description>
      <pubDate>Wed, 21 Apr 2010 11:08:46 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/getting-the-third-line-above-the-pattern-matching-line-and/m-p/4621237#M675870</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2010-04-21T11:08:46Z</dc:date>
    </item>
    <item>
      <title>Re: Getting the third line above the pattern matching line and combine</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/getting-the-third-line-above-the-pattern-matching-line-and/m-p/4621238#M675871</link>
      <description>Hi Dennis,&lt;BR /&gt;&lt;BR /&gt;Thanks for your help. Its perfectly working as expected!!&lt;BR /&gt;&lt;BR /&gt;Also, thank you very much for all of the guys who gave the solution.&lt;BR /&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;Ramkumar A.</description>
      <pubDate>Wed, 21 Apr 2010 15:41:56 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/getting-the-third-line-above-the-pattern-matching-line-and/m-p/4621238#M675871</guid>
      <dc:creator>RamkumarA</dc:creator>
      <dc:date>2010-04-21T15:41:56Z</dc:date>
    </item>
    <item>
      <title>Re: Getting the third line above the pattern matching line and combine</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/getting-the-third-line-above-the-pattern-matching-line-and/m-p/4621239#M675874</link>
      <description>This does not have to be very complicated.  Straight shell would handle this:&lt;BR /&gt;&lt;BR /&gt;#! /usr/bin/sh&lt;BR /&gt;&lt;BR /&gt;IFS=:&lt;BR /&gt;&lt;BR /&gt;while read L R&lt;BR /&gt;do&lt;BR /&gt;  case ${L} in&lt;BR /&gt;&lt;BR /&gt;    "Server name")  SERVER=${R} ;;&lt;BR /&gt;&lt;BR /&gt;    "New risk found") print "$SERVER: $R" ;;&lt;BR /&gt;&lt;BR /&gt;  esac&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;With output:&lt;BR /&gt; ng171tdsep1:  WindowsAntivirusPro.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 21 Apr 2010 17:56:44 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/getting-the-third-line-above-the-pattern-matching-line-and/m-p/4621239#M675874</guid>
      <dc:creator>Bob E Campbell</dc:creator>
      <dc:date>2010-04-21T17:56:44Z</dc:date>
    </item>
  </channel>
</rss>

