LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Extract file (https://www.linuxquestions.org/questions/linux-newbie-8/extract-file-741649/)

elainelaw 07-21-2009 05:44 AM

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

colucix 07-21-2009 05:59 AM

Code:

awk '/Jul 21 00:00:00/,/Jul 21 23:59:59/{if ($0 !~ "Jul 21") print >> "ora1.log"}' ora.log

elainelaw 07-22-2009 08:27 AM

Quote:

Originally Posted by colucix (Post 3615007)
Code:

awk '/Jul 21 00:00:00/,/Jul 21 23:59:59/{if ($0 !~ "Jul 21") print >> "ora1.log"}' ora.log

thx reply ,

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

vonbiber 07-22-2009 08:58 AM

Quote:

Originally Posted by elainelaw (Post 3616357)
thx reply ,

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

how about storing the date (month and year) in a variable, then
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"

colucix 07-22-2009 09:03 AM

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 }
> /'"$(date -d tomorrow +"%b %e")"'/{ scan = 0 }
> scan{ if ($0 !~ "'"$(date +"%b %e")"'") print >> "ora1.log" }
> ' ora.log

This is a litlle more complicate to decipher, but after the command substitutions it is:
Code:

awk '/Jul 22/{ scan = 1 }
/Jul 23/{ scan = 0 }
scan{ if ($0 !~ "Jul 22")
  print >> "ora1.log" }' ora.log


elainelaw 07-22-2009 09:43 AM

Quote:

Originally Posted by colucix (Post 3616405)
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 }
> /'"$(date -d tomorrow +"%b %e")"'/{ scan = 0 }
> scan{ if ($0 !~ "'"$(date +"%b %e")"'") print >> "ora1.log" }
> ' ora.log

This is a litlle more complicate to decipher, but after the command substitutions it is:
Code:

awk '/Jul 22/{ scan = 1 }
/Jul 23/{ scan = 0 }
scan{ if ($0 !~ "Jul 22")
  print >> "ora1.log" }' ora.log


thx the prompt reply ,

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

colucix 07-22-2009 10:18 AM

Quote:

Originally Posted by elainelaw (Post 3616452)
thx the prompt reply ,

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

You put an extra >. Here is the same in 1 line:
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

elainelaw 07-22-2009 12:40 PM

Quote:

Originally Posted by colucix (Post 3616503)
You put an extra >. Here is the same in 1 line:
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

thx reply ,

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.

colucix 07-22-2009 12:46 PM

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"
For more information about time specifications take a look at
Code:

info date

elainelaw 07-22-2009 08:52 PM

Quote:

Originally Posted by colucix (Post 3616685)
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"
For more information about time specifications take a look at
Code:

info date

deleted

elainelaw 07-22-2009 09:22 PM

[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 ?

elainelaw 07-23-2009 06:03 AM

[QUOTE=elainelaw;3617221]
Quote:

Originally Posted by elainelaw (Post 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 ?

thx colucix,

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 .

colucix 07-23-2009 06:19 AM

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.