1

I'm trying to summarise this list to contain only 5 days worth of data, from today to 5 days ago. I tried using awk but to no avail. There is no need to stick to awk too. This is the output:

This is my awk code to search for today's date in a row:

awk -F " " -v todate="$(date +%Y-%m-%d)" 'todate' output.txt

This is my while loop code:

while read line
    do
    if (("$(date +%Y-%m-%d)" in $line))
        then
            echo $line
done < output.txt

This is the output.txt file:

2018-05-09 14:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-10 03:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-10 16:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-11 05:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-11 18:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-12 07:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-12 20:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-13 09:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-13 22:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-14 11:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-15 00:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-15 08:00:00 UTC+0800;swlt;;BKP_USGSN03_OLP_MK8;CXS101289_R73B13(001-00-12)
2018-05-15 08:00:00 UTC+0800;sau;;2025,licensed_sau_normal;CXS101289_R73B13(001-00-12)
2018-05-15 08:00:00 UTC+0800;sau_lte;;1025,licensed_sau_lte_normal;CXS101289_R73B13(001-00-12)
2018-05-15 08:00:00 UTC+0800;pdp;;2001,licensed_pdp_normal;CXS101289_R73B13(001-00-12)
2018-05-15 13:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-16 02:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-16 15:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-17 04:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-17 17:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-18 06:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-18 19:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-19 08:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-19 21:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-20 10:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-20 23:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-21 12:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-22 01:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-22 14:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-23 03:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2
  • Given the clarifying comments on the answers so far, I think it’d be helpful to edit in your “date -d” restriction.
    – Jeff Schaller
    May 23, 2018 at 11:22
  • I'm afraid that is not possible, because the list is non-static, it updates everyday in a crontab schedule. I have checked the man page for the date command in my system and found no other options that could call up the dates of the past 5 days.
    – tthhss
    May 24, 2018 at 1:14

3 Answers 3

1

In the following script, the function mktime creates a unix timestamp based on the 1st and 2nd field of the file that can be compared with the reference date from date given in argument to awk:

awk -v startdate=$(date -d '5 days ago' +%s) '{d=$1 OFS $2; gsub("[-:]", " ", d); t=mktime(d)} t>startdate' file
1

GNU awk approach:

awk -F'[:-]' -v from_date=$(date -d'-4 days' +%s) \
'mktime(sprintf("%d %d %d %d %d %d", $1, $2, $3, $4, $5, $6)) >= from_date' file.txt

Sample output:

2018-05-18 06:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-18 19:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-19 08:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-19 21:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-20 10:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-20 23:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-21 12:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-22 01:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-22 14:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-23 03:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
1
  • Hi thank you so much for helping. Im a first time user, so pardon me if i type unclear in this forum. I have a problem with the argument that you're passing into your "date" command. The system does not recognise -d. when i apply this command: $ date -d'-4 days' +%s The result is: date: illegal option -- d date: illegal option -- - date: illegal option -- 4 date: illegal option -- date: illegal option -- d date: invalid argument -- ys usage: date [-u] mmddHHMM[[cc]yy][.SS] date [-u] [+format] date -a [-]sss[.fff]
    – tthhss
    May 23, 2018 at 8:24
0

Using AWK:

OPTION 1:

DATE=`date --date='5 days ago' +"%Y-%m-%d"`;awk '/'$DATE'/,EOF { print $0 }' file.txt

OPTION 2:

DATE=`date +%Y-%m-%d -d "5 day ago"`;awk '/'$DATE'/,EOF { print $0 }' file.txt

OPTION 3:

awk '/2018-05-18/,EOF { print $0 }' file.txt

Output:

2018-05-18 06:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-18 19:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-19 08:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-19 21:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-20 10:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-20 23:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-21 12:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-22 01:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-22 14:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-23 03:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
9
  • Thanks so much for helping! I'm a first time user and i amazed at the help. However, i havent been able to use the --date argument in my date command. It says Illegal option.usage: date [-u] mmddHHMM[[cc]yy][.SS] date [-u] [+format] date -a [-]sss[.fff]
    – tthhss
    May 23, 2018 at 8:34
  • Updated my answer , Try option 2
    – Siva
    May 23, 2018 at 8:40
  • something's wrong. -d isn't working either. I'm getting DATE=2018-05-23: Command not found. DATE: Undefined variable. I suspect the date command in my system does not take in -d argument. This might help, the usage is specified as: "usage: date [-u] mmddHHMM[[cc]yy][.SS] date [-u] [+format] date -a [-]sss[.fff]"
    – tthhss
    May 23, 2018 at 8:43
  • Which distro of Linux are you using?
    – Siva
    May 23, 2018 at 8:47
  • I think it means that only -u and -a arguments are legal, just a wild guess.
    – tthhss
    May 23, 2018 at 8:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .