AWK Challenge


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK Challenge
# 1  
Old 02-16-2008
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.
# 2  
Old 02-16-2008
Quote:
What I need to do is select the entire line that matches a variable in this case SQL-2005-UserDB

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

# 3  
Old 02-16-2008
You don't need awk for that.
Code:
grep sql-2005-userdb | tail -1

# 4  
Old 02-16-2008
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.
# 5  
Old 02-16-2008
if you are sure there are always some sql-2005-userdb patterns near the bottom of the file
Code:
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

# 6  
Old 02-16-2008
Quote:
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.
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

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

# 7  
Old 02-17-2008
Quote:
Originally Posted by matrixmadhan
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

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

That looks like it would work. Can you put a variable in the pattern?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Anyone like a challenge?

I have searched through google, and this forum to try and find the answer, but alas, nothing quite hits the whole answer. I am trying to read the last line (or lines) of some log files. I do this often. The files are named sequentially, using the date as part of the file name, and appending... (18 Replies)
Discussion started by: BatterBits
18 Replies

2. UNIX for Advanced & Expert Users

Interesting awk/Perl/sed parsing challenge

I have a log with entries like: out/target/product/imx53_smd/obj/STATIC_LIBRARIES/libwebcore_intermediates/Source/WebCore/bindings/V8HTMLVideoElement.cpp : target thumb C++: libwebcore <=... (8 Replies)
Discussion started by: glev2005
8 Replies

3. UNIX for Dummies Questions & Answers

Touch Challenge

I've been given a directory full of subdirectories full of logfiles of the same name: /logfiles/day1/file1/blockednodes.csv day1-14 file1-48 The above is the actual directory structure for 14 days worth of a logfile that is generated every 30 minutes. It's been done this way to preserve the... (15 Replies)
Discussion started by: Cludgie
15 Replies

4. Shell Programming and Scripting

PS1 challenge

Ok then i Have a challenge for you : Give me PS1 so that it always display the least 2 levels of directory (except if i am above of course) I want it this way : so if i go to / /home/ /home/user /home/user/whatever /home/user/whatever1/whatever2 my PS1 should respectively... (12 Replies)
Discussion started by: ctsgnb
12 Replies

5. Shell Programming and Scripting

Geo Weather Challenge

Hi everybody, I'm new to these forums and this is my first post. A couple days ago I was trying to find a simple script that would return an individual's local weather conditions using I.P. based geolocation. After many failed search attempts, I began my quest to create this for myself. I have to... (0 Replies)
Discussion started by: o0110o
0 Replies

6. Shell Programming and Scripting

regex challenge

Here's a regex substitution operation that has stumped me with sed: How do you convert lines like this: first.key ?{x.y.z} second.key ?{xa.ys.zz.s} third.key ?{xa.k} to: first.key ?{x_y_z} second.key ?{xa_ys_zz_s} third.key ?{xa_k} So i'm basically converting all the... (11 Replies)
Discussion started by: neked
11 Replies

7. UNIX for Dummies Questions & Answers

A challenge for you sed/awk wizards...

Here's a challenge for you wizards... I have a file formatted as follows; $ What I need to output is; 87654321 Bobby One 12345678 Bobby One 09876543 Bobby One 1107338 Bobby! Two Any Ideas how I can do this? I've tried sed but I'm not sure if perl might be a better way to... (2 Replies)
Discussion started by: th3g0bl1n
2 Replies

8. UNIX for Advanced & Expert Users

safeword challenge

Hi, there are some servers here at work which issue a Safeword challenge after I login. Can anyone tell me exactly how the challenge/response system works? In particular, how are the valid keys decided? (2 Replies)
Discussion started by: blowtorch
2 Replies

9. UNIX for Dummies Questions & Answers

Loaded question - anyone up to the challenge?

Hello everyone. I am new to these forums and also new to Unix. And by saying "new to Unix" I mean I have never used it and 10 minutes ago was asked to start learning. So here I am. I was wondering if anyone could help me find out how long it would take to write code in Unix that will do the... (1 Reply)
Discussion started by: idesaj
1 Replies

10. UNIX for Dummies Questions & Answers

A find and sort challenge...

Hi, I need to generate a listing of files that have been changed since x day. the listing is to be sorted by date. I managed to get the 1st requirement using the find command : find . -mtime -100 -type f -ls but I don't know how to sort the ls listing by date. :( The challenge comes... (4 Replies)
Discussion started by: ppohz
4 Replies
Login or Register to Ask a Question

Featured Tech Videos