bkbn
bkbn

Reputation: 41

How to delete identical lines in my output files?

I have this script :

#!/bin/bash

ps -eo lstart,pid,cmd --sort lstart | while read line 2> /dev/null
        do
        if [ "$(date -d "${line::24}" "+%Y%m%d%H%M%S")" -gt "$(date -d "Thu Apr  7 00:55:38" "+%Y%m%d%H%M%S")" ] 2> /dev/null
        then echo "Date : $(date -d "${line::24}" "+%d/%m/%Y %H:%M:%S") | PID & CMD : ${line:25:29}" >> process.log 2> /dev/null
        fi 2> /dev/null
        done

sort process.log | uniq > process.log

#sort process.log | uniq -u | tee process.log

My script runs automatically every 10 seconds, so I would like the identical lines to be deleted. As you can see, I tried with uniq but it doesn't work. I would like all lines in my file to be deleted if they are identical.

As I did, the second time the script is executed, there is nothing in the output file and I don't understand why.

I would also like nothing to be displayed in my terminal when the script runs. I used tee but when executing the uniq command, it returns an output in my terminal... How to remove it?

I thank you in advance for your help and wish you a good day Thanks a lot

Upvotes: 0

Views: 43

Answers (1)

KamilCuk
KamilCuk

Reputation: 141030

You should not parse ps output ever, especially not of lstart. Also, you are running date in the loop all the time, again and again.

I think something along this would be better to do:

some_date=$(date -d "Thu Apr  7 00:55:38" +%s)
now=$(date +%s)
how_much_time_ago=$(( now - some_date ))
ps -eo etimes,pid,cmd --sort etimes |
awk -v v="$how_much_time_ago" '$1 > v' |
while IFS=' ' read line etimes pid cmd; do
     printf "Date : %s | PID & CMD : %s %s\n" \
         "$(date -d "$((now - etimes))" "+%d/%m/%Y %H:%M:%S")" \
         "$pid" "$cmd"
done |
sort |
uniq > process.log

Note that you can pipe the output of a while .... done | stuff loop to another thing normally. Instead of sprinkling 2>/dev/null everywhere, try to actually solve the issue, not hide the error.

Upvotes: 0

Related Questions