<?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: extracting data from string in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601724#M104089</link>
    <description>Hi,&lt;BR /&gt;&lt;BR /&gt;I got some idea to extract the flagged patern to share with you. &lt;BR /&gt;Using combination of commands.&lt;BR /&gt;&lt;BR /&gt;grep -n &lt;START_FLAG&gt; filename&lt;BR /&gt;&lt;BR /&gt;where -n option give you line no. of startflag.&lt;BR /&gt;Extract the pattern using the head or tail command option to -/+&lt;LINENO&gt;.&lt;BR /&gt;&lt;BR /&gt;Hope this hint may give some more idea.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Rajesh &lt;BR /&gt;&lt;BR /&gt;&lt;/LINENO&gt;&lt;/START_FLAG&gt;</description>
    <pubDate>Thu, 11 Aug 2005 08:53:41 GMT</pubDate>
    <dc:creator>Rajesh SB</dc:creator>
    <dc:date>2005-08-11T08:53:41Z</dc:date>
    <item>
      <title>extracting data from string</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601717#M104082</link>
      <description>Hi All,&lt;BR /&gt;&lt;BR /&gt;I need to extract some tagged line from a file formated like this:&lt;BR /&gt;&lt;BR /&gt;flag_start&lt;BR /&gt;line1&lt;BR /&gt;line2&lt;BR /&gt;...&lt;BR /&gt;...&lt;BR /&gt;Serial_number1&lt;BR /&gt;linea&lt;BR /&gt;lineb&lt;BR /&gt;...&lt;BR /&gt;...&lt;BR /&gt;flag_stop&lt;BR /&gt;flag_start&lt;BR /&gt;line1&lt;BR /&gt;line2&lt;BR /&gt;...&lt;BR /&gt;...&lt;BR /&gt;Serial_number2&lt;BR /&gt;linea&lt;BR /&gt;lineb&lt;BR /&gt;...&lt;BR /&gt;...&lt;BR /&gt;flag_stop&lt;BR /&gt;&lt;BR /&gt;In the file, there are several flagged patern with Serial_number1, and I would like to extract the whole pattern and write it in a new file ...&lt;BR /&gt;&lt;BR /&gt;Can someone help me with this &lt;BR /&gt;&lt;BR /&gt;Thanks in advance for your help&lt;BR /&gt;&lt;BR /&gt;Best Regards&lt;BR /&gt;&lt;BR /&gt;Jérôme&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 11 Aug 2005 08:09:14 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601717#M104082</guid>
      <dc:creator>Chartier Jerome</dc:creator>
      <dc:date>2005-08-11T08:09:14Z</dc:date>
    </item>
    <item>
      <title>Re: extracting data from string</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601718#M104083</link>
      <description>Hey;&lt;BR /&gt;&lt;BR /&gt;My understanding of your request is that you want everything from "Serial_number1" to flag_stop in a different file.  Assuming that's the case, it's easy:&lt;BR /&gt;&lt;BR /&gt;sed -n -e '/^Serial_number1/,/^flag_stop/p' ${in_file} &amp;gt; ${new_out_file}&lt;BR /&gt;&lt;BR /&gt;HTH;&lt;BR /&gt;&lt;BR /&gt;Doug</description>
      <pubDate>Thu, 11 Aug 2005 08:15:14 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601718#M104083</guid>
      <dc:creator>Doug O'Leary</dc:creator>
      <dc:date>2005-08-11T08:15:14Z</dc:date>
    </item>
    <item>
      <title>Re: extracting data from string</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601719#M104084</link>
      <description>Hi,&lt;BR /&gt;Not sure about exact what you want to do but, awk is a very useful tool for processing text files. Search the forum for "awk" and you will find a lot of examples how to use awk.</description>
      <pubDate>Thu, 11 Aug 2005 08:15:52 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601719#M104084</guid>
      <dc:creator>Leif Halvarsson_2</dc:creator>
      <dc:date>2005-08-11T08:15:52Z</dc:date>
    </item>
    <item>
      <title>Re: extracting data from string</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601720#M104085</link>
      <description>Hi all, &lt;BR /&gt;&lt;BR /&gt;Thanks for your answers, in fact, In each file, the pattern is flagged beetween flag_start and flag_stop.&lt;BR /&gt;In each file, I recognize the pattern by Serial_number, and there are several per file.&lt;BR /&gt;What I would like is to extract all the patterns (beetween flag_start and flag_stop) that have the same Serial_number.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Thanks in advance for your help&lt;BR /&gt;&lt;BR /&gt;JÃ©rÃ´me C</description>
      <pubDate>Thu, 11 Aug 2005 08:22:23 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601720#M104085</guid>
      <dc:creator>Chartier Jerome</dc:creator>
      <dc:date>2005-08-11T08:22:23Z</dc:date>
    </item>
    <item>
      <title>Re: extracting data from string</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601721#M104086</link>
      <description>Hmm, so you want your logic to say: -&lt;BR /&gt;&lt;BR /&gt;If the "serial number" pattern within this particular flagged block matched X, then extract the whole block?&lt;BR /&gt;&lt;BR /&gt;If so then try something like: &lt;BR /&gt;&lt;BR /&gt;awk -F"\n" 'BEGIN { matched=0 ; block="" } \&lt;BR /&gt;     $1 !~ "^flag_s*" { block = block$1"\n" } \&lt;BR /&gt;     $1 == "Serial_number1" { matched = 1 } \&lt;BR /&gt;     $1 == "flag_stop" { if ( matched == 1 ) { print block } ; block="" ; matched=0 }' infile&lt;BR /&gt;&lt;BR /&gt;Basically it sets a flagged called "matched" to zero.  Then reads each line in turn, and appends it to a variable.  If it gets to a matching serial number, it sets the "matched" flag to 1.  Then when it comes to "flag_stop" it knows it's finished a block, and if at this time "matched" is 1, it prints the block, otherwise it doesn't.&lt;BR /&gt;&lt;BR /&gt;This suit?</description>
      <pubDate>Thu, 11 Aug 2005 08:48:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601721#M104086</guid>
      <dc:creator>Simon Hargrave</dc:creator>
      <dc:date>2005-08-11T08:48:54Z</dc:date>
    </item>
    <item>
      <title>Re: extracting data from string</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601722#M104087</link>
      <description>Jerome --&lt;BR /&gt;&lt;BR /&gt;Can you give us like 10 to 20 lines of a real file.&lt;BR /&gt;&lt;BR /&gt;The problem I'm having is figuring out if Serial_numberN appears multiple times or what.&lt;BR /&gt;&lt;BR /&gt;The following script will print ALL of the data between flag_start and flag_stop.  I suspect that's not what you want, but I thought I would post that much while I was asking my questions.&lt;BR /&gt;&lt;BR /&gt;Create a file called useme.awk with the following in it:&lt;BR /&gt;&lt;BR /&gt;BEGIN{daflag=0;}&lt;BR /&gt;/flag_start/ {daflag=1;next}&lt;BR /&gt;/flag_stop/  {daflag=0;next}&lt;BR /&gt;daflag==1 {print $0}&lt;BR /&gt;&lt;BR /&gt;Run:&lt;BR /&gt;&lt;BR /&gt;awk -f useme.awk &amp;lt; inputfile &amp;gt; outputfile&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 11 Aug 2005 08:49:35 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601722#M104087</guid>
      <dc:creator>Kent Ostby</dc:creator>
      <dc:date>2005-08-11T08:49:35Z</dc:date>
    </item>
    <item>
      <title>Re: extracting data from string</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601723#M104088</link>
      <description>Oh and my example assumed you wanted to strip the flag_start and flag_stop entries.  If you want to keep them, simply change the second line to be:&lt;BR /&gt;&lt;BR /&gt;{ block = block$1"\n" } \&lt;BR /&gt;&lt;BR /&gt;and thus removing the ^flag_s* ignorance.</description>
      <pubDate>Thu, 11 Aug 2005 08:50:22 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601723#M104088</guid>
      <dc:creator>Simon Hargrave</dc:creator>
      <dc:date>2005-08-11T08:50:22Z</dc:date>
    </item>
    <item>
      <title>Re: extracting data from string</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601724#M104089</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;I got some idea to extract the flagged patern to share with you. &lt;BR /&gt;Using combination of commands.&lt;BR /&gt;&lt;BR /&gt;grep -n &lt;START_FLAG&gt; filename&lt;BR /&gt;&lt;BR /&gt;where -n option give you line no. of startflag.&lt;BR /&gt;Extract the pattern using the head or tail command option to -/+&lt;LINENO&gt;.&lt;BR /&gt;&lt;BR /&gt;Hope this hint may give some more idea.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Rajesh &lt;BR /&gt;&lt;BR /&gt;&lt;/LINENO&gt;&lt;/START_FLAG&gt;</description>
      <pubDate>Thu, 11 Aug 2005 08:53:41 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601724#M104089</guid>
      <dc:creator>Rajesh SB</dc:creator>
      <dc:date>2005-08-11T08:53:41Z</dc:date>
    </item>
    <item>
      <title>Re: extracting data from string</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601725#M104090</link>
      <description>&lt;BR /&gt;How do you recognize a serial number?&lt;BR /&gt;Does it always start with "Serial" in the begin of a line?&lt;BR /&gt;You wanted the lines BETWEEN start and stop right, not inclusive right?&lt;BR /&gt;If so ten I think the perl below is what you need.&lt;BR /&gt;You'll need to adapt the 'Serial' match to the actual pattern, and you probably want a different algoritme for the file name generation ($file = ...)&lt;BR /&gt;&lt;BR /&gt;hth,&lt;BR /&gt;Hein.&lt;BR /&gt;&lt;BR /&gt;while (&amp;lt;&amp;gt;) {&lt;BR /&gt;if (/^flag_start/) { &lt;BR /&gt;  $save = 1; &lt;BR /&gt;  next;&lt;BR /&gt;  }&lt;BR /&gt;if (/^flag_stop/) {&lt;BR /&gt;  $save = 0; &lt;BR /&gt;  if ($file) {&lt;BR /&gt;    open (FILE,"&amp;gt;&amp;gt; $file") or die "could not append to $file";&lt;BR /&gt;    print FILE $line while ($line = shift @lines);&lt;BR /&gt;    close FILE;&lt;BR /&gt;    }&lt;BR /&gt;  }&lt;BR /&gt;next unless $save;&lt;BR /&gt;push @lines, $_;&lt;BR /&gt;if (/^Serial/) {&lt;BR /&gt; chop;&lt;BR /&gt; $file = $_ . ".tmp" ;&lt;BR /&gt; }&lt;BR /&gt;}    &lt;BR /&gt;</description>
      <pubDate>Thu, 11 Aug 2005 09:02:40 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601725#M104090</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2005-08-11T09:02:40Z</dc:date>
    </item>
    <item>
      <title>Re: extracting data from string</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601726#M104091</link>
      <description>Thanks everyone ...&lt;BR /&gt;&lt;BR /&gt;I would like to include the lines.&lt;BR /&gt;I am sorry, I am very bad in scripting so .. &lt;BR /&gt;Where do I put the input filename .. in the script?&lt;BR /&gt;&lt;BR /&gt;Thanks in advance&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Best Regards&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;JÃ©rÃ´me</description>
      <pubDate>Thu, 11 Aug 2005 09:29:23 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601726#M104091</guid>
      <dc:creator>Chartier Jerome</dc:creator>
      <dc:date>2005-08-11T09:29:23Z</dc:date>
    </item>
    <item>
      <title>Re: extracting data from string</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601727#M104092</link>
      <description>Who's script do you refer to?  If you mean mine (the use of awk), then the input filename is after the code, eg you will see "infile" at the end of my post, that is the name of the input file.  If you want the output in an output file then append &amp;gt;outfile at the end of the last line.</description>
      <pubDate>Thu, 11 Aug 2005 09:41:23 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601727#M104092</guid>
      <dc:creator>Simon Hargrave</dc:creator>
      <dc:date>2005-08-11T09:41:23Z</dc:date>
    </item>
    <item>
      <title>Re: extracting data from string</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601728#M104093</link>
      <description>&lt;BR /&gt;Still not too clear.&lt;BR /&gt;But here is an other PERL variation.&lt;BR /&gt;This one takes the serial number desired as input variable.&lt;BR /&gt;&lt;BR /&gt;If you cut &amp;amp; paste the perl below into a file called for example 'extract.p' then you execute as:&lt;BR /&gt;&lt;BR /&gt;#perl extract.p Serial_number1 &amp;lt; all.data &amp;gt; extracted.data&lt;BR /&gt;&lt;BR /&gt;Between my first example and this tweak you should have all possible needs covered (the second outputs start and stop... I'm sure you can figure out how)&lt;BR /&gt;&lt;BR /&gt;My first example just execute as:&lt;BR /&gt;&lt;BR /&gt;#perl extract.p &amp;lt; all.data&lt;BR /&gt;&lt;BR /&gt;it will create: Serial_number1.tmp and Serial_number2.tmp&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Good luck!&lt;BR /&gt;Hein&lt;BR /&gt;&lt;BR /&gt;$serial = shift @ARGV or die "please provide serial number to select on";&lt;BR /&gt;while (&amp;lt;&amp;gt;) {&lt;BR /&gt;if (/^flag_start/) {&lt;BR /&gt;  $save = 1;&lt;BR /&gt;  $file = 0;&lt;BR /&gt;  undef @lines;&lt;BR /&gt;  }&lt;BR /&gt;if (/^flag_stop/) {&lt;BR /&gt;  $save = 0; &lt;BR /&gt;  push @lines, $_;&lt;BR /&gt;  if ($file) {&lt;BR /&gt;    print while ($_ = shift @lines );&lt;BR /&gt;    }&lt;BR /&gt;  print "\n";&lt;BR /&gt;}&lt;BR /&gt;push @lines, $_ if $save;&lt;BR /&gt;$file++ if /$serial/;&lt;BR /&gt;} &lt;BR /&gt;</description>
      <pubDate>Thu, 11 Aug 2005 09:50:01 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601728#M104093</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2005-08-11T09:50:01Z</dc:date>
    </item>
    <item>
      <title>Re: extracting data from string</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601729#M104094</link>
      <description>Hello all,&lt;BR /&gt;&lt;BR /&gt;Thanks for your answers.&lt;BR /&gt;Hein, your second script seems to run .. congratulations and thanks for all.&lt;BR /&gt;A last question Hein, If I want to do this on all files in the same directory?&lt;BR /&gt;For example files starting with a patern ABC.&lt;BR /&gt;&lt;BR /&gt;Thanks again &lt;BR /&gt;&lt;BR /&gt;JÃ©rÃ´me C&lt;BR /&gt;</description>
      <pubDate>Thu, 11 Aug 2005 10:16:39 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601729#M104094</guid>
      <dc:creator>Chartier Jerome</dc:creator>
      <dc:date>2005-08-11T10:16:39Z</dc:date>
    </item>
    <item>
      <title>Re: extracting data from string</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601730#M104095</link>
      <description>Hi All,&lt;BR /&gt;&lt;BR /&gt;Thanks a lot for your help.&lt;BR /&gt;Is it possible to extend the second perl script to pick every file in a directory starting with a common pattern and store all the results in one file?&lt;BR /&gt;&lt;BR /&gt;Thanks all again&lt;BR /&gt;&lt;BR /&gt;Best Regards&lt;BR /&gt;&lt;BR /&gt;JÃ©rÃ´me</description>
      <pubDate>Fri, 12 Aug 2005 02:49:39 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601730#M104095</guid>
      <dc:creator>Chartier Jerome</dc:creator>
      <dc:date>2005-08-12T02:49:39Z</dc:date>
    </item>
    <item>
      <title>Re: extracting data from string</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601731#M104096</link>
      <description>You can execute perl script as,&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;perl extract.pl ABC*.log&lt;BR /&gt;&lt;BR /&gt;It will check all the files with ABC*.log pattern.&lt;BR /&gt;&lt;BR /&gt;You can also use this script as,&lt;BR /&gt;&lt;BR /&gt;for file in `ls &lt;PATTERN&gt;`&lt;BR /&gt;do&lt;BR /&gt;    perl -ne '{if(/^Serial_number1/){undef @arr;next;}if(/^flag_stop/){exit;}push @arr,$_;}END{print @arr;}' $file&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;For example:&lt;BR /&gt;&lt;BR /&gt;test.log test1.log test2.log&lt;BR /&gt;&lt;BR /&gt;for file in `ls test*.log`&lt;BR /&gt;do&lt;BR /&gt;    perl -ne '{if(/^Serial_number1/){undef @arr;next;}if(/^flag_stop/){exit;}push @arr,$_;}END{print @arr;}' $file&lt;BR /&gt;done &amp;gt; Serial_number1.tmp&lt;BR /&gt;&lt;BR /&gt;hth.&lt;/PATTERN&gt;</description>
      <pubDate>Fri, 12 Aug 2005 03:26:03 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601731#M104096</guid>
      <dc:creator>Muthukumar_5</dc:creator>
      <dc:date>2005-08-12T03:26:03Z</dc:date>
    </item>
    <item>
      <title>Re: extracting data from string</title>
      <link>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601732#M104097</link>
      <description>JÃ©rÃ´me, Good to see you are all set now. &lt;BR /&gt;I kanda had read the requirement for multiple input files, but wanted to leave somethign for you to do :-).&lt;BR /&gt;&lt;BR /&gt;Muthukumar, I discarded a one liner along the lines you showed because it was my understanding that the 'serial number' was after an arbitrary number of data lines that would also be needed. So you need to start remembering at flag_start, not just when you see you are reading data for the right serial number.&lt;BR /&gt;&lt;BR /&gt;Cheers,&lt;BR /&gt;Hein.&lt;BR /&gt; &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 12 Aug 2005 08:55:05 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/extracting-data-from-string/m-p/3601732#M104097</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2005-08-12T08:55:05Z</dc:date>
    </item>
  </channel>
</rss>

