How to grep for a word and display last transection?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to grep for a word and display last transection?
# 1  
Old 07-29-2015
How to grep for a word and display last transection?

Hi,

When we "grep" for a word in a file, it returns the last lines containing the word that we searched for.
Is there a way to display last line to grep.

Thanks


Ex log.



Ex. logname.log
Code:
2015-07-29 06:43:07.023|BETA |2015-07-29 06:43:06|10.112.128.100|input|1507290643060096|tran_74037637|014|0001|0000|Complete|S|598 ms
2015-07-29 06:43:07.691|BETA |2015-07-29 06:43:07|10.112.128.100|input|1507290643060104|66914318823|014|0001|0000|Complete|S|605 ms
2015-07-29 06:43:09.726|BETA |2015-07-29 06:43:09|10.112.130.195|output|20150729064308005510|tran_36248287|002|0001|0000|Complete|S|155 ms
2015-07-29 06:43:14.670|BETA |2015-07-29 06:43:14|10.112.128.216|input|K01832290715064313|66948069537|006|0001|0000|Complete|S|168 ms
2015-07-29 06:43:15.492|BETA |2015-07-29 06:43:15|10.112.128.216|output|K01832290715064313|66948069537|006|0001|0000|Complete|S|170 ms
2015-07-29 06:43:17.182|BETA |2015-07-29 06:43:17|10.112.128.163|input|2015072906431688276|tran_15987580|025|0003|0000|Complete|S|153 ms
2015-07-29 06:43:17.778|BETA |2015-07-29 06:43:17|10.112.130.195|input|20150729064317057787|tran_67971793|002|0005|0000|Complete|S|135 ms
2015-07-29 06:43:20.244|BETA |2015-07-29 06:43:20|10.112.128.163|input|2015072906431932059|tran_59129556|025|0003|0000|Complete|S|141 ms
2015-07-29 06:43:23.348|BETA |2015-07-29 06:43:22|10.112.128.100|output|1507290642339828|tran_81952995|014|0003|0000|Complete|S|512 ms
2015-07-29 06:43:24.059|BETA |2015-07-29 06:43:23|10.112.128.100|output|1507290643160202|tran_46417802|014|0001|0000|Complete|S|509 ms
2015-07-29 06:43:24.453|BETA |2015-07-29 06:43:24|10.112.128.216|input|K06089290715064323|tran_39676937|006|0001|0000|Complete|S|162 ms
2015-07-29 06:43:25.234|BETA |2015-07-29 06:43:25|10.112.128.216|output|K06089290715064323|tran_39676937|006|0001|0000|Complete|S|155 ms
2015-07-29 06:43:25.733|BETA |2015-07-29 06:43:25|10.112.128.100|input|1507290643250284|tran_68664372|014|0001|0000|Complete|S|418 ms
2015-07-29 06:43:30.360|BETA |2015-07-29 06:43:29|10.112.128.100|output|1507290642590042|tran_88385218|014|0003|0000|Complete|S|463 ms
2015-07-29 06:43:31.795|BETA |2015-07-29 06:43:31|10.112.128.163|input|2015072906433127637|tran_16275947|025|0001|0000|Complete|S|153 ms
2015-07-29 06:43:35.068|BETA |2015-07-29 06:43:34|10.112.130.195|input|20150729064334024185|tran_54977645|002|0004|0000|Complete|S|265 ms
2015-07-29 06:43:35.606|BETA |2015-07-29 06:43:35|10.112.128.100|input|1507290643340368|tran_16613660|014|0003|0000|Complete|S|408 ms
2015-07-29 06:43:37.515|BETA |2015-07-291157394|tran_74811900|014|0001|0000|Complete|S|541 ms
2015-07-29 07:11:20.283|BETA |2015-07-29 07:11:19|10.112.128.100|output|1507290711127261|tran_81608285|014|0001|0000|Complete|S|552 ms
2015-07-29 07:11:21.546|BETA |2015-07-29 07:11:20|10.112.128.100|output|1507290711147341|tran_27133954|014|0001|0000|Complete|S|1208 ms
2015-07-29 07:11:21.550|BETA |2015-07-29 07:11:20|10.112.128.100|output|1507290711147338|66949212162|014|0001|0000|Complete|S|834 ms
2015-07-29 07:11:21.698|BETA |2015-07-29 07:11:21|10.112.128.100|output|1507290711137276|tran_31365066|014|0001|0000|Complete|S|658 ms


Code:
grep "\|014\|"  logname.log


output
Code:
2015-07-29 07:11:21.698|BETA |2015-07-29 07:11:21|10.112.128.100|output|1507290711137276|tran_31365066|014|0001|0000|Complete|S|658 ms


