awk - last instance of a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - last instance of a string
# 1  
Old 04-07-2011
awk - last instance of a string

How do I use awk to find the last occurence of a string in a file?

Last edited by locoroco; 04-07-2011 at 04:55 PM..
# 2  
Old 04-07-2011
Code:
grep string file | tail -1

If you insist on using awk:
Code:
awk '/string/{a=$0}END{print a}' file

This User Gave Thanks to mirni For This Post:
# 3  
Old 04-07-2011
Quote:
Originally Posted by mirni
Code:
grep string file | tail -1

If you insist on using awk:
Code:
awk '/string/{a=$0}END{print a}' file

Hi, mirni:

To match an arbitrary string, it's best to stay away from regular expression syntax, since the string could include metacharacters. Tweaking your code slightly:

Code:
grep -F string file | tail -n1

Code:
awk -v s=string 'index(s, $0) {a=$0} END {print a}' file

Regards,
Alister
These 2 Users Gave Thanks to alister For This Post:
# 4  
Old 04-07-2011
Code:
$ tac file  | grep -m1 string

This User Gave Thanks to kurumi For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

grep for a string until instance of a space

Hey guys, I'm having a bit of trouble getting this to work using either sed or grep. It's possible awk might be the ticket I need as well, but my regulat expression skills aren't quite up to the task for doing this. I'm looking to grep for the string ERROR from the following log up until any... (6 Replies)
Discussion started by: terrell
6 Replies

2. Shell Programming and Scripting

grep second instance of same string

Hi all, i am new to unix scripting in ksh or any shell for that matter. I have downloaded a xml file from a website and saved on my local harddrive. inside the xml, the same tag is listed multiple times. <title>Tonight</title> <title>Thursday</title> <title>Friday</title>... (6 Replies)
Discussion started by: scubasteve39
6 Replies

3. 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

4. Shell Programming and Scripting

awk - find first instance of string after NR==10

How do I use awk to find the NR of first instance of a specific string after eg NR==10? I need to find the first instance of the word "class" after a specific NR. (2 Replies)
Discussion started by: locoroco
2 Replies

5. UNIX for Dummies Questions & Answers

replacing a particular instance in a string globally

Hi, I'm trying to update the last two characters coming in a string globally in a file. Here is the sample data: file1 In file1, I want to have all instances replace where _o is appearing in the end of a word with _g. If _o is appearing in the middle or any other position except the... (2 Replies)
Discussion started by: er_ashu
2 Replies

6. 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

7. Shell Programming and Scripting

Deleting files that don't contain particular text strings / more than one instance of a string

Hi all, I have a directory containing many subdirectories each named like KOG#### where # represents any digit 0-9. There are several files in each KOG#### folder but the one I care about is named like KOG####_final.fasta. I am trying to write a script to copy all of the KOG####_final.fasta... (3 Replies)
Discussion started by: kmkocot
3 Replies

8. Shell Programming and Scripting

find the first instance after a string

I have this file (below) and need to get out specific data that appears after OSE1_1.FIX, but before OSE1_2.FIX. Specifically I need to get all of the data after "ROW80_20:", "ROW80_21:", "ROW80_22:" & "ROW80_23:" then I need to do the same for data the appears after OSE1_2.FIX, but before... (2 Replies)
Discussion started by: josslate
2 Replies

9. Shell Programming and Scripting

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 : add session begin for mfgeb on /dev/pts/th. : Converting relative path to absolute path. : add session end. Tue Jan 27 04:03:29... (6 Replies)
Discussion started by: dinesh1178
6 Replies

10. Shell Programming and Scripting

replace nth instance of string

Hi all, I have file with following content ........................... ..........TEST.......... ..........TEST.......... ..................... .....TEST.......... ..................... ..................... .....TEST.......... I want to replace nth "TEST" with "OK" using... (4 Replies)
Discussion started by: uttamhoode
4 Replies
Login or Register to Ask a Question