LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-21-2009, 06:44 AM   #1
elainelaw
Member
 
Registered: Jan 2008
Posts: 258

Rep: Reputation: 30
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
 
Old 07-21-2009, 06:59 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Code:
awk '/Jul 21 00:00:00/,/Jul 21 23:59:59/{if ($0 !~ "Jul 21") print >> "ora1.log"}' ora.log
 
Old 07-22-2009, 09:27 AM   #3
elainelaw
Member
 
Registered: Jan 2008
Posts: 258

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by colucix View Post
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
 
Old 07-22-2009, 09:58 AM   #4
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware 14.1 64-bit, slackware 14.2 64-bit, SystemRescueCD
Posts: 420

Rep: Reputation: 86
Quote:
Originally Posted by elainelaw View Post
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"
 
Old 07-22-2009, 10:03 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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
 
Old 07-22-2009, 10:43 AM   #6
elainelaw
Member
 
Registered: Jan 2008
Posts: 258

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by colucix View Post
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
 
Old 07-22-2009, 11:18 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by elainelaw View Post
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
 
Old 07-22-2009, 01:40 PM   #8
elainelaw
Member
 
Registered: Jan 2008
Posts: 258

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by colucix View Post
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.
 
Old 07-22-2009, 01:46 PM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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
 
Old 07-22-2009, 09:52 PM   #10
elainelaw
Member
 
Registered: Jan 2008
Posts: 258

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by colucix View Post
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

Last edited by elainelaw; 07-22-2009 at 10:41 PM.
 
Old 07-22-2009, 10:22 PM   #11
elainelaw
Member
 
Registered: Jan 2008
Posts: 258

Original Poster
Rep: Reputation: 30
[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 ?
 
Old 07-23-2009, 07:03 AM   #12
elainelaw
Member
 
Registered: Jan 2008
Posts: 258

Original Poster
Rep: Reputation: 30
[QUOTE=elainelaw;3617221]
Quote:
Originally Posted by elainelaw View Post
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 .
 
Old 07-23-2009, 07:19 AM   #13
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Please, can you post the code you're written so far? I will take a look and try to fill the missing points.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to extract spec file from rpm file nayankk Programming 18 11-12-2015 03:11 PM
How to do search & replace on a text file--need to extract URLs from a sitemap file Mountain Linux - General 4 08-07-2015 11:52 AM
how to extract the name of the file bahadur Programming 9 03-22-2005 07:48 PM
Extract file Electboy Linux - General 1 07-21-2003 06:51 PM
How do you extract a file? Neb Linux - Newbie 9 05-14-2003 04:46 PM


All times are GMT -5. The time now is 03:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration