AWK Challenge

I have the following text

Microsoft iSCSI Initiator version 2.0 Build 3497

Targets List:
iqn.2001-05.com.equallogic:0-8a0906-daef43402-138000002a4477ba-grsrv12-extra
iqn.2001-05.com.equallogic:0-8a0906-986f43402-520000002b447951-exchange
iqn.2001-05.com.equallogic:0-8a0906-a97ca4602-1dcf35dc2a647b3c-sql-2005-userdb-2008-02-13-23:59:03.85
iqn.2001-05.com.equallogic:0-8a0906-9ecca4602-cfef35dc2d547b4b-exchange-2008-02-14-17:00:12.95
iqn.2001-05.com.equallogic:0-8a0906-c20ca4602-850f35dc2e447b51-sql-2005-userdb-2008-02-14-23:59:12.99
iqn.2001-05.com.equallogic:0-8a0906-d97ca4602-94af35dc30a47b66-sql-2005-userdb-2008-02-15-23:59:03.110
iqn.2001-05.com.equallogic:0-8a0906-3d4ca4602-c54f35dc2eb47b59-virtual-data-2008-02-15-08:29:56.101
iqn.2001-05.com.equallogic:0-8a0906-606ca4602-578f35dc2ed47b59-sql-2005-userdb-2008-02-15-08:39:18.102
iqn.2001-05.com.equallogic:0-8a0906-802ca4602-4c0f35dc2ef47b59-sql-2005-userdb-2008-02-15-08:47:46.103
iqn.2001-05.com.equallogic:0-8a0906-872ca4602-d92f35dc2f147b59-sql-2005-userdb-clone-2008:02:15-08:49:51.491
iqn.2001-05.com.equallogic:0-8a0906-ff1ca4602-834f35dc31c47b72-sql-2005-userdb-2008-02-16-13:48:17.115
The operation completed successfully.
(each one is a full line)
What I need to do is select the entire line that matches a variable in this case SQL-2005-UserDB

What I need to do is select only the entries with a SQL-2005-UserDB and only the line that that is the oldest of that day.

I need the full line like

iqn.2001-05.com.equallogic:0-8a0906-ff1ca4602-834f35dc31c47b72-sql-2005-userdb-2008-02-16-13:48:17.115

I would like to use AWK..most of my other parsing is being done by awk where I have my AWK code in another file and I am calling the file using AWk -f file > another file.

I need help created in file with the code that will do what I need. I am struggling with the best way to accomplish this.

awk '/SQL-2005-UserDB/ { print }' filename

You don't need awk for that.

grep sql-2005-userdb | tail -1

If I do a awk '/SQL-2005-UserDB/ { print }' filename won't that print each SQL-2005-UserDB that is found. I the file its looking at there may be multiple SQL-2005-userDB. Out of all it finds I need to print only the one that matches the current date and one that is the latest time.

if you are sure there are always some sql-2005-userdb patterns near the bottom of the file

start=1000
while true
do
    result=`tail -$start file | awk '/sql-2005-userdb/{l=$0}END{if(l) print l}'`
    if [ -z "$result" ];then
        start=`echo $start + 1000|bc`    
    else
        echo $result && exit
    fi
done

Yes it would do.

Assuming the logs are generated in a timely fashion so the last message in the file would have the latest timestamp

awk '/pattern/{a=$0}END{ print a }' filename

That looks like it would work. Can you put a variable in the pattern?

yes.

shell_var="this is the pattern to check"
awk -v var=$shell_var '/var/{a=$0}END{ print a }' filename

I'm not sure I can do that. Maybe a little more explaining on my part.

The base script is a batch file that sets up the variables and calls several awk programs. (see attached). I have other variables like the one BCKDRIVE which I pass in another awk program file that sets a print result:
print "Assign " ENVIRON["BCKDRIVE"]

That one puts the proper designation in that string, but I can seem to get it to work in the pattern.

Hi,

I think below one may be ok for you.

Fist get the desired line with line number, put in temp file.
Second get the latest record.
Third only pring the latest record line number.
code:

grep sql-2005-userdb file | cat -n > temp
line=`cat temp | sed  's/\([0-9][0-9][0-9][0-9]\)-\([0-9][0-9]\)-\([0-9][0-9]\)-\([0-9][0-9]\):\([0-9][0-9]\):\([0-9][0-9]\)\.\([0-9]*\)$/ \1\2\3\4\5\6\7/' | sort +2 | tail -1 | awk '{print $1}'`
sed -n "${line}p" temp