<?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: awk Separating Records in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063565#M94563</link>
    <description>Try the awk construct below:&lt;BR /&gt;&lt;BR /&gt;awk '{if($0~"^1"){if(l) print l;l=$0}else l=l","$0}END{print l}' file</description>
    <pubDate>Fri, 31 Aug 2007 16:31:48 GMT</pubDate>
    <dc:creator>Sandman!</dc:creator>
    <dc:date>2007-08-31T16:31:48Z</dc:date>
    <item>
      <title>awk Separating Records</title>
      <link>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063559#M94557</link>
      <description>Hi&lt;BR /&gt;&lt;BR /&gt;I am trying to separate records using awk.&lt;BR /&gt;&lt;BR /&gt;Below is a sample of the records in a file.&lt;BR /&gt;------------&lt;BR /&gt;1|D|hfoo01||39322|74883|service-call-master&lt;BR /&gt;2|call-no|143814&lt;BR /&gt;3|call-status|D|F&lt;BR /&gt;3|call-date-last-change|20-AUG-2007|30-AUG-2007&lt;BR /&gt;1|U|hfoo01||39322|74893|service-call-master&lt;BR /&gt;2|call-no|143814&lt;BR /&gt;3|call-user-only-num1|0.0000|23158.0000&lt;BR /&gt;1|I|hfoo01||39322|74893|job-cost-master&lt;BR /&gt;2|job-code|143814&lt;BR /&gt;3|jcm-project-manager|GNOHPH01|HFOO01&lt;BR /&gt;1|U|hfoo01||39322|74903|service-call-master&lt;BR /&gt;2|call-no|143814&lt;BR /&gt;3|call-invoice-no||S3106895&lt;BR /&gt;---------------------------&lt;BR /&gt;&lt;BR /&gt;The record separators are,&lt;BR /&gt;1|I| or 1|D| or 1|U|&lt;BR /&gt;&lt;BR /&gt;Here is my feeble attempt to do it.&lt;BR /&gt;-------------&lt;BR /&gt;% more 1.awk&lt;BR /&gt;BEGIN { FS = "\n"&lt;BR /&gt;        RS = "\/&amp;lt;1\|[IDU]" }&lt;BR /&gt;{&lt;BR /&gt;           print "New Record "$1", "$2", "$3", "$4",,"RT&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;# awk -f 1.awk audit.log&lt;BR /&gt;New Record 1|D|hfoo01||39322|74883|service-call-master, 2|call-no|143814, 3|call&lt;BR /&gt;-status|D|F, 3|call-date-last-change|20-AUG-2007|30-AUG-2007,,&lt;BR /&gt;----------------------&lt;BR /&gt;&lt;BR /&gt;Appreciate help from awk gurus on the RS variable.&lt;BR /&gt;&lt;BR /&gt;Thank you.&lt;BR /&gt;nash&lt;BR /&gt;</description>
      <pubDate>Fri, 31 Aug 2007 03:55:35 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063559#M94557</guid>
      <dc:creator>nz_1</dc:creator>
      <dc:date>2007-08-31T03:55:35Z</dc:date>
    </item>
    <item>
      <title>Re: awk Separating Records</title>
      <link>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063560#M94558</link>
      <description>&lt;!--!*#--&gt;It appears that RS can only be a single char.  Not a regexp.&lt;BR /&gt;&lt;BR /&gt;&amp;gt;I am trying to separate records using awk.&lt;BR /&gt;&lt;BR /&gt;Looks like you are joining them.&lt;BR /&gt;(You haven't defined RT?)&lt;BR /&gt;&lt;BR /&gt;Here is what I have to concatenate your lines:&lt;BR /&gt;awk '&lt;BR /&gt;BEGIN { getline; save=$0 }&lt;BR /&gt;/^1\|[IDU]\|/ {&lt;BR /&gt;   print save ",,"&lt;BR /&gt;   save=$0&lt;BR /&gt;   next&lt;BR /&gt;}&lt;BR /&gt;{&lt;BR /&gt;save=save $0 # concatenate lines&lt;BR /&gt;next&lt;BR /&gt;}&lt;BR /&gt;END { print save ",," } ' audit.log&lt;BR /&gt;</description>
      <pubDate>Fri, 31 Aug 2007 04:35:15 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063560#M94558</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2007-08-31T04:35:15Z</dc:date>
    </item>
    <item>
      <title>Re: awk Separating Records</title>
      <link>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063561#M94559</link>
      <description>Hi Nash:&lt;BR /&gt;&lt;BR /&gt;Try this:&lt;BR /&gt;&lt;BR /&gt;# perl -0377 -ne '@a=split(/(1\|I\||1\|D\||1\|U\|)/,$_);@a;for $b (@a) {print "$b\n"}' file&lt;BR /&gt;&lt;BR /&gt;...using your data, this would ouput:&lt;BR /&gt;&lt;BR /&gt;1|D|&lt;BR /&gt;hfoo01||39322|74883|service-call-master&lt;BR /&gt;2|call-no|143814&lt;BR /&gt;3|call-status|D|F&lt;BR /&gt;3|call-date-last-change|20-AUG-2007|30-AUG-2007&lt;BR /&gt;&lt;BR /&gt;1|U|&lt;BR /&gt;hfoo01||39322|74893|service-call-master&lt;BR /&gt;2|call-no|143814&lt;BR /&gt;3|call-user-only-num1|0.0000|23158.0000&lt;BR /&gt;&lt;BR /&gt;1|I|&lt;BR /&gt;hfoo01||39322|74893|job-cost-master&lt;BR /&gt;2|job-code|143814&lt;BR /&gt;3|jcm-project-manager|GNOHPH01|HFOO01&lt;BR /&gt;&lt;BR /&gt;...&lt;BR /&gt;&lt;BR /&gt;This reads your whole file into memory (slurps it) without regard to any record seperator.  It then leverages 'split' where regular expresssion patterns can be used.&lt;BR /&gt;&lt;BR /&gt;The fact that your pattern to match contains the pipe (vertical bar) and alternation uses that symbol too, means that we have to escape with backslashes making a rather ugly expression.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Fri, 31 Aug 2007 07:19:16 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063561#M94559</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-08-31T07:19:16Z</dc:date>
    </item>
    <item>
      <title>Re: awk Separating Records</title>
      <link>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063562#M94560</link>
      <description>&lt;P&gt;&amp;gt;JRF: means that we have to escape with backslashes making a rather ugly expression.&lt;BR /&gt;&lt;BR /&gt;Yep, awk needs that too.&lt;BR /&gt;My script leaves the record separators and joins the lines into a bigger record.&lt;/P&gt;</description>
      <pubDate>Sun, 25 Sep 2011 00:48:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063562#M94560</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2011-09-25T00:48:54Z</dc:date>
    </item>
    <item>
      <title>Re: awk Separating Records</title>
      <link>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063563#M94561</link>
      <description>&lt;!--!*#--&gt;Hi (again) Nash:&lt;BR /&gt;&lt;BR /&gt;&amp;gt;Dennis: My script leaves the record separators and joins the lines into a bigger record.&lt;BR /&gt;&lt;BR /&gt;OK, I missed that requirement, but that's an easy fix:&lt;BR /&gt;&lt;BR /&gt;# perl -0377 -wne 's/\012//g;@a=split(/(?=1\|I\||1\|D\||1\|U\|)/,$_);for $b (@a) {print "NEW RECORD:$b\n\n"}' audit.log&lt;BR /&gt;&lt;BR /&gt;...outputs:&lt;BR /&gt;&lt;BR /&gt;NEW RECORD:1|D|hfoo01||39322|74883|service-call-master2|call-no|1438143|call-sta&lt;BR /&gt;tus|D|F3|call-date-last-change|20-AUG-2007|30-AUG-2007&lt;BR /&gt;&lt;BR /&gt;NEW RECORD:1|U|hfoo01||39322|74893|service-call-master2|call-no|1438143|call-use&lt;BR /&gt;r-only-num1|0.0000|23158.0000&lt;BR /&gt;&lt;BR /&gt;NEW RECORD:1|I|hfoo01||39322|74893|job-cost-master2|job-code|1438143|jcm-project&lt;BR /&gt;-manager|GNOHPH01|HFOO01&lt;BR /&gt;&lt;BR /&gt;NEW RECORD:1|U|hfoo01||39322|74903|service-call-master2|call-no|1438143|call-inv&lt;BR /&gt;oice-no||S3106895&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 31 Aug 2007 08:37:22 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063563#M94561</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-08-31T08:37:22Z</dc:date>
    </item>
    <item>
      <title>Re: awk Separating Records</title>
      <link>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063564#M94562</link>
      <description>how about just doing something like this:&lt;BR /&gt;&lt;BR /&gt;awk '&lt;BR /&gt;#prints a newline before printing every line that begins with a 1&lt;BR /&gt;#expcept for the first line&lt;BR /&gt;/^1/ { if ( NR &amp;gt; 1 )&lt;BR /&gt;        printf("\n%s",$0);&lt;BR /&gt;       else&lt;BR /&gt;        printf("%s",%0);&lt;BR /&gt;       next;&lt;BR /&gt;      }&lt;BR /&gt;# on every line not beginning with a one&lt;BR /&gt;# print the whole line except the first character, but without a carriage return&lt;BR /&gt;{printf("%s",substr($0,2));}&lt;BR /&gt;'</description>
      <pubDate>Fri, 31 Aug 2007 10:04:52 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063564#M94562</guid>
      <dc:creator>larsoncu</dc:creator>
      <dc:date>2007-08-31T10:04:52Z</dc:date>
    </item>
    <item>
      <title>Re: awk Separating Records</title>
      <link>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063565#M94563</link>
      <description>Try the awk construct below:&lt;BR /&gt;&lt;BR /&gt;awk '{if($0~"^1"){if(l) print l;l=$0}else l=l","$0}END{print l}' file</description>
      <pubDate>Fri, 31 Aug 2007 16:31:48 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063565#M94563</guid>
      <dc:creator>Sandman!</dc:creator>
      <dc:date>2007-08-31T16:31:48Z</dc:date>
    </item>
    <item>
      <title>Re: awk Separating Records</title>
      <link>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063566#M94564</link>
      <description>Using Dennis' solution and adjust it a bit.&lt;BR /&gt;---------------&lt;BR /&gt;BEGIN { getline; save=$0 }&lt;BR /&gt;/^1\|[IDU]\|/ {&lt;BR /&gt;   print save&lt;BR /&gt;   save=$0"|"&lt;BR /&gt;   next&lt;BR /&gt;}&lt;BR /&gt;{&lt;BR /&gt;save=save $0"|" # concatenate lines&lt;BR /&gt;next&lt;BR /&gt;}&lt;BR /&gt;END  {}&lt;BR /&gt;-------------&lt;BR /&gt;Voila! I got records with | as the delimiter.&lt;BR /&gt;-----------------&lt;BR /&gt;% awk -f 1.awk audit.log&lt;BR /&gt;1|D|hfoo01||39322|74883|service-call-master2|call-no|143814|3|call-status|D|F|3|&lt;BR /&gt;call-date-last-change|20-AUG-2007|30-AUG-2007|&lt;BR /&gt;1|U|hfoo01||39322|74893|service-call-master|2|call-no|143814|3|call-user-only-nu&lt;BR /&gt;m1|0.0000|23158.0000|&lt;BR /&gt;1|I|hfoo01||39322|74893|job-cost-master|2|job-code|143814|3|jcm-project-manager|&lt;BR /&gt;GNOHPH01|HFOO01|&lt;BR /&gt;-----------&lt;BR /&gt;&lt;BR /&gt;Ah... except for the first record which missed a "|" and the fourth record which is totally missing.</description>
      <pubDate>Sat, 01 Sep 2007 01:37:47 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063566#M94564</guid>
      <dc:creator>nz_1</dc:creator>
      <dc:date>2007-09-01T01:37:47Z</dc:date>
    </item>
    <item>
      <title>Re: awk Separating Records</title>
      <link>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063567#M94565</link>
      <description>Sandman's solution.&lt;BR /&gt;----&lt;BR /&gt;%  awk '{if($0~"^1"){if(l) print l;l=$0}else l=l"|"$0}END{print l}' audit.log&lt;BR /&gt;1|D|hfoo01||39322|74883|service-call-master|2|call-no|143814|3|call-status|D|F|3&lt;BR /&gt;|call-date-last-change|20-AUG-2007|30-AUG-2007&lt;BR /&gt;1|U|hfoo01||39322|74893|service-call-master|2|call-no|143814|3|call-user-only-nu&lt;BR /&gt;m1|0.0000|23158.0000&lt;BR /&gt;1|I|hfoo01||39322|74893|job-cost-master|2|job-code|143814|3|jcm-project-manager|&lt;BR /&gt;GNOHPH01|HFOO01&lt;BR /&gt;1|U|hfoo01||39322|74903|service-call-master|2|call-no|143814|3|call-invoice-no||&lt;BR /&gt;S3106895|&lt;BR /&gt;--------&lt;BR /&gt;&lt;BR /&gt;Yes! All records retrieved!</description>
      <pubDate>Sat, 01 Sep 2007 01:41:13 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063567#M94565</guid>
      <dc:creator>nz_1</dc:creator>
      <dc:date>2007-09-01T01:41:13Z</dc:date>
    </item>
    <item>
      <title>Re: awk Separating Records</title>
      <link>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063568#M94566</link>
      <description>James' solution works fine but I cant figure out how to append | to the subsequent lines using perl.&lt;BR /&gt;-----------&lt;BR /&gt;% perl -0377 -wne 's/\012//g;@a=split(/(?=1\|I\||1\|D\||1\|U\|)/,$_);for $b (&amp;gt;&lt;BR /&gt;NEW RECORD:1|D|hfoo01||39322|74883|service-call-master2|call-no|1438143|call-sta&lt;BR /&gt;tus|D|F3|call-date-last-change|20-AUG-2007|30-AUG-2007&lt;BR /&gt;&lt;BR /&gt;NEW RECORD:1|U|hfoo01||39322|74893|service-call-master2|call-no|1438143|call-use&lt;BR /&gt;r-only-num1|0.0000|23158.0000&lt;BR /&gt;&lt;BR /&gt;NEW RECORD:1|I|hfoo01||39322|74893|job-cost-master2|job-code|1438143|jcm-project&lt;BR /&gt;-manager|GNOHPH01|HFOO01&lt;BR /&gt;&lt;BR /&gt;NEW RECORD:1|U|hfoo01||39322|74903|service-call-master2|call-no|1438143|call-inv&lt;BR /&gt;oice-no||S3106895</description>
      <pubDate>Sat, 01 Sep 2007 01:49:56 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063568#M94566</guid>
      <dc:creator>nz_1</dc:creator>
      <dc:date>2007-09-01T01:49:56Z</dc:date>
    </item>
    <item>
      <title>Re: awk Separating Records</title>
      <link>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063569#M94567</link>
      <description>Hi (again) Nash:&lt;BR /&gt;&lt;BR /&gt;&amp;gt; James' solution works fine but I cant figure out how to append | to the subsequent lines using perl.&lt;BR /&gt;&lt;BR /&gt;OK, try this:&lt;BR /&gt;&lt;BR /&gt;# perl -0377 -wne 's/\012//g;@a=split(/(?=1\|I\||1\|D\||1\|U\|)/,$_);for $b (@a) {print "NEW RECORD:$b|\n\n"}' audit.log&lt;BR /&gt;&lt;BR /&gt;I suspect that you want to drop the "NEW RECORD:" preamble and the additional newline ('\n') that I added for clarity, so:&lt;BR /&gt;&lt;BR /&gt;# perl -0377 -wne 's/\012//g;@a=split(/(?=1\|I\||1\|D\||1\|U\|)/,$_);for $b (@a) {print "$b|\n"}' audit.log&lt;BR /&gt;&lt;BR /&gt;1|D|hfoo01||39322|74883|service-call-master2|call-no|1438143|call-status|D|F3|ca&lt;BR /&gt;ll-date-last-change|20-AUG-2007|30-AUG-2007|&lt;BR /&gt;1|U|hfoo01||39322|74893|service-call-master2|call-no|1438143|call-user-only-num1&lt;BR /&gt;|0.0000|23158.0000|&lt;BR /&gt;1|I|hfoo01||39322|74893|job-cost-master2|job-code|1438143|jcm-project-manager|GN&lt;BR /&gt;OHPH01|HFOO01|&lt;BR /&gt;1|U|hfoo01||39322|74903|service-call-master2|call-no|1438143|call-invoice-no||S3&lt;BR /&gt;106895|&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Sat, 01 Sep 2007 08:24:44 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063569#M94567</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-09-01T08:24:44Z</dc:date>
    </item>
    <item>
      <title>Re: awk Separating Records</title>
      <link>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063570#M94568</link>
      <description>&amp;gt;except for the first record which missed a "|" and the fourth record which is totally missing.&lt;BR /&gt;&lt;BR /&gt;You need to adjust the BEGIN to add that "|".&lt;BR /&gt;And you lost my END to print out the last record.</description>
      <pubDate>Sat, 01 Sep 2007 16:16:24 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/awk-separating-records/m-p/4063570#M94568</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2007-09-01T16:16:24Z</dc:date>
    </item>
  </channel>
</rss>