Last edited by Scrutinizer; 07-29-2015 at 09:07 AM.. Reason: additional code tags
# 2  
Old 07-29-2015
Try:
Code:
grep '|014|' logname.log|tail -n 1

# 3  
Old 07-29-2015
Code:
awk '/|014|/ {s = $0} END {print s}' logname.log

# 4  
Old 07-29-2015
@Srinishoo, note that awk uses ERE (Extended Regular Expressions) and therefore the vertical bars are special and need to be escaped, whereas grep use BRE (Basic Regular Expressions), where they are ordinary characters, so:
Code:
awk '/\|014\|/ ...


Last edited by Scrutinizer; 07-29-2015 at 07:01 PM..
# 5  
Old 07-29-2015
Code:
tac logname.log | grep -m1 '|014|'

# 6  
Old 07-29-2015
Code:
sed '/\|014\|/h;$!d;g' logname.log

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep word after last occurance of string and display next few lines

Hi, I wanted to grep string "ERROR" and "WORNING" after last occurrence of String "Starting" only and wanted to display two lines after searched ERROR and WORNING string and one line before. I have following cronjob log file "errorlog" file and I have written the code for same in Unix as below... (17 Replies)
Discussion started by: nes
17 Replies

2. Shell Programming and Scripting

Need a word which just comes next to after grep of a specific word

Hi, Below is an example : ST1 PREF: int1 AVAIL: int2 ST2 PREF :int1 AVAIL: int2 I need int1 to come in preferred variable while programming and int2 in available variable Please help me doing so Best regards, Vishal (10 Replies)
Discussion started by: Vishal_dba
10 Replies

3. Shell Programming and Scripting

grep part of word or Another word from a string

Hi all, FileOne family balance >>>>> 0 0 0 0 java.io.FileNotFoundException: Settings.xml (No such file or directory) at java.io.FileInputStream.open(Native Method) .. .... ..... ..... java.lang.NullPointerException ... ..... ...... Stacktrace: at... (2 Replies)
Discussion started by: linuxadmin
2 Replies

4. Shell Programming and Scripting

Grep out specific word and only that word

ok, so this is proving to be kind of difficult even though it should not be. say for instance I want to grep out ONLY the word fkafal from the below output, how do I do it? echo ajfjf fjfjf iafjga fkafal foeref afoafahfia | grep -w "fkafal" If i run the above command, i get back all the... (4 Replies)
Discussion started by: SkySmart
4 Replies

5. Shell Programming and Scripting

Finding a "word" through grep but display line above?

Hi guys. I am trying to perform a search using grep. I get my grep to work, but need to "awk" a Process Number that is 2 lines above... Example: I run a query on my TSM server for Processes that are "Waiting" for something...it returns this: Process Number: 32,881 Process... (14 Replies)
Discussion started by: Stephan
14 Replies

6. UNIX for Dummies Questions & Answers

how to grep the word and display only the second word from it

hi, consider the below line in a text file, 'Y',getdate(),'N','V',NULL ..... 'N',getdate(),'Y','D',NULL ..... 'Y','N','Y',getdate(),'Y','D',NULL .... as u see above, i want only the second word after the getdate() word... getdate() will not come 2nd word alwys it may be any position but i... (11 Replies)
Discussion started by: prsam
11 Replies

7. Shell Programming and Scripting

grep display word only

Folks, is it possible to display only words with grep (or any built-in ultility)? I have more than 1 pattern to search, say apple & orange The text goes like this: So I need to display all the words starting with apple or orange The output should be: Any idea? (7 Replies)
Discussion started by: bsddaemon
7 Replies

8. Shell Programming and Scripting

How to Grep for particular word and display..

Hi Guru's.... I've one log file in all my systems which writes the backup information.. I'have written a command like this: ssh -l ora${sid} ${primaryhost} "tail -50 /oracle/$ORACLE_SID/newbackup/END_BACKUP.log" |grep 'insert' |tail -1| awk '{print $7}' We have nearly 50 systems in our... (2 Replies)
Discussion started by: suri.tyson
2 Replies

9. UNIX for Dummies Questions & Answers

how to grep for a word and display only the word

Hi, When we "grep" for a word in a file, it returns the lines containing the word that we searched for. Is there a way to display only the words and not the entire line containing them. Thanks Ananth (6 Replies)
Discussion started by: ananthmm
6 Replies

10. UNIX for Dummies Questions & Answers

grep a word and display its column

Hi, I need idea about this, say I have this line: 05 21 * * 0,6 /user/clean.desktop.sh > /tmp/desktop_rpt 2>&1 I would need to grep the word desktop and display the /user/clean.desktop.sh and not the whole line. And if I have some more lines say, 05 21 * * 0,6 /user/clean.desktop.sh >... (1 Reply)
Discussion started by: Orbix
1 Replies
Login or Register to Ask a Question