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

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

SamJain
SamJain

Written by 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.

Responses (1)

Write a response