LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-18-2013, 03:41 AM   #1
veda92
LQ Newbie
 
Registered: Jul 2013
Posts: 8

Rep: Reputation: Disabled
extract lines using date range


I wish to extract lines from files based on date from first two fields eg: lines with todays date or week ago etc
eg of file:
Jul 17 gfgf gfdg sdfg Jul 11
Jul 17 sdfd dfg Jul 11 fdf
Jul 17 ffgg ggg Jul 16 gfg
Jul 16 gfg ffg fdg Jul 17

I used awk suppose for todays date:
k=`date '+%B %d'`
awk -v m="$k" '($1,$2) == m' file

For a week before:
k=`date --date=="7 days ago" +%b\ %e`
awk -v m="$k" '($1,$2) == m' file

For user to enter date for search
echo "enter month"
read mon
echo "enter date"
read dat
awk -v "m=$mon" "d=$dat" '$1 == m && $2 == d' file

where am i going wrong in above scripts
Also if there is other way i could do this
 
Old 07-19-2013, 02:15 AM   #2
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
# For Jul 19 - Find lines that have the string "Jul 19" in file.txt
Code:
grep "`date '+%b %d'`" file.txt
# For Jul 19 - Find lines that have the string "Jul 12" 7 days ago in file.txt
Code:
grep "`date '+%b %d' --date='7 days ago'`" file.txt
# For Jul 19 - Find lines that have either string "Jul 12" 7 days ago or the string "Jul 19" in file.txt
Code:
grep -E -e "`date '+%b %d'`|`date '+%b %d' --date='7 days ago'`" file.txt
# For first two columns today
Code:
awk -v date="$(date +"%b %d")" '{if ($1" "$2==date) {print $0}}' file.txt
# For first two columns last week
Code:
awk -v date="$(date +"%b %d" --date="7 days ago")" '{if ($1" "$2==date) {print $0}}' file.txt
# For first two columns last week or today
Code:
awk -v date="$(date +"%b %d" --date="7 days ago")" -v date2="$(date +"%b %d")" '{if ($1" "$2==date || $1" "$2==date2) {print $0}}' file.txt
If this helps please add a rep on the left.

Thanks

Last edited by Sydney; 07-19-2013 at 03:05 AM. Reason: Added code tags.
 
1 members found this post helpful.
Old 07-22-2013, 05:58 AM   #3
veda92
LQ Newbie
 
Registered: Jul 2013
Posts: 8

Original Poster
Rep: Reputation: Disabled
Smile

thanks sydney was stuck for about two days
 
  


Reply



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
Display lines of two date range from syslog file p2006.prashant Programming 4 07-03-2013 04:17 PM
Can tar do a date range? ghughes5669 Linux - Software 5 10-25-2012 05:03 PM
[SOLVED] How do I cat a range of lines? raybies Linux - Newbie 3 03-14-2012 09:29 AM
Command to extract a range of bytes from a file calande Linux - General 2 10-02-2011 11:58 AM
tar a date range rayia Programming 3 01-03-2006 07:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:12 AM.

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
Open Source Consulting | Domain Registration