<?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: extract fields .... in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/extract-fields/m-p/5101525#M90923</link>
    <description>ok thats great and thanks all for the help</description>
    <pubDate>Mon, 07 Apr 2008 07:33:35 GMT</pubDate>
    <dc:creator>lawrenzo_1</dc:creator>
    <dc:date>2008-04-07T07:33:35Z</dc:date>
    <item>
      <title>extract fields ....</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extract-fields/m-p/5101517#M90915</link>
      <description>Hello all,&lt;BR /&gt;&lt;BR /&gt;I am trying to remove certain fields from each row in a file however the data I require isnt always the same field:&lt;BR /&gt;&lt;BR /&gt;sample output &lt;BR /&gt;&lt;BR /&gt;&lt;DATE&gt; 16:39:27 * (DATA=(CID=(PROGRAM=)(HOST=&lt;HOST&gt;)(USER=&lt;USER&gt;))(COMMAND=status)(ARGUMENTS=64)(SERVICE=LISTENER)(VERSION=153094144)) * data * 1 2 3 4&lt;BR /&gt;&lt;BR /&gt;the data I require is the date, HOST and USER fields however the first "*" dictates 1-4 fields depending on what has been written to the file.&lt;BR /&gt;&lt;BR /&gt;As far as I am aware the application that writes the file cannot be changed.&lt;BR /&gt;&lt;BR /&gt;any help is much appreciated&lt;BR /&gt;&lt;BR /&gt;Thanks&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/USER&gt;&lt;/HOST&gt;&lt;/DATE&gt;</description>
      <pubDate>Fri, 04 Apr 2008 15:15:12 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extract-fields/m-p/5101517#M90915</guid>
      <dc:creator>lawrenzo_1</dc:creator>
      <dc:date>2008-04-04T15:15:12Z</dc:date>
    </item>
    <item>
      <title>Re: extract fields ....</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extract-fields/m-p/5101518#M90916</link>
      <description>I have been reading and looking the awk "match" function:&lt;BR /&gt;&lt;BR /&gt; awk '{match($0, HOST)};{print substr($0, RSTART, RLENGHT)}' file&lt;BR /&gt;&lt;BR /&gt;but this doesnt work?&lt;BR /&gt;&lt;BR /&gt;I know I will have to build the statement up but any idea's to get me started?</description>
      <pubDate>Fri, 04 Apr 2008 15:40:38 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extract-fields/m-p/5101518#M90916</guid>
      <dc:creator>lawrenzo_1</dc:creator>
      <dc:date>2008-04-04T15:40:38Z</dc:date>
    </item>
    <item>
      <title>Re: extract fields ....</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extract-fields/m-p/5101519#M90917</link>
      <description>I would use perl.. but then I used perl for just about anything.&lt;BR /&gt;&lt;BR /&gt;$ perl -ne '$user=$1 if /\(USER=([^\)]+)/;$host=$1 if /\(HOST=([^\)]+)/; print qq($user $host\n)' some-file&lt;BR /&gt;&lt;BR /&gt;For an explanation, let's take the chunk:&lt;BR /&gt;&lt;BR /&gt;$user=$1 if /\(USER=([^)]+)/&lt;BR /&gt;&lt;BR /&gt;We make variable $user become the first remembered variable ($1) if we match &lt;BR /&gt;&lt;BR /&gt;\(USER=  : piece of string starting with escaped parenthesis&lt;BR /&gt;&lt;BR /&gt;([^)]+) : remember "()", some series of "[]+", NOT closing parenthesis "^)"&lt;BR /&gt;&lt;BR /&gt;Enjoy,&lt;BR /&gt;Hein.&lt;BR /&gt;</description>
      <pubDate>Fri, 04 Apr 2008 15:42:02 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extract-fields/m-p/5101519#M90917</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2008-04-04T15:42:02Z</dc:date>
    </item>
    <item>
      <title>Re: extract fields ....</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extract-fields/m-p/5101520#M90918</link>
      <description>Thanks Hein for the perl solution however I was looking for an awk / sed example as so I can manipulate and adapt if necessary!&lt;BR /&gt;&lt;BR /&gt;Thanks&lt;BR /&gt;&lt;BR /&gt;Chris .....&lt;BR /&gt;&lt;BR /&gt;any awk?</description>
      <pubDate>Fri, 04 Apr 2008 15:46:52 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extract-fields/m-p/5101520#M90918</guid>
      <dc:creator>lawrenzo_1</dc:creator>
      <dc:date>2008-04-04T15:46:52Z</dc:date>
    </item>
    <item>
      <title>Re: extract fields ....</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extract-fields/m-p/5101521#M90919</link>
      <description>Lawrenzo... given the style of questions you frequently ask you may want to look into picking up perl (or better).&lt;BR /&gt;&lt;BR /&gt;Yes, awk's MATCH should allow you to get there. But I find it cumbersome to use&lt;BR /&gt;&lt;BR /&gt;I would solve it by going very generic first, then ask for a detail.&lt;BR /&gt;Take that whole line, look for (name=value) chunks and add each chunk to an array.&lt;BR /&gt;&lt;BR /&gt;Here is a final solution:&lt;BR /&gt;&lt;BR /&gt;$ awk '{split($0,pairs,"\)+\("); for (i in pairs) {split(pairs[i],x,"="); y[x[1]]=x[2]}; print y["USER"],y["HOST"]}' file-name&lt;BR /&gt;&lt;USER&gt; &lt;HOST&gt;&lt;BR /&gt;&lt;BR /&gt;You (hopefully) see you could pick any component to print.&lt;BR /&gt;&lt;BR /&gt;Here are partial solutions to help you, and other interested readers, understand what is happening:&lt;BR /&gt;&lt;BR /&gt;First see if we can find 'pairs' by looking for splitting the line on ")(" but tolerating "))("&lt;BR /&gt;&lt;BR /&gt;$ awk '{split($0,pairs,"\)+\("); for (i in pairs) print pairs[i]}' x&lt;BR /&gt;HOST=&lt;HOST&gt;&lt;BR /&gt;USER=&lt;USER&gt;&lt;BR /&gt;COMMAND=status&lt;BR /&gt;ARGUMENTS=64&lt;BR /&gt;:&lt;BR /&gt;&lt;BR /&gt;Now pick apart each pair and print members.&lt;BR /&gt;&lt;BR /&gt;$ awk '{split($0,pairs,"\)+\("); for (i in pairs) {split(pairs[i],x,"="); print x[1],x[2]}}' x&lt;BR /&gt;HOST &lt;HOST&gt;&lt;BR /&gt;USER &lt;USER&gt;&lt;BR /&gt;COMMAND status&lt;BR /&gt;ARGUMENTS 64&lt;BR /&gt;&lt;BR /&gt;And finally use those members to build a generic array as per solution.&lt;BR /&gt;&lt;BR /&gt;Hein.&lt;/USER&gt;&lt;/HOST&gt;&lt;/USER&gt;&lt;/HOST&gt;&lt;/HOST&gt;&lt;/USER&gt;</description>
      <pubDate>Fri, 04 Apr 2008 16:03:00 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extract-fields/m-p/5101521#M90919</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2008-04-04T16:03:00Z</dc:date>
    </item>
    <item>
      <title>Re: extract fields ....</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extract-fields/m-p/5101522#M90920</link>
      <description>fwiw... it picks up those pieces even better if you simply allow for any number of opening parens and at least one close:&lt;BR /&gt;&lt;BR /&gt;$ awk '{split($0,pairs,"\)+\(*"); for (i in pairs) print pairs[i]}' x&lt;BR /&gt;HOST=&lt;HOST&gt;&lt;BR /&gt;USER=&lt;USER&gt;&lt;BR /&gt;COMMAND=status&lt;BR /&gt;ARGUMENTS=64&lt;BR /&gt;SERVICE=LISTENER&lt;BR /&gt;VERSION=153094144&lt;BR /&gt;&lt;BR /&gt;Arguably the nested initial names are better picked up by allowing an evan more generic "any close followed by any open"&lt;BR /&gt;&lt;BR /&gt;$ awk '{split($0,pairs,"\)*\(*"); for (i in pairs) print pairs[i]}' x&lt;BR /&gt;DATA=&lt;BR /&gt;CID=&lt;BR /&gt;PROGRAM=&lt;BR /&gt;HOST=&lt;HOST&gt;&lt;BR /&gt;USER=&lt;USER&gt;&lt;BR /&gt;COMMAND=status&lt;BR /&gt;ARGUMENTS=64&lt;BR /&gt;SERVICE=LISTENER&lt;BR /&gt;VERSION=153094144&lt;BR /&gt;&lt;BR /&gt;Just for for thought.&lt;BR /&gt;ZERO points please.&lt;BR /&gt;&lt;BR /&gt;Hein.&lt;BR /&gt;&lt;/USER&gt;&lt;/HOST&gt;&lt;/USER&gt;&lt;/HOST&gt;</description>
      <pubDate>Fri, 04 Apr 2008 16:11:42 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extract-fields/m-p/5101522#M90920</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2008-04-04T16:11:42Z</dc:date>
    </item>
    <item>
      <title>Re: extract fields ....</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extract-fields/m-p/5101523#M90921</link>
      <description>&amp;gt;the data I require is the date, HOST and USER fields however the first "*" dictates 1-4 fields depending on what has been written to the file.&lt;BR /&gt;&lt;BR /&gt;I'm not sure what you mean about that "*"?&lt;BR /&gt;&lt;BR /&gt;You can use awk and pretend you are programming in C.  Is the date enclosed in &amp;lt;&amp;gt; or that's just your metachars?  If not in &amp;lt;&amp;gt;, how many fields?&lt;BR /&gt;&lt;BR /&gt;awk '&lt;BR /&gt;{&lt;BR /&gt;# extract date stuff&lt;BR /&gt;...&lt;BR /&gt;# find HOST&lt;BR /&gt;h = index($0, "HOST=")&lt;BR /&gt;t = substr($0, h)&lt;BR /&gt;e = index(t, ")")&lt;BR /&gt;host = substr(t, 1, e-1)&lt;BR /&gt;# find USER&lt;BR /&gt;u = index($0, "USER=")&lt;BR /&gt;t = substr($0, u)&lt;BR /&gt;e = index(t, ")")&lt;BR /&gt;user = substr(t, 1, e-1)&lt;BR /&gt;print date, host, user&lt;BR /&gt;}' file&lt;BR /&gt;&lt;BR /&gt;You could then write a function that does the work for HOST and USER:&lt;BR /&gt;&lt;BR /&gt;awk '&lt;BR /&gt;function find_it(line, str) {&lt;BR /&gt;h = index(line, str)&lt;BR /&gt;t = substr(line, h)&lt;BR /&gt;e = index(t, ")")&lt;BR /&gt;return substr(t, 1, e-1)&lt;BR /&gt;}&lt;BR /&gt;{&lt;BR /&gt;# extract date stuff&lt;BR /&gt;...&lt;BR /&gt;host = find_it($0, "HOST=")&lt;BR /&gt;user = find_it($0, "USER=")&lt;BR /&gt;print date, host, user&lt;BR /&gt;}' file&lt;BR /&gt;&lt;BR /&gt;Of course you can go with your first thought about using match:&lt;BR /&gt;awk '&lt;BR /&gt;{&lt;BR /&gt;match($0, "HOST=[^\)]*\)")&lt;BR /&gt;print substr($0, RSTART, RLENGTH-1)&lt;BR /&gt;}' file</description>
      <pubDate>Fri, 04 Apr 2008 23:16:33 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extract-fields/m-p/5101523#M90921</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2008-04-04T23:16:33Z</dc:date>
    </item>
    <item>
      <title>Re: extract fields ....</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extract-fields/m-p/5101524#M90922</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;I would consider using the char '=' as a delimiter and split out the value.&lt;BR /&gt;Something like this:&lt;BR /&gt;awk -F= '{for(i=2;i&lt;NF&gt;&lt;/NF&gt;&lt;BR /&gt;mfG Peter</description>
      <pubDate>Sat, 05 Apr 2008 06:26:21 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extract-fields/m-p/5101524#M90922</guid>
      <dc:creator>Peter Nikitka</dc:creator>
      <dc:date>2008-04-05T06:26:21Z</dc:date>
    </item>
    <item>
      <title>Re: extract fields ....</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extract-fields/m-p/5101525#M90923</link>
      <description>ok thats great and thanks all for the help</description>
      <pubDate>Mon, 07 Apr 2008 07:33:35 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extract-fields/m-p/5101525#M90923</guid>
      <dc:creator>lawrenzo_1</dc:creator>
      <dc:date>2008-04-07T07:33:35Z</dc:date>
    </item>
    <item>
      <title>Re: extract fields ....</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extract-fields/m-p/5101526#M90924</link>
      <description>this is what I used in the end:&lt;BR /&gt;&lt;BR /&gt;awk '{match($0, "HOST=[^\)]*\)")} {host = substr($0, RSTART, RLENGTH-1)};{match($0, "USER=[^\)]*\)")} {user = substr($0, RSTART, RLENGTH-1)};{print $1,host,user}' file"&lt;BR /&gt;&lt;BR /&gt;04-APR-2008 HOST=&lt;SERVER&gt; USER=&lt;USER&gt;&lt;BR /&gt;&lt;BR /&gt;cheers &lt;BR /&gt;&lt;BR /&gt;Chris.&lt;/USER&gt;&lt;/SERVER&gt;</description>
      <pubDate>Mon, 07 Apr 2008 08:27:24 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extract-fields/m-p/5101526#M90924</guid>
      <dc:creator>lawrenzo_1</dc:creator>
      <dc:date>2008-04-07T08:27:24Z</dc:date>
    </item>
    <item>
      <title>Re: extract fields ....</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extract-fields/m-p/5101527#M90925</link>
      <description>done</description>
      <pubDate>Mon, 07 Apr 2008 08:28:03 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extract-fields/m-p/5101527#M90925</guid>
      <dc:creator>lawrenzo_1</dc:creator>
      <dc:date>2008-04-07T08:28:03Z</dc:date>
    </item>
  </channel>
</rss>

