Bash Scipt combines one or more chronologically ordered log files into a single sorted output file, removing all the rows that contain data older than 24 hours from the current system datetime

SamJain
Dec 23, 2020

The required hours of data can be achieved by replacing 24 below with desired hours. Here I am sorting with first 2 columns because that's how my logs are stored say:

2018–08–25 20:24:49 webserver-1:production | Cache warmed
2018–12–30 1:15:01 webserver-2:producction | Server started

cat /dev/null>>result.txt
cat $@ | sort --key=1,2 > tmp.txt
sed -i ‘s/\\n/\n/g’ tmp.txt
awk -v d="$(date -d'24 hours ago' +'%F %T,0000')" '$1" "$2>=d' tmp.txt | tee result.txtif [ -s result.txt ]
then
echo “non_blank file”
else
echo “” > result.txt
fi

--

--

SamJain

Hi, I am Sampada Jain, a system and Cloud Technologies enthusiast. I find learning technologies easy and love to trouble-shoot errors. Passionate Linux learner.