On Thu, 26 Jul 2001, Mayur Joshi wrote:
Which language you are using? I assume Perl, because this is much simpler in Perl.
Not really. It is easiest using grep, tr and cut, all tools that do a single job and do it well. That's why the pipe was invented. Perl is too heavy for the job.
Here is the code which I would prefer to write. open(RH,"</root/sandeep/log111.txt"); while(<RH>) { if($_ =~ /##Date/) { $flag = 1; } else { $flag = 0; } if($flag eq 1) { ($str1, $str2) = split(/ /, $_); $date = $str2; } }
This perl code would be shorter and faster:
perl -pe ' next unless /^##Date\s+([012]?[1-9]|3[01]-0?[1-9]|1?[0-2]-200[01])/; print "$1\n"; next; ' filename
If you're sure that ##Date occurs only once, then replace the second next with last. (Either next or last are required!!)
Note, this is significantly longer than the earlier versions using grep, cut and tr sent by manish and me.
If you want to use awk instead, it's simpler:
cat filename | awk '/^##Date/ {print $2}'
finally, either the perl or the awk output would have to be written to a variable using the $( expr ) syntax.
Philip