Search the last instance of a string in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search the last instance of a string in a file
# 1  
Old 01-27-2009
Bug Search the last instance of a string in a file

I have a big database log file which keepsgrowing daily. OS is HP UX. Here is a small part of it:

Tue Jan 27 04:03:22 2009
[2009/01/27@04:03:22.906-0500] : add session begin for mfgeb on /dev/pts/th.
[2009/01/27@04:03:22.909-0500] : Converting relative path to absolute path.
[2009/01/27@04:03:22.967-0500] : add session end.

Tue Jan 27 04:03:29 2009
[2009/01/27@04:03:29.345-0500] : list session begin for mfgeb on /dev/pts/th.
[2009/01/27@04:03:29.356-0500] : list session end.

Tue Jan 27 04:03:46 2009
[2009/01/27@04:03:46.262-0500] : add session begin for mfgeb on /dev/pts/tb.
[2009/01/27@04:03:46.331-0500] : add session end.

Tue Jan 27 04:03:49 2009
[2009/01/27@04:03:49.964-0500] : list session begin for mfgeb on /dev/pts/tb.
[2009/01/27@04:03:49.972-0500] : list session end.

Tue Jan 27 04:04:32 2009
[2009/01/27@04:04:32.147-0500] : add session begin for mfgeb on /dev/pts/tg.
[2009/01/27@04:04:32.182-0500] : add session end.


I need to search last instance of string "add session begin" in the file and then print date from that line. In the case here, it's 27th Jan 2009.
Someone please help.
# 2  
Old 01-27-2009
If you have the command "tac", just do:
Code:
tac FILE |awk '/add session begin/ { print $1 }' | head -1

Otherwise, you can do this
Code:
awk '/add session begin/ { lastseen=$1; } END { print lastseen }' FILE


Last edited by otheus; 01-28-2009 at 05:34 AM.. Reason: fixed per jim's post
# 3  
Old 01-27-2009
I think "rev" should be "tac" - rev reverses the order of characters
From the man page:
Quote:
The rev utility copies the specified files to the standard output, reversing the order of characters in every line.
tac reverses the order of the lines, printing the last line first.
# 4  
Old 01-27-2009
Another possible solution (a long n crude one though Smilie ) :
grep -i "add session begin" a.txt|tail -1|cut -c 2-11
where,
a.txt = sample log entry posted in the original query
# 5  
Old 01-28-2009
"rev" reverse the order of characters line-wise
# 6  
Old 01-28-2009
"tac" is not available on HP UX
# 7  
Old 01-28-2009
Gaurav,
Thanks friend, your solution is working fine.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

2. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

3. Shell Programming and Scripting

Search for / and insert \ for every instance

Hi, I want to do a sed (linux) or the alternative in PHP to insert a backslash in front of every occurrence of every forward slash ex. /archive/data/stanley --> \/archive\/data\/stanley I appreciate it! (4 Replies)
Discussion started by: deadyetagain
4 Replies

4. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

5. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

6. Shell Programming and Scripting

Search text file, then grep next instance of string

I need to be able to search for a beginning line header, then use grep or something else to get the very next instance of a particular string, which will ALWAYS be in "Line5". What I have is some data that appears like this: Line1 Line2 Line3 Line4 Line5 Line6 Line7 Line1 Line2 ...... (4 Replies)
Discussion started by: Akilleez
4 Replies

7. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

8. Shell Programming and Scripting

Search for last instance of word in a file

Hi I'm trying to search for the last instance of the word 'cache' in a HTML file that I have downloaded from YouTube. I'm using the following syntax, but an error is thrown when I try it. grep -f "cache" Also I wish to append the above grep command to the below so that the search for cache... (3 Replies)
Discussion started by: colmbell
3 Replies

9. Shell Programming and Scripting

Deleting a line from a file based on one specific string instance?

Hello! I need to delete one line in a file which matches one very precise instance of a string only. When searching the forum I unfortunately only found a solution which would delete each line on which a particular string occurs. Let's assume I have a file composed of thousands of lines... (4 Replies)
Discussion started by: Black Sun
4 Replies

10. Shell Programming and Scripting

appending string to text file based on search string

Hi, I need to append string "Hi" to the beginning of the lines containing some specific string. How can I achieve that? Please help. Malay (1 Reply)
Discussion started by: malaymaru
1 Replies
Login or Register to Ask a Question