<?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: Find and print string in textfile in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/find-and-print-string-in-textfile/m-p/5040237#M65982</link>
    <description>I would try something like this:&lt;BR /&gt;&lt;BR /&gt;# cat myfile.txt | sed -n 's/.*\(searchstring.*\)/\1/p' | awk '{print $1,$2,$3}'&lt;BR /&gt;&lt;BR /&gt;This example would first use sed to return the line(s) containing 'searchstring' and cut off everything preceding 'searchstring'.&lt;BR /&gt;&lt;BR /&gt;Then awk will take these lines, where $1 is 'searchstring', $2 is the next field, and so on. Hence, this example would return the searchstring and the two next fields.&lt;BR /&gt;&lt;BR /&gt;Cheers,&lt;BR /&gt;Wout</description>
    <pubDate>Mon, 16 Apr 2007 08:41:44 GMT</pubDate>
    <dc:creator>Wouter Jagers</dc:creator>
    <dc:date>2007-04-16T08:41:44Z</dc:date>
    <item>
      <title>Find and print string in textfile</title>
      <link>https://community.hpe.com/t5/operating-system-linux/find-and-print-string-in-textfile/m-p/5040236#M65981</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;I'm trying to find a neat way of finding a string in a file, then print the string and a number of following characters (or groups of characters/fields).&lt;BR /&gt;  I've tried the "awk '{print $8,$9,$10}'" but the problem is that the number of fields before the ones I want, changes.&lt;BR /&gt; Something like: Search för the string "ABC" inside the file, the print the string/field and following 3 fields.&lt;BR /&gt;&lt;BR /&gt;Any suggestions anyone?&lt;BR /&gt;&lt;BR /&gt;Thanks in advance,  /Joakim</description>
      <pubDate>Mon, 16 Apr 2007 04:27:18 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/find-and-print-string-in-textfile/m-p/5040236#M65981</guid>
      <dc:creator>Joakim Brosten</dc:creator>
      <dc:date>2007-04-16T04:27:18Z</dc:date>
    </item>
    <item>
      <title>Re: Find and print string in textfile</title>
      <link>https://community.hpe.com/t5/operating-system-linux/find-and-print-string-in-textfile/m-p/5040237#M65982</link>
      <description>I would try something like this:&lt;BR /&gt;&lt;BR /&gt;# cat myfile.txt | sed -n 's/.*\(searchstring.*\)/\1/p' | awk '{print $1,$2,$3}'&lt;BR /&gt;&lt;BR /&gt;This example would first use sed to return the line(s) containing 'searchstring' and cut off everything preceding 'searchstring'.&lt;BR /&gt;&lt;BR /&gt;Then awk will take these lines, where $1 is 'searchstring', $2 is the next field, and so on. Hence, this example would return the searchstring and the two next fields.&lt;BR /&gt;&lt;BR /&gt;Cheers,&lt;BR /&gt;Wout</description>
      <pubDate>Mon, 16 Apr 2007 08:41:44 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/find-and-print-string-in-textfile/m-p/5040237#M65982</guid>
      <dc:creator>Wouter Jagers</dc:creator>
      <dc:date>2007-04-16T08:41:44Z</dc:date>
    </item>
    <item>
      <title>Re: Find and print string in textfile</title>
      <link>https://community.hpe.com/t5/operating-system-linux/find-and-print-string-in-textfile/m-p/5040238#M65983</link>
      <description>Hi Joakim:&lt;BR /&gt;&lt;BR /&gt;Here's one way using 'awk'.  For any line that matches your pattern, the pattern and whatever follows to the line's end will be printed.  Consider teh example where "HERE" is the pattern for which you want to match:&lt;BR /&gt;&lt;BR /&gt;# X="the pattern I want is HERE to the end!"&lt;BR /&gt;&lt;BR /&gt;# echo ${X} | awk '{OFF=match($0,/HERE/);if (OFF&amp;gt;0) {print substr($0,OFF)}}' &lt;BR /&gt;&lt;BR /&gt;HERE to the end!&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Mon, 16 Apr 2007 13:37:43 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/find-and-print-string-in-textfile/m-p/5040238#M65983</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-04-16T13:37:43Z</dc:date>
    </item>
    <item>
      <title>Re: Find and print string in textfile</title>
      <link>https://community.hpe.com/t5/operating-system-linux/find-and-print-string-in-textfile/m-p/5040239#M65984</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;&amp;gt; I'm trying to find a neat way of finding a string in a file,&lt;BR /&gt;&amp;gt; then print the string and a number of following characters&lt;BR /&gt;&amp;gt; (or groups of characters/fields).&lt;BR /&gt;&lt;BR /&gt;&amp;gt; I've tried the [awk ...]&lt;BR /&gt;&lt;BR /&gt;Any suggestions anyone?&lt;BR /&gt;&lt;BR /&gt;I often use perl (Portable Extraction and Report Language) for such.&lt;BR /&gt;Perl is an effective combination of awk, lex, yacc, and lisp.&lt;BR /&gt;&lt;BR /&gt;&lt;EXAMPLE&gt;&lt;BR /&gt;ksh&amp;gt; cat &amp;lt;&lt;EOF&gt;&lt;/EOF&gt;&amp;gt;    This line should be ignored&lt;BR /&gt;&amp;gt;    This line should appear ABC foo bar fie ignored&lt;BR /&gt;&amp;gt;    This line should be ignored&lt;BR /&gt;&amp;gt;    And this line CAB morefoo&lt;BR /&gt;&amp;gt;    This line should be ignored&lt;BR /&gt;&amp;gt;EOF&lt;BR /&gt;&amp;gt;    perl -pe '$_="" unless m:^.*([ABC][ABC][ABC])(\s+(\S+))?(\s+(\S+))?(\s+(\S+))?:;$_="$1:$3:$5:$7\n" unless ""eq"$_";'&lt;BR /&gt;ABC:foo:bar:fie&lt;BR /&gt;CAB:morefoo::&lt;BR /&gt;&lt;/EXAMPLE&gt;&lt;BR /&gt;&lt;BR /&gt;I don't know your level of comfort with perl.&lt;BR /&gt;More details are available on further request.&lt;BR /&gt;&lt;BR /&gt;perl -pe = run the given command(s) on each line and print&lt;BR /&gt;$_="" unless = throw away any lines not matching the sentinel&lt;BR /&gt;m:^...  = define what matches&lt;BR /&gt;([ABC][ABC][ABC]) = sentinel is ABC, BAC, ACB, CAB, BCA, CBA, assign to $1&lt;BR /&gt;(\s+  = match whitespace, assign to $2(, $4, $6)&lt;BR /&gt;(\S+  = match non-whitespace, assign to $3(, $5, $7)&lt;BR /&gt;))?  = close the regular expressions&lt;BR /&gt;$_=  = assign what to print&lt;BR /&gt;"$1:$3:$5:$7"           = concatenate match values that begin at 1st, 3rd, 5th, and 7th left parentheses.&lt;BR /&gt;&lt;BR /&gt;See "ksh&amp;gt; perldoc perlre" for further details on the ([:+?]) usage.&lt;BR /&gt;See "ksh&amp;gt; perldoc perlrun" for further details on -pe.&lt;BR /&gt;&lt;BR /&gt;Hopefully helpful,&lt;BR /&gt;</description>
      <pubDate>Mon, 16 Apr 2007 16:28:16 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/find-and-print-string-in-textfile/m-p/5040239#M65984</guid>
      <dc:creator>drb_1</dc:creator>
      <dc:date>2007-04-16T16:28:16Z</dc:date>
    </item>
    <item>
      <title>Re: Find and print string in textfile</title>
      <link>https://community.hpe.com/t5/operating-system-linux/find-and-print-string-in-textfile/m-p/5040240#M65985</link>
      <description>Thank you fellows,&lt;BR /&gt;&lt;BR /&gt;Wout's solution is just what i need, many thanks.&lt;BR /&gt;&lt;BR /&gt;JRF, this can be very helpful in a other challenge I have, thank you for your contribution.&lt;BR /&gt;&lt;BR /&gt;drb, perl is something that is very interesting, I will look into this in the future. You have done a very good instruction and for me, an introduction, to the world of perl. Thank you.&lt;BR /&gt;&lt;BR /&gt;  /Joakim</description>
      <pubDate>Tue, 17 Apr 2007 01:46:03 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/find-and-print-string-in-textfile/m-p/5040240#M65985</guid>
      <dc:creator>Joakim Brosten</dc:creator>
      <dc:date>2007-04-17T01:46:03Z</dc:date>
    </item>
    <item>
      <title>Re: Find and print string in textfile</title>
      <link>https://community.hpe.com/t5/operating-system-linux/find-and-print-string-in-textfile/m-p/5040241#M65986</link>
      <description>Thanks again.  /Joakim</description>
      <pubDate>Tue, 17 Apr 2007 04:25:36 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/find-and-print-string-in-textfile/m-p/5040241#M65986</guid>
      <dc:creator>Joakim Brosten</dc:creator>
      <dc:date>2007-04-17T04:25:36Z</dc:date>
    </item>
  </channel>
</rss>

