LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash output with timestamp removing duplicates (http://www.linuxquestions.org/questions/programming-9/bash-output-with-timestamp-removing-duplicates-845568/)

sociopathichaze 11-20-2010 09:56 PM

Bash output with timestamp removing duplicates
 
I'm writing a bash script to auto run on boot in Tinycore. This is a watered down version.
Code:

#!/bin/bash
touch database
ifconfig | grep 'HWaddr' | sed 's/://g' | awk '{print$5}' > file
hwclock | awk '{print$2" "$3" "$5" "$4}' > stamp
cat database stamp file > temp
awk '!x[$0]++' temp > database
rm temp stamp file

I need it to either not add the time stamp if the awk finds a duplicate or write over the time with the new time if awk finds a duplicate.

BTW this is all pretty much cut-and-paste scripting so please feel free to comment if you know a more elegant way.

grail 11-20-2010 11:37 PM

What is in database? Or is it created via touch each time it is run?

Also, why is the time stamp (better use date function by the way) on its own line above the mac address?
Normally you would see it in the same line.

Also, as the data would appear to only contain this information, why are the colons (:) being removed?

My general suggestion would be to change all of your output files to variables.

sociopathichaze 11-21-2010 01:15 AM

Quote:

What is in database? Or is it created via touch each time it is run?

Also, why is the time stamp (better use date function by the way) on its own line above the mac address?
Normally you would see it in the same line.

Also, as the data would appear to only contain this information, why are the colons ( being removed?

My general suggestion would be to change all of your output files to variables.
The database file is just a file stored on the usb drive that can later be imported into another program. This is going to be run on a couple hundred machines, each time adding to the file.

The timestamp is on its own line because if it was on the same line as the info, the awk command wouldn't see it as a duplicate because the time is different.

The colons are removed because the program that the database will be read by only reads macs without colons.

My output files are files and not variables because when they are created and deleted, they are written to the usb which make the light flicker, which is useful because the computers aren't going to have monitors hooked up to them. Kind of a ghetto progress bar.

grail 11-21-2010 05:10 AM

How about something that just checks as you go, like:
Code:

ifconfig | awk '/HWaddr/{stamp=strftime("%d %b %T %Y");mac=gensub(/:/,"","g",$NF)}END{while((getline line < "database") > 0){if(++c % 2){t=line;continue}else{if(mac == line){t=stamp;m=mac}else m=line}print t"\n"m}}' > temp
mv temp database
rm temp



All times are GMT -5. The time now is 02:31 PM.