Find out timestamp line and delete if it is older than particular/specific days using bash script
Hi All,
I have a file with MAC addresses in which few lines are having epoch value in end of the line and few lines don't have any epoch value. Now I want to grep only those lines which are having epoch value. So could anybody please help me how to grep only epoch value lines? Please see the below example file. Code:
$ cat mac_address |
I take it you just want to extract the lines that have the timestamps? All that takes is an appropriate regex.
Code:
grep -E '# [0-9]+$' mac_address I recommend taking the time to learn something about regular expressions. You'll be glad you did. Here are a few regular expressions tutorials: http://mywiki.wooledge.org/RegularExpression http://www.grymoire.com/Unix/Regular.html http://www.regular-expressions.info/ |
Invoke:
Code:
awk '$5 !~ /\#/ {if(NF>4) print $5}' mac_address |
Respected David the H. Sir, I am very lucky, you came to this thread.
Code:
grep -E '# [0-9]+$' mac_address Actually I want to find out only those MAC address which are having epoch value(The time we added) and added 90 days ago and delete those lines from mac_address file and then reload the squid3 service. This is because whichever MAC address are given in this file will be having internet access for a couple of days/weeks. So we are giving internet access for maximum 90 days after that those device should not access internet. If anybody wanted internet access even after 90 days then we will add those MAC addresses manually with a new timestamp. But I don't know shell script to achieve this so begging your kind help. So could you please provide a bash script for this i.e searching timestamp, calculating days with current timestamp and if it is reached 90 or more than 90 days then those lines should delete from mac_address file and run the "service squid3 reload" command. Thanks in advance for your kind help. |
Quote:
|
Simply do:
Code:
awk '{if(NF>4) print $0}' mac_address |
Thanks for the reply Shivva,
Quote:
Code:
00:11:22:aa:bb:c1 # Asus WiFi # some more text Code:
grep -E '# [0-9]+$' mac_address |
Hi All,
After reading many threads (especially from David the H.) from linuxquestions.org, I could try for bash script and worked successfully. For that first I found the epoch value for 90 days using Code:
expr $(date -d "90 days" +%s) - $(date +%s) Code:
# cat find_timestamp_and_delete_lines Code:
# cat /etc/squid3/mac_address Once agagin thank you all and Thanks David The H. |
Quote:
Code:
awk 'BEGIN{ time = systime() - (86400*90) } ( $NF ~ /^[0-9]+$/ && $NF < time ) { next } { print }' mac_address I'll let you flesh out the rest of the script yourself. Edit the next day: After looking back at this again with fresh eyes, we can simplify the above code even more: Code:
awk '! ( $NF ~ /^[0-9]+$/ && $NF < systime() - 7776000 )' "$mac_address" |
Now for a few quick comments on the script you posted.
1) Code:
###backup $mac_file file and keep only 7 backup files Code:
cd "$backup_dir" And when using bash or ksh, it's recommended to use ((..)) for numerical tests. http://mywiki.wooledge.org/ArithmeticExpression 2) Code:
timestamp=( $( grep -E '# [0-9]+$' "$mac_file" | awk '{ print $NF }' ) ) It's also not the cleanest way to load an array, since it relies on shell word-splitting. I suggest using mapfile instead, with a process substitution. Code:
mapfile -t timestamp < <( awk '$NF ~ /^[0-9]+$/ { print $NF }' "$mac_file" ) Code:
for i in "${!timestamp[@]}" ; do 4) Code:
$(grep "${timestamp[i]}" "$mac_file" 2>/dev/null)" | mail -s "MAC address deleted" me@mydomain.com If you really need to run a command in a subshell, without expansion, use "(..)" brackets instead. 5) Code:
line2delete=( $( grep "${timestamp[i]}" "$mac_file" ) ) Assuming the grep output is only one line, use simple command substitution instead. Code:
line2delete=$( grep "${timestamp[i]}" "$mac_file" ) Or just use awk, as I posted above. ;) |
Hi Guru,
I was not aware of your reply. I am really sorry....... for this. And still that script is working fine but still I would like to follow your suggestion. Thansk a lot. |
All times are GMT -5. The time now is 12:49 PM. |