Extract file
I have a file like that
============== $vi ora.log Mon Jul 20 23:59:57 GMT 2009 file1.txt file2.txt Mon Jul 20 23:59:58 GMT 2009 Mon Jul 20 23:59:59 GMT 2009 Mon Jul 21 00:00:00 GMT 2009 Tue Jul 21 00:00:01 GMT 2009 Tue Jul 21 00:00:02 GMT 2009 Tue Jul 21 00:00:03 GMT 2009 Tue Jul 21 00:00:04 GMT 2009 file3.txt file4.txt Tue Jul 21 00:00:05 GMT 2009 Tue Jul 21 00:00:06 GMT 2009 file5.txt Tue Jul 21 00:00:07 GMT 2009 " " the files is appended continuely and separated by current date. I would like to extract all file name that is created on today to another file . Assume today is 21July , from the above file , file3 , file4 & file5 is created today , so what I would like is to output the file name - file3 , file4 & file5 to another file ( eg. ora1.log ) as below, can advise what can i do ? thx much in advance. my desired output ============ $vi ora1.log file3.txt file4.txt file5.txt |
Code:
awk '/Jul 21 00:00:00/,/Jul 21 23:59:59/{if ($0 !~ "Jul 21") print >> "ora1.log"}' ora.log |
Quote:
it works , but if I want to extract TODAY 's files ( not just Jul 21 ) , for example , today is Jul 22 , then extract Jul 22 's file , tomorrow extract Jul 23 , can advise what can i do ? thx much |
Quote:
call the command today="$(date +'%b %y')" cmd="awk '/$today 00:00:00/,/$today 23:59:59/{if ($0 !~ \"$today\") print >> \"ora1.log\"}' ora.log" eval "$cmd" |
You can retrieve the date using the date command. To pass variable to awk from the command line, use either the -v option or a proper quoting (alternating single quotes and double quotes):
Code:
awk '/'"$(date +"%b %e")"'/{ scan = 1 } Code:
awk '/Jul 22/{ scan = 1 } |
Quote:
but I am not too understand how to use your program , it is as below ? sorry to my fool . your program ============= awk '/'"$(date +"%b %e")"'/{ scan = 1 } /'"$(date -d tomorrow +"%b %e")"'/{ scan = 0 } scan{ if ($0 !~ "'"$(date +"%b %e")"'") print >> "ora1.log" } > ' ora.log |
Quote:
Code:
awk '/'"$(date +"%b %e")"'/{ scan = 1 } /'"$(date -d tomorrow +"%b %e")"'/{ scan = 0 } scan{ if ($0 !~ "'"$(date +"%b %e")"'") print >> "ora1.log" }' ora.log |
Quote:
I tested it , it work . Sorry to ask again , if I want to extract the files which are within two hours ( not today ) , is it possible ? thx much. |
You can easily figure it out. Take in mind that you can retrieve any date if you use the -d option of the date command. Just specify a time relative to "now" and adjust the format to match that one in the file, for example:
Code:
date -d "2 hours ago" +"%b %e %H:%M" Code:
info date |
Quote:
|
[QUOTE=elainelaw;3617189]thx reply,
I still have two more questions for it 1) the program ignore the year string , if the file is created on last year , it s will output the file , I tried at %Y to "date +"%b %e" , but it seems not work , can advise how to change it so that only check current year file ? 2) if I want to extract the files that only the file name is begins with the word "file" , that means if the files that the file name is not begins with "file" , then do not extract it , can advise what can i do ? |
[QUOTE=elainelaw;3617221]
Quote:
I think I can use your program , it nearly fully meet my requirement , I fixed the above point 2 , for point 1 , can you please kindly help to solve for me ? my program is almost complete . thx a lot . |
Please, can you post the code you're written so far? I will take a look and try to fill the missing points.
|
All times are GMT -5. The time now is 01:16 AM. |