\root\sandeep\log111.txt This files has a lot of text and one particulr line is ..... ##Date 24-07-2001
Now
- I need to locate the line "##Date" in the above file
- I then need to store the date value "24-07.2001" in a variable.
Which language you are using? I assume Perl, because this is much simpler in Perl. 1. Open the file for reading. 2. Search for the above string by reading every line. $_ variable contains the current line. You can do this by using pattern searching operations in Perl. 3. Once you find the above line then split that line at spaces and collect the required date in a variable.
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; } }
Hope this is very much clear.
Regards,
Mayur
_________________________________________________________________ Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
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
Sometime today, Mayur Joshi wrote:
open(RH,"</root/sandeep/log111.txt"); while(<RH>) { if($_ =~ /##Date/) { $flag = 1; } else { $flag = 0; } if($flag eq 1) { ($str1, $str2) = split(/ /, $_); $date = $str2; } }
Or, from the command line,
perl -ne 's/^Date## +(.+)$/$1/ && print $_' </root/sandeep/log111.txt
Manish
Sometime Today, Manish Jethani assembled some asciibets to say:
perl -ne 's/^Date## +(.+)$/$1/ && print $_' </root/sandeep/log111.txt
^^^^^^^^
That makes it inefficient (although, in this case only mildly so).
instead, try avoiding the back reference:
s/^Date##\s+// && print $_
Since you know that what's left on the line is all that you want.
Alternately, use a match: /^Date##\s+(.+)$/ && print $1
Philip
hi,
How can I forward emails with an .EXE attachment to junk@my_company.com id. I guess procmail should help me ??
kindly advise, --Sandeep
_________________________________________________________ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com
On Wed, 2 Jan 2002, SP wrote:
How can I forward emails with an .EXE attachment to junk@my_company.com id.
procmail. You will have to scan the body of the mail, not the header (B flag) Look for the Content-type and Content-disposition headers
Content-type may have a name="..." part, while Content-disposition may have a filename="..." part (both after the main data and a semicolon).
eg: Content-type: application/octet-stream; name="something.exe" Content-disposition: attachment; filename="something.exe"
Some clients do not set one of these fields, that is a bug. There is also no guarantee that there will be a name/filename part, or that they will contain valid information. Also no guarantee that the content-type will match the filename. (you could have text/plain; name="something.exe")
Philip