Issue
I'm building a bash script to check all rows inside local file older than X days. I have this file:
ANDREA_20231002_050004_10800s+60s_rec-uk-london-03.txt
ANDREA_20231005_050004_10800s+60s_rec-uk-london-03.txt
ANDREA_20231009_050004_10800s+60s_rec-uk-london-03.txt
ANDREA_20231011_020004_10800s+60s_rec-uk-london-03.txt
ANDREA_20231012_000002_10800s+60s_rec-uk-london-03.txt
ANDREA_20231012_210002_10800s+60s_rec-uk-london-03.txt
ANDREA_20231013_060003_10800s+60s_rec-uk-london-03.txt
I should generate a new file to view only rows older than two day/three from today. For example, if today is 20231014 I should view all rows from 20231002 to 20231011. I have created this script but is not correct because I see only lines older than two days but not those older:
yesterday="`date +%Y%m%d --date="-2 day"`"
cat myfile | grep $yesterday
can you help me, please :-)
Solution
Using GNU AWK
$ awk -F_ -v x=2 '
BEGIN{
print "today is", strftime("%Y%m%d")
date = strftime("%Y%m%d",systime()-(x*60*60*24))
}
$2 < date
' file
today is 20231014
ANDREA_20231002_050004_10800s+60s_rec-uk-london-03.txt
ANDREA_20231005_050004_10800s+60s_rec-uk-london-03.txt
ANDREA_20231009_050004_10800s+60s_rec-uk-london-03.txt
ANDREA_20231011_020004_10800s+60s_rec-uk-london-03.txt
Answered By - ufopilot Answer Checked By - Senaida (WPSolving Volunteer)