LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   extract lines using date range (https://www.linuxquestions.org/questions/programming-9/extract-lines-using-date-range-4175470038/)

veda92 07-18-2013 03:41 AM

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

Sydney 07-19-2013 02:15 AM

# 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

veda92 07-22-2013 05:58 AM

thanks sydney was stuck for about two days


All times are GMT -5. The time now is 06:16 PM.