awk - remove line in text file with bash if the date is older than 30 days -
i have text file looks this:
test10 2016-05-30 207 test11 2016-06-01 207 test12 2016-07-20 207 test13 2016-07-21 207 test14 2016-07-25 207
and want remove lines text file if date older 30 days. how can this? have read aboud sed
not sure if can done or how go doing it.
the nice thing yyyy-mm-dd alpha sort identical sort order date object -- can generate string representing cutoff date , compare that.
if have gnu date:
cutoff=$(date -d 'now - 30 days' '+%y-%m-%d') awk -v cutoff="$cutoff" '$2 >= cutoff { print }' <in.txt >out.txt && mv out.txt in.txt
it's possible rely on gnu awk (gawk
) rather gnu date:
gawk -v current="$(date +"%y %m %d %h %m %s")" \ 'begin { cutoff_secs = mktime(current) - (60 * 60 * 24 * 30) } { line_secs=mktime(gensub(/-/, " ", "g", $2) " 00 00 00") if (line_secs >= cutoff_secs) { print } }' <in.txt >out.txt && mv out.txt in.txt
note latter implementation starts @ current time 30 days ago, rather @ beginning of day 30 days ago; replace %h %m %s
00 00 00
if don't want behavior.
Comments
Post a Comment